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\Migration\MigrationManager; |
11
|
|
|
use Bdf\Prime\ServiceLocator; |
12
|
|
|
use Bdf\Prime\Sharding\ShardingConnection; |
13
|
|
|
use Bdf\Prime\Sharding\ShardingQuery; |
14
|
|
|
use Bdf\Prime\Types\ArrayType; |
15
|
|
|
use Bdf\Prime\Types\TypeInterface; |
16
|
|
|
use Bdf\PrimeBundle\Collector\PrimeDataCollector; |
17
|
|
|
use Bdf\PrimeBundle\DependencyInjection\Compiler\PrimeConnectionFactoryPass; |
18
|
|
|
use Bdf\PrimeBundle\PrimeBundle; |
19
|
|
|
use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
20
|
|
|
use Doctrine\DBAL\Driver; |
21
|
|
|
use PHPUnit\Framework\TestCase; |
22
|
|
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
23
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
24
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
28
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* BdfSerializerBundleTest |
32
|
|
|
*/ |
33
|
|
|
class BdfPrimeBundleTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
public function test_default_config() |
36
|
|
|
{ |
37
|
|
|
$builder = new ContainerBuilder(); |
38
|
|
|
$bundle = new PrimeBundle(); |
39
|
|
|
$bundle->build($builder); |
40
|
|
|
|
41
|
|
|
$compilerPasses = $builder->getCompiler()->getPassConfig()->getPasses(); |
42
|
|
|
$found = 0; |
43
|
|
|
|
44
|
|
|
foreach ($compilerPasses as $pass) { |
45
|
|
|
if ($pass instanceof PrimeConnectionFactoryPass) { |
46
|
|
|
$found++; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->assertSame(1, $found); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
public function test_kernel() |
57
|
|
|
{ |
58
|
|
|
$kernel = new \TestKernel('dev', true); |
59
|
|
|
$kernel->boot(); |
60
|
|
|
|
61
|
|
|
$this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('prime')); |
62
|
|
|
$this->assertSame($kernel->getContainer()->get('prime'), $kernel->getContainer()->get(ServiceLocator::class)); |
63
|
|
|
$this->assertInstanceOf(MigrationManager::class, $kernel->getContainer()->get(MigrationManager::class)); |
64
|
|
|
$this->assertInstanceOf(SimpleConnection::class, $kernel->getContainer()->get(ServiceLocator::class)->connection('test')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function test_collector() |
71
|
|
|
{ |
72
|
|
|
$kernel = new \TestKernel('dev', true); |
73
|
|
|
$kernel->boot(); |
74
|
|
|
|
75
|
|
|
$collector = $kernel->getContainer()->get(PrimeDataCollector::class); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf(PrimeDataCollector::class, $collector); |
78
|
|
|
$kernel->handle(Request::create('http://127.0.0.1/')); |
79
|
|
|
|
80
|
|
|
$isDoctrine2 = method_exists(Driver::class, 'getDatabase'); |
81
|
|
|
|
82
|
|
|
$this->assertEquals($isDoctrine2 ? 3 : 4, $collector->getQueryCount()); // Doctrine 3 always perform a select query on Connection::getDatabase() |
83
|
|
|
$this->assertEquals('SELECT * FROM test_ WHERE id = ? LIMIT 1', $collector->getQueries()[''][$isDoctrine2 ? 3 : 4]['sql']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
public function test_functional() |
90
|
|
|
{ |
91
|
|
|
$kernel = new \TestKernel('dev', true); |
92
|
|
|
$kernel->boot(); |
93
|
|
|
|
94
|
|
|
\TestEntity::repository()->schema()->migrate(); |
95
|
|
|
|
96
|
|
|
$entity = new \TestEntity(['name' => 'foo']); |
97
|
|
|
$entity->insert(); |
98
|
|
|
|
99
|
|
|
$this->assertEquals([$entity], \TestEntity::all()); |
|
|
|
|
100
|
|
|
$this->assertEquals($entity, \TestEntity::where('name', 'foo')->first()); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
public function test_sharding_connection() |
107
|
|
|
{ |
108
|
|
|
$kernel = new class('test', true) extends Kernel { |
109
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
110
|
|
|
|
111
|
|
|
public function registerBundles(): iterable |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
new FrameworkBundle(), |
115
|
|
|
new PrimeBundle(), |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$loader->import(__DIR__.'/Fixtures/sharding.yaml'); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function configureRoutes(RouteCollectionBuilder $routes) { } |
|
|
|
|
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
$kernel->boot(); |
128
|
|
|
|
129
|
|
|
/** @var ServiceLocator $prime */ |
130
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
131
|
|
|
$this->assertInstanceOf(ShardingConnection::class, $prime->connection('test')); |
132
|
|
|
$this->assertInstanceOf(ShardingQuery::class, $prime->connection('test')->builder()); |
133
|
|
|
|
134
|
|
|
/** @var SimpleConnection $connection */ |
135
|
|
|
$connection = $prime->connection('test.shard1'); |
136
|
|
|
$this->assertSame($prime->connection('test')->getConfiguration(), $connection->getConfiguration()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
*/ |
142
|
|
|
public function test_array_cache() |
143
|
|
|
{ |
144
|
|
|
$kernel = new class('test', true) extends Kernel { |
145
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
146
|
|
|
|
147
|
|
|
public function registerBundles(): iterable |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
|
|
new FrameworkBundle(), |
151
|
|
|
new PrimeBundle(), |
152
|
|
|
]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$loader->import(__DIR__.'/Fixtures/array_cache.yaml'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
161
|
|
|
}; |
162
|
|
|
|
163
|
|
|
$kernel->boot(); |
164
|
|
|
|
165
|
|
|
/** @var ServiceLocator $prime */ |
166
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
167
|
|
|
$this->assertEquals(new ArrayCache(), $prime->mappers()->getResultCache()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
*/ |
173
|
|
|
public function test_pool_cache() |
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
|
|
|
$c->register('custom.cache', FilesystemAdapter::class); |
189
|
|
|
|
190
|
|
|
$loader->import(__DIR__.'/Fixtures/pool_cache.yaml'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
194
|
|
|
}; |
195
|
|
|
|
196
|
|
|
$kernel->boot(); |
197
|
|
|
|
198
|
|
|
/** @var ServiceLocator $prime */ |
199
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
200
|
|
|
$this->assertEquals(new DoctrineCacheAdapter(DoctrineProvider::wrap(new FilesystemAdapter())), $prime->mappers()->getResultCache()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* |
205
|
|
|
*/ |
206
|
|
|
public function test_global_config() |
207
|
|
|
{ |
208
|
|
|
$kernel = new class('test', true) extends Kernel { |
209
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
210
|
|
|
|
211
|
|
|
public function registerBundles(): iterable |
212
|
|
|
{ |
213
|
|
|
return [ |
214
|
|
|
new FrameworkBundle(), |
215
|
|
|
new PrimeBundle(), |
216
|
|
|
]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
$loader->import(__DIR__.'/Fixtures/config.yaml'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
225
|
|
|
}; |
226
|
|
|
|
227
|
|
|
$kernel->boot(); |
228
|
|
|
|
229
|
|
|
/** @var ServiceLocator $prime */ |
230
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
231
|
|
|
/** @var SimpleConnection $connection */ |
232
|
|
|
$connection = $prime->connection('test2'); |
233
|
|
|
|
234
|
|
|
$this->assertNotNull($connection->getConfiguration()->getSQLLogger()); |
235
|
|
|
$this->assertTrue($connection->getConfiguration()->getAutoCommit()); |
236
|
|
|
$this->assertInstanceOf(FooType::class, $connection->getConfiguration()->getTypes()->get('foo')); |
237
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar')); |
238
|
|
|
$this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array')); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* |
243
|
|
|
*/ |
244
|
|
|
public function test_connection_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('test'); |
271
|
|
|
|
272
|
|
|
$this->assertNull($connection->getConfiguration()->getSQLLogger()); |
273
|
|
|
$this->assertFalse($connection->getConfiguration()->getAutoCommit()); |
274
|
|
|
$this->assertInstanceOf(BarType::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
|
|
|
class FooType implements TypeInterface |
281
|
|
|
{ |
282
|
|
|
public function fromDatabase($value, array $fieldOptions = []) |
283
|
|
|
{ |
284
|
|
|
// TODO: Implement fromDatabase() method. |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function toDatabase($value) |
288
|
|
|
{ |
289
|
|
|
// TODO: Implement toDatabase() method. |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function name(): string |
293
|
|
|
{ |
294
|
|
|
// TODO: Implement name() method. |
295
|
|
|
} |
|
|
|
|
296
|
|
|
|
297
|
|
|
public function phpType(): string |
298
|
|
|
{ |
299
|
|
|
// TODO: Implement phpType() method. |
300
|
|
|
} |
|
|
|
|
301
|
|
|
} |
302
|
|
|
class BarType implements TypeInterface |
303
|
|
|
{ |
304
|
|
|
public function fromDatabase($value, array $fieldOptions = []) |
305
|
|
|
{ |
306
|
|
|
// TODO: Implement fromDatabase() method. |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function toDatabase($value) |
310
|
|
|
{ |
311
|
|
|
// TODO: Implement toDatabase() method. |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function name(): string |
315
|
|
|
{ |
316
|
|
|
// TODO: Implement name() method. |
317
|
|
|
} |
|
|
|
|
318
|
|
|
|
319
|
|
|
public function phpType(): string |
320
|
|
|
{ |
321
|
|
|
// TODO: Implement phpType() method. |
322
|
|
|
} |
|
|
|
|
323
|
|
|
} |
324
|
|
|
|