Completed
Push — master ( 9d8998...346ad6 )
by Dmitry
03:39
created

TarantoolTest::testPoolConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Exception;
8
use ReflectionClass;
9
use ReflectionProperty;
10
use Procedure\Greet;
11
use Repository\Note;
12
use Tarantool\Mapper\Bootstrap;
13
use Tarantool\Mapper\Mapper;
14
use Tarantool\Mapper\Pool;
15
use Basis\Test;
16
17
class TarantoolTest extends Test
18
{
19
    public function tearDown()
20
    {
21
        $fs = $this->app->get(Filesystem::class);
22
        $classes = $fs->listClasses('Migration');
23
24
        $dirs = [];
25
        foreach ($classes as $class) {
26
            $filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php');
27
            unlink($filename);
28
            $dirs[dirname($filename)] = true;
29
        }
30
        foreach ($dirs as $dir => $_) {
31
            rmdir($dir);
32
        }
33
    }
34
35
    public function testProcedureRegistration()
36
    {
37
        $this->assertSame($this->app->get(Greet::class)('Dmitry'), 'Hello, Dmitry!');
38
    }
39
40
    public function testMigrationOrder()
41
    {
42
        $migration = $this->app->dispatch('generate.migration', [
43
            'name' => 'b',
44
        ]);
45
        $contents = file_get_contents($migration['filename']);
46
        $contents = str_replace('throw', '//throw', $contents);
47
        file_put_contents($migration['filename'], $contents);
48
49
        sleep(1);
50
51
        $migration = $this->app->dispatch('generate.migration', [
52
            'name' => 'a',
53
        ]);
54
55
        $contents = file_get_contents($migration['filename']);
56
        $contents = str_replace('throw', '//throw', $contents);
57
        file_put_contents($migration['filename'], $contents);
58
59
        $this->app->dispatch('tarantool.migrate');
60
61
        $bootstrap = $this->app->get(Bootstrap::class);
62
63
        $reflection = new ReflectionClass(Bootstrap::class);
64
        $property = $reflection->getProperty('migrations');
65
        $property->setAccessible(true);
66
67
        $migrations = $property->getValue($bootstrap);
68
69
        $this->assertCount(2, $migrations);
70
71
        $order = [];
72
        foreach ($migrations as $migration) {
73
            $order[] = substr($migration, -1);
74
        }
75
        $this->assertSame(['B', 'A'], $order);
76
    }
77
78
    public function testMigrationGenerator()
79
    {
80
        $fs = $this->app->get(Filesystem::class);
81
82
        $classes = $fs->listClasses('Migration');
83
        $this->assertCount(0, $classes);
84
85
        $this->app->dispatch('generate.migration', [
86
            'name' => 'my migration created at ' . time(),
87
        ]);
88
89
        $classes = $fs->listClasses('Migration');
90
        $this->assertCount(1, $classes);
91
    }
92
93
    public function testEntity()
94
    {
95
        $this->app->dispatch('tarantool.migrate');
96
97
        $mapper = $this->app->get(Mapper::class);
98
        $mapper->getRepository('note')->truncate();
99
        $note = $mapper->getRepository('note')->create('zzz');
100
        $this->assertSame($note->message, 'zzz');
101
102
        $note->message = 'test';
103
        $note->save();
104
105
        $this->assertNotNull($note->id);
106
        $this->assertSame($note->message, 'test');
107
108
        $this->assertSame($note->app, $this->app);
109
110
        ob_start();
111
        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...
112
        $contents = ob_get_clean();
113
114
        $this->assertNotContains("app", $contents);
115
    }
116
117
    public function testRepositoryRegistration()
118
    {
119
        $repository = $this->app->get(Note::class);
120
        $this->assertSame($this->app->get(Mapper::class), $repository->getMapper());
121
    }
122
123
    public function testJobShortcuts()
124
    {
125
        $job = new class($this->app) extends Job {
126
            public function run(TarantoolTest $test)
127
            {
128
                // dispatch shortcut
129
                $result = $this->dispatch('test.hello');
130
                $test->assertSame($result, ['message' => 'hello world!']);
131
132
                // get instance shortcut
133
                $mapper = $this->get(Mapper::class);
134
                $mapper->getRepository('note')->truncate();
135
136
                // find shortcut
137
                $test->assertCount(0, $this->find('note'));
138
                $note = $this->create('note', ['message' => 'hello world']);
139
                $test->assertCount(1, $this->find('note'));
140
                // find one shortcut
141
                $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...
142
                $test->assertSame([$note], $this->find('note'));
143
144
                // find or create shortcut
145
                $testing = $this->findOrCreate('note', ['id' => $note->id]);
146
                $test->assertSame($note, $testing);
147
148
                $testing = $this->findOrCreate('note', ['id' => $note->id+1]);
149
                $test->assertCount(2, $this->find('note'));
150
151
                // find or fail shortcut
152
                $this->findOrFail('note', $testing->id);
153
154
                // remove shortcut
155
                $this->remove('note', ['id' => $testing->id]);
156
157
                $test->assertNull($this->findOne('note', ['id' => $testing->id]));
158
159
                $test->expectException(Exception::class);
160
                $this->findOrFail('note', $testing->id);
161
            }
162
        };
163
164
        $job->run($this);
165
166
        ob_start();
167
        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...
168
        $contents = ob_get_clean();
169
170
        $this->assertNotContains("app", $contents);
171
    }
172
173
    public function testPoolConfiguration()
174
    {
175
        $pool = $this->get(Pool::class);
176
177
        $this->mock('web.services')->willReturn(['names' => ['guard']]);
178
179
        $connection = $pool->get('guard')->getClient()->getConnection();
180
181
        $property = new ReflectionProperty(get_class($connection), 'uri');
182
        $property->setAccessible(true);
183
184
        $this->assertSame($property->getValue($connection), 'tcp://guard-db:3301');
185
186
        $this->expectException(Exception::class);
187
        $pool->get('gateway');
188
    }
189
}
190