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