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