1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\PrimeBundle\Tests; |
4
|
|
|
|
5
|
|
|
require_once __DIR__.'/TestKernel.php'; |
6
|
|
|
|
7
|
|
|
use Bdf\Prime\Cache\ArrayCache; |
8
|
|
|
use Bdf\Prime\Cache\DoctrineCacheAdapter; |
9
|
|
|
use Bdf\Prime\Connection\SimpleConnection; |
10
|
|
|
use Bdf\Prime\Console\UpgraderCommand; |
11
|
|
|
use Bdf\Prime\Locatorizable; |
12
|
|
|
use Bdf\Prime\Migration\MigrationManager; |
13
|
|
|
use Bdf\Prime\Schema\RepositoryUpgrader; |
14
|
|
|
use Bdf\Prime\Schema\StructureUpgraderResolverAggregate; |
15
|
|
|
use Bdf\Prime\Schema\StructureUpgraderResolverInterface; |
16
|
|
|
use Bdf\Prime\ServiceLocator; |
17
|
|
|
use Bdf\Prime\Sharding\ShardingConnection; |
18
|
|
|
use Bdf\Prime\Sharding\ShardingQuery; |
19
|
|
|
use Bdf\Prime\Types\ArrayType; |
20
|
|
|
use Bdf\Prime\Types\TypeInterface; |
21
|
|
|
use Bdf\PrimeBundle\Collector\PrimeDataCollector; |
22
|
|
|
use Bdf\PrimeBundle\DependencyInjection\Compiler\PrimeConnectionFactoryPass; |
23
|
|
|
use Bdf\PrimeBundle\PrimeBundle; |
24
|
|
|
use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
28
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
29
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
30
|
|
|
use Symfony\Component\Console\Command\LazyCommand; |
31
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
33
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
34
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* BdfSerializerBundleTest. |
38
|
|
|
*/ |
39
|
|
|
class BdfPrimeBundleTest extends TestCase |
40
|
|
|
{ |
41
|
|
|
public function testDefaultConfig() |
42
|
|
|
{ |
43
|
|
|
$builder = new ContainerBuilder(); |
44
|
|
|
$bundle = new PrimeBundle(); |
45
|
|
|
$bundle->build($builder); |
46
|
|
|
|
47
|
|
|
$compilerPasses = $builder->getCompiler()->getPassConfig()->getPasses(); |
48
|
|
|
$found = 0; |
49
|
|
|
|
50
|
|
|
foreach ($compilerPasses as $pass) { |
51
|
|
|
if ($pass instanceof PrimeConnectionFactoryPass) { |
52
|
|
|
++$found; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->assertSame(1, $found); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testKernel() |
60
|
|
|
{ |
61
|
|
|
$kernel = new \TestKernel('dev', true); |
62
|
|
|
$kernel->boot(); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('prime')); |
65
|
|
|
$this->assertSame($kernel->getContainer()->get('prime'), $kernel->getContainer()->get(ServiceLocator::class)); |
66
|
|
|
$this->assertInstanceOf(MigrationManager::class, $kernel->getContainer()->get(MigrationManager::class)); |
67
|
|
|
$this->assertInstanceOf(SimpleConnection::class, $kernel->getContainer()->get(ServiceLocator::class)->connection('test')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function testStructureUpgrader() |
74
|
|
|
{ |
75
|
|
|
if (!interface_exists(StructureUpgraderResolverInterface::class)) { |
76
|
|
|
$this->markTestSkipped('StructureUpgraderResolverInterface not present'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$kernel = new \TestKernel('dev', true); |
80
|
|
|
$kernel->boot(); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf(StructureUpgraderResolverAggregate::class, $kernel->getContainer()->get(StructureUpgraderResolverInterface::class)); |
83
|
|
|
$this->assertInstanceOf(StructureUpgraderResolverAggregate::class, $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)); |
84
|
|
|
|
85
|
|
|
$console = new Application($kernel); |
86
|
|
|
$command = $console->get(UpgraderCommand::getDefaultName()); |
|
|
|
|
87
|
|
|
|
88
|
|
|
if ($command instanceof LazyCommand) { |
89
|
|
|
$command = $command->getCommand(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$r = new \ReflectionProperty($command, 'resolver'); |
93
|
|
|
$r->setAccessible(true); |
94
|
|
|
|
95
|
|
|
$this->assertSame( |
96
|
|
|
$kernel->getContainer()->get(StructureUpgraderResolverAggregate::class), |
97
|
|
|
$r->getValue($command) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$upgrader = $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByDomainClass(\TestEntity::class); |
101
|
|
|
|
102
|
|
|
$this->assertInstanceOf(RepositoryUpgrader::class, $upgrader); |
103
|
|
|
$this->assertEquals('test_', $upgrader->table()->name()); |
104
|
|
|
|
105
|
|
|
$this->assertEquals($upgrader, $kernel->getContainer()->get(StructureUpgraderResolverAggregate::class)->resolveByMapperClass(\TestEntityMapper::class)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testCollector() |
109
|
|
|
{ |
110
|
|
|
$kernel = new \TestKernel('dev', true); |
111
|
|
|
$kernel->boot(); |
112
|
|
|
|
113
|
|
|
$collector = $kernel->getContainer()->get(PrimeDataCollector::class); |
114
|
|
|
|
115
|
|
|
$this->assertInstanceOf(PrimeDataCollector::class, $collector); |
116
|
|
|
$kernel->handle(Request::create('http://127.0.0.1/')); |
117
|
|
|
|
118
|
|
|
$this->assertGreaterThanOrEqual(3, $collector->getQueryCount()); |
119
|
|
|
|
120
|
|
|
$queries = array_map(function ($entry) { return $entry['sql']; }, $collector->getQueries()['']); |
121
|
|
|
$this->assertContains('SELECT * FROM test_ WHERE id = ? LIMIT 1', $queries); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testFunctional() |
125
|
|
|
{ |
126
|
|
|
$kernel = new \TestKernel('dev', true); |
127
|
|
|
$kernel->boot(); |
128
|
|
|
|
129
|
|
|
\TestEntity::repository()->schema()->migrate(); |
130
|
|
|
|
131
|
|
|
$entity = new \TestEntity(['name' => 'foo']); |
132
|
|
|
$entity->insert(); |
133
|
|
|
|
134
|
|
|
$this->assertEquals([$entity], \TestEntity::all()); |
|
|
|
|
135
|
|
|
$this->assertEquals($entity, \TestEntity::where('name', 'foo')->first()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testShardingConnection() |
139
|
|
|
{ |
140
|
|
|
$kernel = new class('test', true) extends Kernel { |
141
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
142
|
|
|
|
143
|
|
|
public function registerBundles(): iterable |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
new FrameworkBundle(), |
147
|
|
|
new PrimeBundle(), |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$loader->import(__DIR__.'/Fixtures/sharding.yaml'); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function configureRoutes(RouteCollectionBuilder $routes) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
} |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
$kernel->boot(); |
162
|
|
|
|
163
|
|
|
/** @var ServiceLocator $prime */ |
164
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
165
|
|
|
$this->assertInstanceOf(ShardingConnection::class, $prime->connection('test')); |
166
|
|
|
$this->assertInstanceOf(ShardingQuery::class, $prime->connection('test')->builder()); |
167
|
|
|
|
168
|
|
|
/** @var SimpleConnection $connection */ |
169
|
|
|
$connection = $prime->connection('test.shard1'); |
170
|
|
|
$this->assertSame($prime->connection('test')->getConfiguration(), $connection->getConfiguration()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testArrayCache() |
174
|
|
|
{ |
175
|
|
|
$kernel = new class('test', true) extends Kernel { |
176
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
177
|
|
|
|
178
|
|
|
public function registerBundles(): iterable |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
new FrameworkBundle(), |
182
|
|
|
new PrimeBundle(), |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$loader->import(__DIR__.'/Fixtures/array_cache.yaml'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
protected function configureRoutes($routes) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
} |
194
|
|
|
}; |
195
|
|
|
|
196
|
|
|
$kernel->boot(); |
197
|
|
|
|
198
|
|
|
/** @var ServiceLocator $prime */ |
199
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
200
|
|
|
$this->assertEquals(new ArrayCache(), $prime->mappers()->getResultCache()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testPoolCache() |
204
|
|
|
{ |
205
|
|
|
$kernel = new class('test', true) extends Kernel { |
206
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
207
|
|
|
|
208
|
|
|
public function registerBundles(): iterable |
209
|
|
|
{ |
210
|
|
|
return [ |
211
|
|
|
new FrameworkBundle(), |
212
|
|
|
new PrimeBundle(), |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
217
|
|
|
{ |
218
|
|
|
$c->register('custom.cache', FilesystemAdapter::class); |
219
|
|
|
|
220
|
|
|
$loader->import(__DIR__.'/Fixtures/pool_cache.yaml'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
protected function configureRoutes($routes) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
} |
226
|
|
|
}; |
227
|
|
|
|
228
|
|
|
$kernel->boot(); |
229
|
|
|
|
230
|
|
|
/** @var ServiceLocator $prime */ |
231
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
232
|
|
|
$this->assertEquals(new DoctrineCacheAdapter(DoctrineProvider::wrap(new FilesystemAdapter())), $prime->mappers()->getResultCache()); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testGlobalConfig() |
236
|
|
|
{ |
237
|
|
|
$kernel = new class('test', true) extends Kernel { |
238
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
239
|
|
|
|
240
|
|
|
public function registerBundles(): iterable |
241
|
|
|
{ |
242
|
|
|
return [ |
243
|
|
|
new FrameworkBundle(), |
244
|
|
|
new PrimeBundle(), |
245
|
|
|
]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$loader->import(__DIR__.'/Fixtures/config.yaml'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
protected function configureRoutes($routes) |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
} |
256
|
|
|
}; |
257
|
|
|
|
258
|
|
|
$kernel->boot(); |
259
|
|
|
|
260
|
|
|
/** @var ServiceLocator $prime */ |
261
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
262
|
|
|
/** @var SimpleConnection $connection */ |
263
|
|
|
$connection = $prime->connection('test2'); |
264
|
|
|
|
265
|
|
|
$this->assertNotNull($connection->getConfiguration()->getSQLLogger()); |
|
|
|
|
266
|
|
|
$this->assertTrue($connection->getConfiguration()->getAutoCommit()); |
267
|
|
|
$this->assertInstanceOf(FooType::class, $connection->getConfiguration()->getTypes()->get('foo')); |
268
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar')); |
269
|
|
|
$this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array')); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testConnectionConfig() |
273
|
|
|
{ |
274
|
|
|
$kernel = new class('test', true) extends Kernel { |
275
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
276
|
|
|
|
277
|
|
|
public function registerBundles(): iterable |
278
|
|
|
{ |
279
|
|
|
return [ |
280
|
|
|
new FrameworkBundle(), |
281
|
|
|
new PrimeBundle(), |
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
$loader->import(__DIR__.'/Fixtures/config.yaml'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
protected function configureRoutes($routes) |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
} |
293
|
|
|
}; |
294
|
|
|
|
295
|
|
|
$kernel->boot(); |
296
|
|
|
|
297
|
|
|
/** @var ServiceLocator $prime */ |
298
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
299
|
|
|
/** @var SimpleConnection $connection */ |
300
|
|
|
$connection = $prime->connection('test'); |
301
|
|
|
|
302
|
|
|
$this->assertNull($connection->getConfiguration()->getSQLLogger()); |
|
|
|
|
303
|
|
|
$this->assertFalse($connection->getConfiguration()->getAutoCommit()); |
304
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('foo')); |
305
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar')); |
306
|
|
|
$this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array')); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Check that destroying entity with prime unconfigured will works. |
311
|
|
|
*/ |
312
|
|
|
public function testEntityDestroyAfterShutdown() |
313
|
|
|
{ |
314
|
|
|
$this->expectNotToPerformAssertions(); |
315
|
|
|
|
316
|
|
|
$kernel = new \TestKernel('test', true); |
317
|
|
|
$kernel->boot(); |
318
|
|
|
\TestEntity::repository()->schema()->migrate(); |
319
|
|
|
$entity = new \TestEntity(['name' => 'foo']); |
320
|
|
|
$entity->insert(); |
321
|
|
|
$kernel->shutdown(); |
322
|
|
|
$kernel->boot(); |
323
|
|
|
$kernel->shutdown(); |
324
|
|
|
|
325
|
|
|
unset($entity); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function testShutdownShouldDisableActiveRecord() |
329
|
|
|
{ |
330
|
|
|
$kernel = new \TestKernel('test', true); |
331
|
|
|
$kernel->boot(); |
332
|
|
|
$this->assertTrue(Locatorizable::isActiveRecordEnabled()); |
333
|
|
|
$kernel->shutdown(); |
334
|
|
|
$this->assertFalse(Locatorizable::isActiveRecordEnabled()); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
class FooType implements TypeInterface |
339
|
|
|
{ |
340
|
|
|
public function fromDatabase($value, array $fieldOptions = []) |
341
|
|
|
{ |
342
|
|
|
// TODO: Implement fromDatabase() method. |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function toDatabase($value) |
346
|
|
|
{ |
347
|
|
|
// TODO: Implement toDatabase() method. |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function name(): string |
351
|
|
|
{ |
352
|
|
|
// TODO: Implement name() method. |
353
|
|
|
} |
|
|
|
|
354
|
|
|
|
355
|
|
|
public function phpType(): string |
356
|
|
|
{ |
357
|
|
|
// TODO: Implement phpType() method. |
358
|
|
|
} |
|
|
|
|
359
|
|
|
} |
360
|
|
|
class BarType implements TypeInterface |
361
|
|
|
{ |
362
|
|
|
public function fromDatabase($value, array $fieldOptions = []) |
363
|
|
|
{ |
364
|
|
|
// TODO: Implement fromDatabase() method. |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function toDatabase($value) |
368
|
|
|
{ |
369
|
|
|
// TODO: Implement toDatabase() method. |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function name(): string |
373
|
|
|
{ |
374
|
|
|
// TODO: Implement name() method. |
375
|
|
|
} |
|
|
|
|
376
|
|
|
|
377
|
|
|
public function phpType(): string |
378
|
|
|
{ |
379
|
|
|
// TODO: Implement phpType() method. |
380
|
|
|
} |
|
|
|
|
381
|
|
|
} |
382
|
|
|
|