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