|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Test; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Filesystem; |
|
6
|
|
|
use Basis\Job; |
|
7
|
|
|
use Basis\Procedure\Select; |
|
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
|
|
|
$this->assertCount(3, $this->find('web.services')); |
|
|
|
|
|
|
42
|
|
|
$this->assertCount(1, $this->find('web.services', ['name' => 'web'])); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame($this->getRepository('web.services'), $this->get(Pool::class)->get('web')->getRepository('services')); |
|
45
|
|
|
|
|
46
|
|
|
$tester = $this->get(Pool::class)->get('tester'); |
|
47
|
|
|
$this->assertCount(0, $tester->find('data', ['id' => 2])); |
|
48
|
|
|
$this->assertCount(1, $tester->find('data', ['id' => 3])); |
|
49
|
|
|
$this->assertCount(2, $tester->find('data', ['value' => 'test'])); |
|
50
|
|
|
|
|
51
|
|
|
$this->data['web.services'][] = ['id' => 4, 'name' => 'guard']; |
|
52
|
|
|
$this->assertCount(4, $web->find('services')); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($tester->findOne('data', ['value' => 'test'])->id, 3); |
|
55
|
|
|
$this->assertSame($tester->findOrFail('data', ['value' => 'test'])->id, 3); |
|
56
|
|
|
$this->assertNull($tester->findOne('data', ['id' => 1])); |
|
57
|
|
|
|
|
58
|
|
|
$this->expectException(Exception::class); |
|
59
|
|
|
$tester->findOrFail('data', ['id' => 1]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function tearDown(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$fs = $this->app->get(Filesystem::class); |
|
65
|
|
|
$classes = $fs->listClasses('Migration'); |
|
66
|
|
|
|
|
67
|
|
|
$dirs = []; |
|
68
|
|
|
foreach ($classes as $class) { |
|
69
|
|
|
$filename = $fs->getPath('php/'.str_replace('\\', '/', $class).'.php'); |
|
70
|
|
|
unlink($filename); |
|
71
|
|
|
$dirs[dirname($filename)] = true; |
|
72
|
|
|
} |
|
73
|
|
|
foreach ($dirs as $dir => $_) { |
|
74
|
|
|
rmdir($dir); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
parent::tearDown(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testProcedureRegistration() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->assertSame($this->app->get(Greet::class)('Dmitry'), 'Hello, Dmitry!'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testMigrationOrder() |
|
86
|
|
|
{ |
|
87
|
|
|
$migration = $this->app->dispatch('generate.migration', [ |
|
88
|
|
|
'name' => 'b', |
|
89
|
|
|
]); |
|
90
|
|
|
$contents = file_get_contents($migration->filename); |
|
91
|
|
|
$contents = str_replace('throw', '//throw', $contents); |
|
92
|
|
|
file_put_contents($migration->filename, $contents); |
|
93
|
|
|
|
|
94
|
|
|
sleep(1); |
|
95
|
|
|
|
|
96
|
|
|
$migration = $this->app->dispatch('generate.migration', [ |
|
97
|
|
|
'name' => 'a', |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
|
|
$contents = file_get_contents($migration->filename); |
|
101
|
|
|
$contents = str_replace('throw', '//throw', $contents); |
|
102
|
|
|
file_put_contents($migration->filename, $contents); |
|
103
|
|
|
|
|
104
|
|
|
$this->app->dispatch('tarantool.migrate'); |
|
105
|
|
|
|
|
106
|
|
|
$bootstrap = $this->app->get(Bootstrap::class); |
|
107
|
|
|
|
|
108
|
|
|
$reflection = new ReflectionClass(Bootstrap::class); |
|
109
|
|
|
$property = $reflection->getProperty('migrations'); |
|
110
|
|
|
$property->setAccessible(true); |
|
111
|
|
|
|
|
112
|
|
|
$migrations = $property->getValue($bootstrap); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertCount(3, $migrations); |
|
115
|
|
|
|
|
116
|
|
|
$order = []; |
|
117
|
|
|
foreach ($migrations as $migration) { |
|
118
|
|
|
$order[] = substr($migration, -1); |
|
119
|
|
|
} |
|
120
|
|
|
$this->assertSame(['e', 'B', 'A'], $order); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testMigrationGenerator() |
|
124
|
|
|
{ |
|
125
|
|
|
$fs = $this->app->get(Filesystem::class); |
|
126
|
|
|
|
|
127
|
|
|
$classes = $fs->listClasses('Migration'); |
|
128
|
|
|
$this->assertCount(0, $classes); |
|
129
|
|
|
|
|
130
|
|
|
$this->app->dispatch('generate.migration', [ |
|
131
|
|
|
'name' => 'my migration created at ' . time(), |
|
132
|
|
|
]); |
|
133
|
|
|
|
|
134
|
|
|
$classes = $fs->listClasses('Migration'); |
|
135
|
|
|
$this->assertCount(1, $classes); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testEntity() |
|
139
|
|
|
{ |
|
140
|
|
|
$mapper = $this->app->get(Mapper::class); |
|
141
|
|
|
$mapper->getRepository('note')->truncate(); |
|
142
|
|
|
$note = $mapper->getRepository('note')->create('zzz'); |
|
143
|
|
|
$this->assertSame($note->message, 'zzz'); |
|
144
|
|
|
|
|
145
|
|
|
$note->message = 'test'; |
|
146
|
|
|
$note->save(); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertNotNull($note->id); |
|
149
|
|
|
$this->assertSame($note->message, 'test'); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertSame($note->app, $this->app); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertStringNotContainsString("app", json_encode($note->__debugInfo())); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testRepositoryRegistration() |
|
157
|
|
|
{ |
|
158
|
|
|
$repository = $this->app->get(Note::class); |
|
159
|
|
|
$this->assertSame($this->app->get(Mapper::class), $repository->getMapper()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testJobShortcuts() |
|
163
|
|
|
{ |
|
164
|
|
|
$job = new class($this->app) extends Job { |
|
165
|
|
|
public function run(TarantoolTest $test) |
|
166
|
|
|
{ |
|
167
|
|
|
// dispatch shortcut |
|
168
|
|
|
$result = $this->dispatch('test.hello'); |
|
169
|
|
|
$test->assertSame($result->message, 'hello world!'); |
|
170
|
|
|
|
|
171
|
|
|
// get instance shortcut |
|
172
|
|
|
$mapper = $this->get(Mapper::class); |
|
173
|
|
|
$mapper->getRepository('note')->truncate(); |
|
174
|
|
|
|
|
175
|
|
|
// find shortcut |
|
176
|
|
|
$test->assertCount(0, $this->find('note')); |
|
|
|
|
|
|
177
|
|
|
$note = $this->create('note', ['message' => 'hello world']); |
|
178
|
|
|
$test->assertCount(1, $this->find('note')); |
|
|
|
|
|
|
179
|
|
|
// find one shortcut |
|
180
|
|
|
$test->assertNotNull($this->findOne('note', ['id' => $note->id])); |
|
181
|
|
|
$test->assertSame([$note], $this->find('note')); |
|
182
|
|
|
|
|
183
|
|
|
// find or create shortcut |
|
184
|
|
|
$testing = $this->findOrCreate('note', ['id' => $note->id]); |
|
185
|
|
|
$test->assertSame($note, $testing); |
|
186
|
|
|
|
|
187
|
|
|
$testing = $this->findOrCreate('note', ['id' => $note->id+1]); |
|
188
|
|
|
$test->assertCount(2, $this->find('note')); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
// find or fail shortcut |
|
191
|
|
|
$this->findOrFail('note', $testing->id); |
|
192
|
|
|
|
|
193
|
|
|
// remove shortcut |
|
194
|
|
|
$this->remove('note', ['id' => $testing->id]); |
|
195
|
|
|
|
|
196
|
|
|
$test->assertNull($this->findOne('note', ['id' => $testing->id])); |
|
197
|
|
|
|
|
198
|
|
|
$test->expectException(Exception::class); |
|
199
|
|
|
$this->findOrFail('note', $testing->id); |
|
200
|
|
|
} |
|
201
|
|
|
}; |
|
202
|
|
|
|
|
203
|
|
|
$job->run($this); |
|
204
|
|
|
|
|
205
|
|
|
ob_start(); |
|
206
|
|
|
var_dump($job); |
|
|
|
|
|
|
207
|
|
|
$contents = ob_get_clean(); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertNotContains("app", $contents); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function testPoolConfiguration() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->assertSame($this->getMapper()->serviceName, 'test'); |
|
215
|
|
|
|
|
216
|
|
|
$pool = $this->get(Pool::class); |
|
217
|
|
|
|
|
218
|
|
|
$this->mock('web.services')->willReturn(['services' => ['guard']]); |
|
219
|
|
|
$this->assertSame('guard', $pool->get('guard')->serviceName); |
|
220
|
|
|
|
|
221
|
|
|
$this->expectException(Exception::class); |
|
222
|
|
|
$pool->get('gateway'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function testSelectRegistration() |
|
226
|
|
|
{ |
|
227
|
|
|
// procedure was registered |
|
228
|
|
|
$mapper = $this->getMapper(); |
|
229
|
|
|
$result = $mapper->getClient()->evaluate("return basis_select(nil, nil, nil)"); |
|
230
|
|
|
$this->assertNull($result[0]); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function testSelectUsage() |
|
234
|
|
|
{ |
|
235
|
|
|
$mapper = $this->getMapper(); |
|
236
|
|
|
$mapper->getSchema() |
|
237
|
|
|
->createSpace('tester', [ |
|
238
|
|
|
'id' => 'unsigned', |
|
239
|
|
|
'name' => 'string', |
|
240
|
|
|
]) |
|
241
|
|
|
->createIndex('id') |
|
242
|
|
|
->createIndex('name'); |
|
243
|
|
|
|
|
244
|
|
|
$nekufa = $mapper->create('tester', ['id' => 1, 'name' => 'nekufa']); |
|
245
|
|
|
$bazyaba = $mapper->create('tester', ['id' => 2, 'name' => 'bazyaba']); |
|
246
|
|
|
|
|
247
|
|
|
$result = $this->get(Select::class) |
|
248
|
|
|
('tester', 'id', [$nekufa->id]); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertCount(1, $result); |
|
251
|
|
|
|
|
252
|
|
|
$result = $this->get(Select::class) |
|
253
|
|
|
('tester', 'id', [$nekufa->id, $nekufa->id]); |
|
254
|
|
|
|
|
255
|
|
|
$this->assertCount(1, $result); |
|
256
|
|
|
|
|
257
|
|
|
$result = $this->get(Select::class) |
|
258
|
|
|
('tester', 'id', [$bazyaba->id, $nekufa->id, $nekufa->id]); |
|
259
|
|
|
|
|
260
|
|
|
$this->assertCount(2, $result); |
|
261
|
|
|
|
|
262
|
|
|
$result = $this->get(Select::class) |
|
263
|
|
|
('tester', 'name', ['nekufa']); |
|
264
|
|
|
|
|
265
|
|
|
$this->assertCount(1, $result); |
|
266
|
|
|
|
|
267
|
|
|
$result = $this->get(Select::class) |
|
268
|
|
|
('tester', 'name', ['nekufa', 'bazyaba']); |
|
269
|
|
|
|
|
270
|
|
|
$this->assertCount(2, $result); |
|
271
|
|
|
|
|
272
|
|
|
$result = $this->get(Select::class) |
|
273
|
|
|
('tester', 'name', ['nekufa', 'bazyaba', 'nekufa', 'dmitry']); |
|
274
|
|
|
|
|
275
|
|
|
$this->assertCount(2, $result); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function testSelectUsageWithCompositeKeys() |
|
279
|
|
|
{ |
|
280
|
|
|
$mapper = $this->getMapper(); |
|
281
|
|
|
$mapper->getSchema() |
|
282
|
|
|
->createSpace('calendar', [ |
|
283
|
|
|
'year' => 'unsigned', |
|
284
|
|
|
'month' => 'unsigned', |
|
285
|
|
|
'day' => 'unsigned', |
|
286
|
|
|
]) |
|
287
|
|
|
->createIndex(['year', 'month', 'day']); |
|
288
|
|
|
|
|
289
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 4, 'day' => 1]); |
|
290
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 1]); |
|
291
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 2]); |
|
292
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 3]); |
|
293
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 5, 'day' => 5]); |
|
294
|
|
|
$mapper->create('calendar', ['year' => 2018, 'month' => 6, 'day' => 15]); |
|
295
|
|
|
|
|
296
|
|
|
$validations = [ |
|
297
|
|
|
[4, [[2018, 5]]], |
|
298
|
|
|
[5, [[2018, 5], [2018, 6]]], |
|
299
|
|
|
[2, [[2018, 5, 1], [2018, 5, 2]]], |
|
300
|
|
|
[1, [[2018, 5, 3], [2018, 5, 4]]], |
|
301
|
|
|
[2, [[2018, 5, 3], [2018, 5, 4], [2018, 4]]], |
|
302
|
|
|
[2, [[2018, 5, 3], [2018, 5, 4], [2018, 4], [2018, 4]]], |
|
303
|
|
|
]; |
|
304
|
|
|
|
|
305
|
|
|
$select = function($key) { |
|
306
|
|
|
return $this->get(Select::class)('calendar', 'year_month_day', $key); |
|
307
|
|
|
}; |
|
308
|
|
|
|
|
309
|
|
|
foreach ($validations as [$result, $keys]) { |
|
310
|
|
|
$this->assertCount($result, $select($keys)); |
|
|
|
|
|
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$spaceId = $mapper->getClient()->evaluate('return box.space.calendar.id')[0]; |
|
314
|
|
|
$indexId = $mapper->getClient()->evaluate('return box.space.calendar.index.year_month_day.id')[0]; |
|
315
|
|
|
|
|
316
|
|
|
$selectUsingSpaceAndIndexId = function($values) use ($spaceId, $indexId) { |
|
317
|
|
|
return $this->get(Select::class)($spaceId, $indexId, $values); |
|
318
|
|
|
}; |
|
319
|
|
|
|
|
320
|
|
|
foreach ($validations as [$result, $keys]) { |
|
321
|
|
|
$this->assertCount($result, $selectUsingSpaceAndIndexId($keys)); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
public function testSelectUsageWithNestedStructures() |
|
326
|
|
|
{ |
|
327
|
|
|
$mapper = $this->getMapper(); |
|
328
|
|
|
$mapper->getSchema() |
|
329
|
|
|
->createSpace('sector', [ |
|
330
|
|
|
'id' => 'unsigned', |
|
331
|
|
|
]) |
|
332
|
|
|
->addProperty('parent', 'unsigned', [ |
|
333
|
|
|
'is_nullable' => false, |
|
334
|
|
|
'reference' => 'sector' |
|
335
|
|
|
]) |
|
336
|
|
|
->createIndex([ |
|
337
|
|
|
'fields' => ['id'], |
|
338
|
|
|
]) |
|
339
|
|
|
->createIndex([ |
|
340
|
|
|
'fields' => ['parent'], |
|
341
|
|
|
'unique' => false, |
|
342
|
|
|
]); |
|
343
|
|
|
|
|
344
|
|
|
$root = $this->create('sector', []); |
|
345
|
|
|
$moscow = $this->create('sector', ['parent' => $root]); |
|
346
|
|
|
$kaluga = $this->create('sector', ['parent' => $root]); |
|
347
|
|
|
$obninsk = $this->create('sector', ['parent' => $kaluga]); |
|
348
|
|
|
|
|
349
|
|
|
$select = function($values) { |
|
350
|
|
|
return $this->get(Select::class)('sector', 'id', $values); |
|
351
|
|
|
}; |
|
352
|
|
|
|
|
353
|
|
|
$validations = [ |
|
354
|
|
|
[1, [$obninsk->id]], |
|
355
|
|
|
[2, [$kaluga->id]], |
|
356
|
|
|
[3, [$kaluga->id, $moscow->id]], |
|
357
|
|
|
[4, [$obninsk->id, $root->id]], |
|
358
|
|
|
]; |
|
359
|
|
|
|
|
360
|
|
|
foreach ($validations as [$result, $input]) { |
|
361
|
|
|
$this->assertCount($result, $select($input)); |
|
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
$client = $mapper->getClient(); |
|
365
|
|
|
|
|
366
|
|
|
$selectUsingNetBox = function($values) use ($client) { |
|
367
|
|
|
return $client->evaluate(" |
|
368
|
|
|
return require('net.box').connect('tcp://localhost:3301') |
|
369
|
|
|
:call('basis_select', { |
|
370
|
|
|
'sector', 'id', ... |
|
371
|
|
|
}) |
|
372
|
|
|
", $values)[0]; |
|
373
|
|
|
}; |
|
374
|
|
|
|
|
375
|
|
|
foreach ($validations as [$result, $input]) { |
|
376
|
|
|
$this->assertCount($result, $selectUsingNetBox($input)); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
$spaceId = $client->evaluate("return box.space.sector.id")[0]; |
|
380
|
|
|
$indexId = $client->evaluate("return box.space.sector.index.id.id")[0]; |
|
381
|
|
|
|
|
382
|
|
|
$selectUsingNetBoxWithoutNames = function($values) use ($client, $spaceId, $indexId) { |
|
383
|
|
|
return $client->evaluate(" |
|
384
|
|
|
return require('net.box').connect('tcp://localhost:3301') |
|
385
|
|
|
:call('basis_select', { |
|
386
|
|
|
$spaceId, $indexId, ... |
|
387
|
|
|
}) |
|
388
|
|
|
", $values)[0]; |
|
389
|
|
|
}; |
|
390
|
|
|
|
|
391
|
|
|
foreach ($validations as [$result, $input]) { |
|
392
|
|
|
$this->assertCount($result, $selectUsingNetBoxWithoutNames($input)); |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: