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\PrimeBundle\Collector\PrimeDataCollector; |
16
|
|
|
use Bdf\PrimeBundle\DependencyInjection\Compiler\PrimeConnectionFactoryPass; |
17
|
|
|
use Bdf\PrimeBundle\PrimeBundle; |
18
|
|
|
use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
19
|
|
|
use Doctrine\DBAL\Driver; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
22
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
23
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
26
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
27
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* BdfSerializerBundleTest |
31
|
|
|
*/ |
32
|
|
|
class BdfPrimeBundleTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
public function test_default_config() |
35
|
|
|
{ |
36
|
|
|
$builder = new ContainerBuilder(); |
37
|
|
|
$bundle = new PrimeBundle(); |
38
|
|
|
$bundle->build($builder); |
39
|
|
|
|
40
|
|
|
$compilerPasses = $builder->getCompiler()->getPassConfig()->getPasses(); |
41
|
|
|
$found = 0; |
42
|
|
|
|
43
|
|
|
foreach ($compilerPasses as $pass) { |
44
|
|
|
if ($pass instanceof PrimeConnectionFactoryPass) { |
45
|
|
|
$found++; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->assertSame(1, $found); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
public function test_kernel() |
56
|
|
|
{ |
57
|
|
|
$kernel = new \TestKernel('dev', true); |
58
|
|
|
$kernel->boot(); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('prime')); |
61
|
|
|
$this->assertSame($kernel->getContainer()->get('prime'), $kernel->getContainer()->get(ServiceLocator::class)); |
62
|
|
|
$this->assertInstanceOf(MigrationManager::class, $kernel->getContainer()->get(MigrationManager::class)); |
63
|
|
|
$this->assertInstanceOf(SimpleConnection::class, $kernel->getContainer()->get(ServiceLocator::class)->connection('test')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
*/ |
69
|
|
|
public function test_collector() |
70
|
|
|
{ |
71
|
|
|
$kernel = new \TestKernel('dev', true); |
72
|
|
|
$kernel->boot(); |
73
|
|
|
|
74
|
|
|
$collector = $kernel->getContainer()->get(PrimeDataCollector::class); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf(PrimeDataCollector::class, $collector); |
77
|
|
|
$kernel->handle(Request::create('http://127.0.0.1/')); |
78
|
|
|
|
79
|
|
|
$isDoctrine2 = method_exists(Driver::class, 'getDatabase'); |
80
|
|
|
|
81
|
|
|
$this->assertEquals($isDoctrine2 ? 3 : 4, $collector->getQueryCount()); // Doctrine 3 always perform a select query on Connection::getDatabase() |
82
|
|
|
$this->assertEquals('SELECT * FROM test_ WHERE id = ? LIMIT 1', $collector->getQueries()[''][$isDoctrine2 ? 3 : 4]['sql']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function test_functional() |
89
|
|
|
{ |
90
|
|
|
$kernel = new \TestKernel('dev', true); |
91
|
|
|
$kernel->boot(); |
92
|
|
|
|
93
|
|
|
\TestEntity::repository()->schema()->migrate(); |
94
|
|
|
|
95
|
|
|
$entity = new \TestEntity(['name' => 'foo']); |
96
|
|
|
$entity->insert(); |
97
|
|
|
|
98
|
|
|
$this->assertEquals([$entity], \TestEntity::all()); |
|
|
|
|
99
|
|
|
$this->assertEquals($entity, \TestEntity::where('name', 'foo')->first()); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public function test_sharding_connection() |
106
|
|
|
{ |
107
|
|
|
$kernel = new class('test', true) extends Kernel { |
108
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
109
|
|
|
|
110
|
|
|
public function registerBundles(): iterable |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
new FrameworkBundle(), |
114
|
|
|
new PrimeBundle(), |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$loader->import(__DIR__.'/Fixtures/sharding.yaml'); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function configureRoutes(RouteCollectionBuilder $routes) { } |
|
|
|
|
124
|
|
|
}; |
125
|
|
|
|
126
|
|
|
$kernel->boot(); |
127
|
|
|
|
128
|
|
|
/** @var ServiceLocator $prime */ |
129
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
130
|
|
|
$this->assertInstanceOf(ShardingConnection::class, $prime->connection('test')); |
131
|
|
|
$this->assertInstanceOf(ShardingQuery::class, $prime->connection('test')->builder()); |
132
|
|
|
|
133
|
|
|
/** @var SimpleConnection $connection */ |
134
|
|
|
$connection = $prime->connection('test.shard1'); |
135
|
|
|
$this->assertSame($prime->connection('test')->getConfiguration(), $connection->getConfiguration()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
*/ |
141
|
|
|
public function test_array_cache() |
142
|
|
|
{ |
143
|
|
|
$kernel = new class('test', true) extends Kernel { |
144
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
145
|
|
|
|
146
|
|
|
public function registerBundles(): iterable |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
new FrameworkBundle(), |
150
|
|
|
new PrimeBundle(), |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$loader->import(__DIR__.'/Fixtures/array_cache.yaml'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
$kernel->boot(); |
163
|
|
|
|
164
|
|
|
/** @var ServiceLocator $prime */ |
165
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
166
|
|
|
$this->assertEquals(new ArrayCache(), $prime->mappers()->getResultCache()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
public function test_pool_cache() |
173
|
|
|
{ |
174
|
|
|
$kernel = new class('test', true) extends Kernel { |
175
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
176
|
|
|
|
177
|
|
|
public function registerBundles(): iterable |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
|
|
new FrameworkBundle(), |
181
|
|
|
new PrimeBundle(), |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
186
|
|
|
{ |
187
|
|
|
$c->register('custom.cache', FilesystemAdapter::class); |
188
|
|
|
|
189
|
|
|
$loader->import(__DIR__.'/Fixtures/pool_cache.yaml'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
193
|
|
|
}; |
194
|
|
|
|
195
|
|
|
$kernel->boot(); |
196
|
|
|
|
197
|
|
|
/** @var ServiceLocator $prime */ |
198
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
199
|
|
|
$this->assertEquals(new DoctrineCacheAdapter(DoctrineProvider::wrap(new FilesystemAdapter())), $prime->mappers()->getResultCache()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* |
204
|
|
|
*/ |
205
|
|
|
public function test_global_config() |
206
|
|
|
{ |
207
|
|
|
$kernel = new class('test', true) extends Kernel { |
208
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
209
|
|
|
|
210
|
|
|
public function registerBundles(): iterable |
211
|
|
|
{ |
212
|
|
|
return [ |
213
|
|
|
new FrameworkBundle(), |
214
|
|
|
new PrimeBundle(), |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$loader->import(__DIR__.'/Fixtures/config.yaml'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
224
|
|
|
}; |
225
|
|
|
|
226
|
|
|
$kernel->boot(); |
227
|
|
|
|
228
|
|
|
/** @var ServiceLocator $prime */ |
229
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
230
|
|
|
/** @var SimpleConnection $connection */ |
231
|
|
|
$connection = $prime->connection('test2'); |
232
|
|
|
|
233
|
|
|
$this->assertNotNull($connection->getConfiguration()->getSQLLogger()); |
234
|
|
|
$this->assertTrue($connection->getConfiguration()->getAutoCommit()); |
235
|
|
|
$this->assertInstanceOf(FooType::class, $connection->getConfiguration()->getTypes()->get('foo')); |
236
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar')); |
237
|
|
|
$this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array')); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* |
242
|
|
|
*/ |
243
|
|
|
public function test_connection_config() |
244
|
|
|
{ |
245
|
|
|
$kernel = new class('test', true) extends Kernel { |
246
|
|
|
use \Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
|
|
|
|
247
|
|
|
|
248
|
|
|
public function registerBundles(): iterable |
249
|
|
|
{ |
250
|
|
|
return [ |
251
|
|
|
new FrameworkBundle(), |
252
|
|
|
new PrimeBundle(), |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$loader->import(__DIR__.'/Fixtures/config.yaml'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
protected function configureRoutes($routes) { } |
|
|
|
|
262
|
|
|
}; |
263
|
|
|
|
264
|
|
|
$kernel->boot(); |
265
|
|
|
|
266
|
|
|
/** @var ServiceLocator $prime */ |
267
|
|
|
$prime = $kernel->getContainer()->get(ServiceLocator::class); |
268
|
|
|
/** @var SimpleConnection $connection */ |
269
|
|
|
$connection = $prime->connection('test'); |
270
|
|
|
|
271
|
|
|
$this->assertNull($connection->getConfiguration()->getSQLLogger()); |
272
|
|
|
$this->assertFalse($connection->getConfiguration()->getAutoCommit()); |
273
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('foo')); |
274
|
|
|
$this->assertInstanceOf(BarType::class, $connection->getConfiguration()->getTypes()->get('bar')); |
275
|
|
|
$this->assertInstanceOf(ArrayType::class, $connection->getConfiguration()->getTypes()->get('array')); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
class FooType |
280
|
|
|
{ |
281
|
|
|
|
282
|
|
|
} |
283
|
|
|
class BarType |
284
|
|
|
{ |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|