Completed
Push — master ( f0a08a...a3469c )
by Dmitry
05:34
created

testIndexInArrayProcedureUsageWithCompositeKeys()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace Test;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
use Basis\Procedure\IndexInArray;
8
use Basis\Test;
9
use Exception;
10
use Procedure\Greet;
11
use ReflectionClass;
12
use Repository\Note;
13
use Tarantool\Mapper\Bootstrap;
14
use Tarantool\Mapper\Mapper;
15
use Tarantool\Mapper\Pool;
16
17
class TarantoolTest extends Test
18
{
19
    public $data = [
20
        'guard.session' => [],
21
        'web.services' => [
22
            ['id' => 1, 'name' => 'tester'],
23
            ['id' => 2, 'name' => 'basis'],
24
            ['id' => 3, 'name' => 'web'],
25
        ],
26
        'tester.data' => [
27
            ['id' => 3, 'value' => 'test'],
28
            ['id' => 4, 'value' => 'test'],
29
        ],
30
    ];
31
32
    public $mocks = [
33
        ['web.services', [], ['services' => ['web', 'tester', 'basis']]]
34
    ];
35
36
    public function testPool()
37
    {
38
        $web = $this->get(Pool::class)->get('web');
39
        $this->assertCount(3, $web->find('services'));
40
        $this->assertCount(1, $web->find('services', ['name' => 'web']));
41
42
        $tester = $this->get(Pool::class)->get('tester');
43
        $this->assertCount(0, $tester->find('data', ['id' => 2]));
44
        $this->assertCount(1, $tester->find('data', ['id' => 3]));
45
        $this->assertCount(2, $tester->find('data', ['value' => 'test']));
46
47
        $this->data['web.services'][] = ['id' => 4, 'name' => 'guard'];
48
        $this->assertCount(4, $web->find('services'));
49
50
        $this->assertSame($tester->findOne('data', ['value' => 'test'])->id, 3);
51
        $this->assertSame($tester->findOrFail('data', ['value' => 'test'])->id, 3);
52
        $this->assertNull($tester->findOne('data', ['id' => 1]));
53
54
        $this->expectException(Exception::class);
55
        $tester->findOrFail('data', ['id' => 1]);
56
    }
57
58
    public function tearDown()
59
    {
60
        $fs = $this->app->get(Filesystem::class);
61
        $classes = $fs->listClasses('Migration');
62
63
        $dirs = [];
64
        foreach ($classes as $class) {
65
            $filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php');
66
            unlink($filename);
67
            $dirs[dirname($filename)] = true;
68
        }
69
        foreach ($dirs as $dir => $_) {
70
            rmdir($dir);
71
        }
72
    }
73
74
    public function testProcedureRegistration()
75
    {
76
        $this->assertSame($this->app->get(Greet::class)('Dmitry'), 'Hello, Dmitry!');
77
    }
78
79
    public function testMigrationOrder()
80
    {
81
        $migration = $this->app->dispatch('generate.migration', [
82
            'name' => 'b',
83
        ]);
84
        $contents = file_get_contents($migration->filename);
85
        $contents = str_replace('throw', '//throw', $contents);
86
        file_put_contents($migration->filename, $contents);
87
88
        sleep(1);
89
90
        $migration = $this->app->dispatch('generate.migration', [
91
            'name' => 'a',
92
        ]);
93
94
        $contents = file_get_contents($migration->filename);
95
        $contents = str_replace('throw', '//throw', $contents);
96
        file_put_contents($migration->filename, $contents);
97
98
        $this->app->dispatch('tarantool.migrate');
99
100
        $bootstrap = $this->app->get(Bootstrap::class);
101
102
        $reflection = new ReflectionClass(Bootstrap::class);
103
        $property = $reflection->getProperty('migrations');
104
        $property->setAccessible(true);
105
106
        $migrations = $property->getValue($bootstrap);
107
108
        $this->assertCount(2, $migrations);
109
110
        $order = [];
111
        foreach ($migrations as $migration) {
112
            $order[] = substr($migration, -1);
113
        }
114
        $this->assertSame(['B', 'A'], $order);
115
    }
116
117
    public function testMigrationGenerator()
118
    {
119
        $fs = $this->app->get(Filesystem::class);
120
121
        $classes = $fs->listClasses('Migration');
122
        $this->assertCount(0, $classes);
123
124
        $this->app->dispatch('generate.migration', [
125
            'name' => 'my migration created at ' . time(),
126
        ]);
127
128
        $classes = $fs->listClasses('Migration');
129
        $this->assertCount(1, $classes);
130
    }
131
132
    public function testEntity()
133
    {
134
        $mapper = $this->app->get(Mapper::class);
135
        $mapper->getRepository('note')->truncate();
136
        $note = $mapper->getRepository('note')->create('zzz');
137
        $this->assertSame($note->message, 'zzz');
138
139
        $note->message = 'test';
140
        $note->save();
141
142
        $this->assertNotNull($note->id);
143
        $this->assertSame($note->message, 'test');
144
145
        $this->assertSame($note->app, $this->app);
146
147
        ob_start();
148
        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...
149
        $contents = ob_get_clean();
150
151
        $this->assertNotContains("app", $contents);
152
    }
153
154
    public function testRepositoryRegistration()
155
    {
156
        $repository = $this->app->get(Note::class);
157
        $this->assertSame($this->app->get(Mapper::class), $repository->getMapper());
158
    }
159
160
    public function testJobShortcuts()
161
    {
162
        $job = new class($this->app) extends Job {
163
            public function run(TarantoolTest $test)
164
            {
165
                // dispatch shortcut
166
                $result = $this->dispatch('test.hello');
167
                $test->assertSame($result->message, 'hello world!');
168
169
                // get instance shortcut
170
                $mapper = $this->get(Mapper::class);
171
                $mapper->getRepository('note')->truncate();
172
173
                // find shortcut
174
                $test->assertCount(0, $this->find('note'));
175
                $note = $this->create('note', ['message' => 'hello world']);
176
                $test->assertCount(1, $this->find('note'));
177
                // find one shortcut
178
                $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...
179
                $test->assertSame([$note], $this->find('note'));
180
181
                // find or create shortcut
182
                $testing = $this->findOrCreate('note', ['id' => $note->id]);
183
                $test->assertSame($note, $testing);
184
185
                $testing = $this->findOrCreate('note', ['id' => $note->id+1]);
186
                $test->assertCount(2, $this->find('note'));
187
188
                // find or fail shortcut
189
                $this->findOrFail('note', $testing->id);
190
191
                // remove shortcut
192
                $this->remove('note', ['id' => $testing->id]);
193
194
                $test->assertNull($this->findOne('note', ['id' => $testing->id]));
195
196
                $test->expectException(Exception::class);
197
                $this->findOrFail('note', $testing->id);
198
            }
199
        };
200
201
        $job->run($this);
202
203
        ob_start();
204
        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...
205
        $contents = ob_get_clean();
206
207
        $this->assertNotContains("app", $contents);
208
    }
209
210
    public function testPoolConfiguration()
211
    {
212
        $pool = $this->get(Pool::class);
213
214
        $this->mock('web.services')->willReturn(['services' => ['guard']]);
215
        $this->assertSame('guard', $pool->get('guard')->serviceName);
216
217
        $this->expectException(Exception::class);
218
        $pool->get('gateway');
219
    }
220
221
    public function testIndexInArrayProcedureRegistration()
222
    {
223
        $this->dispatch('tarantool.clear');
224
        $this->dispatch('module.bootstrap');
225
226
        // procedure was registered
227
        $mapper = $this->getMapper();
228
        $result = $mapper->getClient()->evaluate("return basis_indexinarray(nil, nil, nil)")->getData();
229
        $this->assertNull($result[0]);
230
    }
231
232
    public function testIndexInArrayProcedureUsage()
233
    {
234
        $this->dispatch('tarantool.clear');
235
        $this->dispatch('module.bootstrap');
236
237
        $mapper = $this->getMapper();
238
        $mapper->getSchema()
239
            ->createSpace('tester', [
240
                'id' => 'unsigned',
241
                'name' => 'string',
242
            ])
243
            ->createIndex('id')
244
            ->createIndex('name');
245
246
        $nekufa = $mapper->create('tester', ['id' => 1, 'name' => 'nekufa']);
247
        $bazyaba = $mapper->create('tester', ['id' => 2, 'name' => 'bazyaba']);
248
249
        $result = $this->get(IndexInArray::class)
250
            ('tester', 'id', [$nekufa->id]);
251
252
        $this->assertCount(1, $result);
253
254
        $result = $this->get(IndexInArray::class)
255
            ('tester', 'id', [$nekufa->id, $nekufa->id]);
256
257
        $this->assertCount(1, $result);
258
259
        $result = $this->get(IndexInArray::class)
260
            ('tester', 'id', [$bazyaba->id, $nekufa->id, $nekufa->id]);
261
262
        $this->assertCount(2, $result);
263
264
        $result = $this->get(IndexInArray::class)
265
            ('tester', 'name', ['nekufa']);
266
267
        $this->assertCount(1, $result);
268
269
        $result = $this->get(IndexInArray::class)
270
            ('tester', 'name', ['nekufa', 'bazyaba']);
271
272
        $this->assertCount(2, $result);
273
274
        $result = $this->get(IndexInArray::class)
275
            ('tester', 'name', ['nekufa', 'bazyaba', 'nekufa', 'dmitry']);
276
277
        $this->assertCount(2, $result);
278
    }
279
280
    public function testIndexInArrayProcedureUsageWithCompositeKeys()
281
    {
282
        $this->dispatch('tarantool.clear');
283
        $this->dispatch('module.bootstrap');
284
285
        $mapper = $this->getMapper();
286
        $mapper->getSchema()
287
            ->createSpace('calendar', [
288
                'year' => 'integer',
289
                'month' => 'integer',
290
                'day' => 'integer',
291
            ])
292
            ->createIndex(['year', 'month', 'day']);
293
294
        $mapper->create('calendar', ['year' => 2018, 'month' => 4, 'day' => 1]);
295
        $mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 1]);
296
        $mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 2]);
297
        $mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 3]);
298
        $mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 5]);
299
        $mapper->create('calendar', ['year' => 2018, 'month' => 6, 'day' => 15]);
300
301
        $select = function($values) {
302
            return $this->get(IndexInArray::class)('calendar', 'year_month_day', $values);
303
        };
304
305
        $this->assertCount(4, $select([[2018, 5]]));
306
        $this->assertCount(5, $select([[2018, 5], [2018, 6]]));
307
        $this->assertCount(2, $select([[2018, 5, 1], [2018, 5, 2]]));
308
        $this->assertCount(1, $select([[2018, 5, 3], [2018, 5, 4]]));
309
        $this->assertCount(2, $select([[2018, 5, 3], [2018, 5, 4], [2018, 4]]));
310
        $this->assertCount(2, $select([[2018, 5, 3], [2018, 5, 4], [2018, 4], [2018, 4]]));
311
    }
312
}
313
314