Completed
Push — master ( 145864...6ab5d4 )
by Dmitry
03:34
created

anonymous//test/php/Test/TarantoolTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 1
lcom 1
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Exception;
8
use ReflectionClass;
9
use Procedure\Greet;
10
use Repository\Note;
11
use Tarantool\Mapper\Bootstrap;
12
use Tarantool\Mapper\Mapper;
13
14
class TarantoolTest extends TestSuite
15
{
16
    public function setup()
17
    {
18
        parent::setup();
19
        $this->app->dispatch('tarantool.clear');
20
21
        parent::setup();
22
23
        $fs = $this->app->get(Filesystem::class);
24
        $classes = $fs->listClasses('Migration');
25
26
        $dirs = [];
27
        foreach ($classes as $class) {
28
            $filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php');
29
            unlink($filename);
30
            $dirs[dirname($filename)] = true;
31
        }
32
        foreach ($dirs as $dir => $_) {
33
            rmdir($dir);
34
        }
35
36
        $this->app->dispatch('tarantool.migrate');
37
    }
38
39
    public function testProcedureRegistration()
40
    {
41
        $this->assertSame($this->app->get(Greet::class)('Dmitry'), 'Hello, Dmitry!');
42
    }
43
44
    public function testMigrationOrder()
45
    {
46
        $migration = $this->app->dispatch('generate.migration', [
47
            'name' => 'b',
48
        ]);
49
        $contents = file_get_contents($migration['filename']);
50
        $contents = str_replace('throw', '//throw', $contents);
51
        file_put_contents($migration['filename'], $contents);
52
53
        sleep(1);
54
55
        $migration = $this->app->dispatch('generate.migration', [
56
            'name' => 'a',
57
        ]);
58
59
        $contents = file_get_contents($migration['filename']);
60
        $contents = str_replace('throw', '//throw', $contents);
61
        file_put_contents($migration['filename'], $contents);
62
63
        $this->app->dispatch('tarantool.migrate');
64
65
        $bootstrap = $this->app->get(Bootstrap::class);
66
67
        $reflection = new ReflectionClass(Bootstrap::class);
68
        $property = $reflection->getProperty('migrations');
69
        $property->setAccessible(true);
70
71
        $migrations = $property->getValue($bootstrap);
72
73
        $this->assertCount(2, $migrations);
74
75
        $order = [];
76
        foreach ($migrations as $migration) {
77
            $order[] = substr($migration, -1);
78
        }
79
        $this->assertSame(['B', 'A'], $order);
80
    }
81
82
    public function testMigrationGenerator()
83
    {
84
        $fs = $this->app->get(Filesystem::class);
85
86
        $classes = $fs->listClasses('Migration');
87
        $this->assertCount(0, $classes);
88
89
        $this->app->dispatch('generate.migration', [
90
            'name' => 'my migration created at ' . time(),
91
        ]);
92
93
        $classes = $fs->listClasses('Migration');
94
        $this->assertCount(1, $classes);
95
    }
96
97
    public function testEntity()
98
    {
99
        $this->app->dispatch('tarantool.migrate');
100
101
        $mapper = $this->app->get(Mapper::class);
102
        $mapper->getRepository('note')->truncate();
103
        $note = $mapper->getRepository('note')->create('zzz');
104
        $this->assertSame($note->message, 'zzz');
105
106
        $note->message = 'test';
107
        $note->save();
108
109
        $this->assertNotNull($note->id);
110
        $this->assertSame($note->message, 'test');
111
112
        $this->assertSame($note->app, $this->app);
113
114
        ob_start();
115
        var_dump($note);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($note); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
116
        $contents = ob_get_clean();
117
118
        $this->assertNotContains("app", $contents);
119
    }
120
121
    public function testRepositoryRegistration()
122
    {
123
        $repository = $this->app->get(Note::class);
124
        $this->assertSame($this->app->get(Mapper::class), $repository->getMapper());
125
    }
126
127
    public function testJobShortcuts()
128
    {
129
        $job = new class($this->app) extends Job {
130
            public function run(TarantoolTest $test)
131
            {
132
                // dispatch shortcut
133
                $result = $this->dispatch('example.hello');
134
                $test->assertSame($result, ['message' => 'hello world!']);
135
136
                // get instance shortcut
137
                $mapper = $this->get(Mapper::class);
138
                $mapper->getRepository('note')->truncate();
139
140
                // find shortcut
141
                $test->assertCount(0, $this->find('note'));
142
                $note = $this->create('note', ['message' => 'hello world']);
143
                $test->assertCount(1, $this->find('note'));
144
                // find one shortcut
145
                $test->assertNotNull($this->findOne('note', ['id' => $note->id]));
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Tarantool\Mapper\Entity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
146
                $test->assertSame([$note], $this->find('note'));
147
148
                // find or create shortcut
149
                $testing = $this->findOrCreate('note', ['id' => $note->id]);
150
                $test->assertSame($note, $testing);
151
152
                $testing = $this->findOrCreate('note', ['id' => $note->id+1]);
153
                $test->assertCount(2, $this->find('note'));
154
155
                // find or fail shortcut
156
                $this->findOrFail('note', $testing->id);
157
158
                // remove shortcut
159
                $this->remove('note', ['id' => $testing->id]);
160
161
                $test->assertNull($this->findOne('note', ['id' => $testing->id]));
162
163
                $test->expectException(Exception::class);
164
                $this->findOrFail('note', $testing->id);
165
            }
166
        };
167
168
        $job->run($this);
169
170
        ob_start();
171
        var_dump($job);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($job); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
172
        $contents = ob_get_clean();
173
174
        $this->assertNotContains("app", $contents);
175
    }
176
}
177