1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Service; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use DoctrineORMModule\Service\ConfigurationFactory; |
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use Laminas\ServiceManager\ServiceManager; |
9
|
|
|
|
10
|
|
|
class ConfigurationFactoryTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ServiceManager |
14
|
|
|
*/ |
15
|
|
|
protected $serviceManager; |
16
|
|
|
/** |
17
|
|
|
* @var ConfigurationFactory |
18
|
|
|
*/ |
19
|
|
|
protected $factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
|
|
public function setUp() : void |
25
|
|
|
{ |
26
|
|
|
$this->serviceManager = new ServiceManager(); |
27
|
|
|
$this->factory = new ConfigurationFactory('test_default'); |
28
|
|
|
$this->serviceManager->setService('doctrine.cache.array', new ArrayCache()); |
29
|
|
|
$this->serviceManager->setService( |
30
|
|
|
'doctrine.driver.orm_default', |
31
|
|
|
$this->createMock(\Doctrine\Common\Persistence\Mapping\Driver\MappingDriver::class) |
|
|
|
|
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testWillInstantiateConfigWithoutNamingStrategySetting() |
36
|
|
|
{ |
37
|
|
|
$config = [ |
38
|
|
|
'doctrine' => [ |
39
|
|
|
'configuration' => [ |
40
|
|
|
'test_default' => [], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
$this->serviceManager->setService('config', $config); |
45
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
46
|
|
|
$this->assertInstanceOf(\Doctrine\ORM\Mapping\NamingStrategy::class, $ormConfig->getNamingStrategy()); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testWillInstantiateConfigWithNamingStrategyObject() |
50
|
|
|
{ |
51
|
|
|
$namingStrategy = $this->createMock(\Doctrine\ORM\Mapping\NamingStrategy::class); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$config = [ |
54
|
|
|
'doctrine' => [ |
55
|
|
|
'configuration' => [ |
56
|
|
|
'test_default' => [ |
57
|
|
|
'naming_strategy' => $namingStrategy, |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
$this->serviceManager->setService('config', $config); |
63
|
|
|
$factory = new ConfigurationFactory('test_default'); |
64
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
65
|
|
|
$this->assertSame($namingStrategy, $ormConfig->getNamingStrategy()); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testWillInstantiateConfigWithNamingStrategyReference() |
69
|
|
|
{ |
70
|
|
|
$namingStrategy = $this->createMock(\Doctrine\ORM\Mapping\NamingStrategy::class); |
|
|
|
|
71
|
|
|
$config = [ |
72
|
|
|
'doctrine' => [ |
73
|
|
|
'configuration' => [ |
74
|
|
|
'test_default' => [ |
75
|
|
|
'naming_strategy' => 'test_naming_strategy', |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
], |
79
|
|
|
]; |
80
|
|
|
$this->serviceManager->setService('config', $config); |
81
|
|
|
$this->serviceManager->setService('test_naming_strategy', $namingStrategy); |
|
|
|
|
82
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
83
|
|
|
$this->assertSame($namingStrategy, $ormConfig->getNamingStrategy()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testWillNotInstantiateConfigWithInvalidNamingStrategyReference() |
87
|
|
|
{ |
88
|
|
|
$config = [ |
89
|
|
|
'doctrine' => [ |
90
|
|
|
'configuration' => [ |
91
|
|
|
'test_default' => [ |
92
|
|
|
'naming_strategy' => 'test_naming_strategy', |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
], |
96
|
|
|
]; |
97
|
|
|
$this->serviceManager->setService('config', $config); |
98
|
|
|
$this->expectException(\Laminas\ServiceManager\Exception\InvalidArgumentException::class); |
|
|
|
|
99
|
|
|
$this->factory->createService($this->serviceManager); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testWillInstantiateConfigWithQuoteStrategyObject() |
103
|
|
|
{ |
104
|
|
|
$quoteStrategy = $this->createMock(\Doctrine\ORM\Mapping\QuoteStrategy::class); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$config = [ |
107
|
|
|
'doctrine' => [ |
108
|
|
|
'configuration' => [ |
109
|
|
|
'test_default' => [ |
110
|
|
|
'quote_strategy' => $quoteStrategy, |
111
|
|
|
], |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
$this->serviceManager->setService('config', $config); |
116
|
|
|
$factory = new ConfigurationFactory('test_default'); |
117
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
118
|
|
|
$this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy()); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testWillInstantiateConfigWithQuoteStrategyReference() |
122
|
|
|
{ |
123
|
|
|
$quoteStrategy = $this->createMock(\Doctrine\ORM\Mapping\QuoteStrategy::class); |
|
|
|
|
124
|
|
|
$config = [ |
125
|
|
|
'doctrine' => [ |
126
|
|
|
'configuration' => [ |
127
|
|
|
'test_default' => [ |
128
|
|
|
'quote_strategy' => 'test_quote_strategy', |
129
|
|
|
], |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
$this->serviceManager->setService('config', $config); |
134
|
|
|
$this->serviceManager->setService('test_quote_strategy', $quoteStrategy); |
|
|
|
|
135
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
136
|
|
|
$this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy()); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testWillNotInstantiateConfigWithInvalidQuoteStrategyReference() |
140
|
|
|
{ |
141
|
|
|
$config = [ |
142
|
|
|
'doctrine' => [ |
143
|
|
|
'configuration' => [ |
144
|
|
|
'test_default' => [ |
145
|
|
|
'quote_strategy' => 'test_quote_strategy', |
146
|
|
|
], |
147
|
|
|
], |
148
|
|
|
], |
149
|
|
|
]; |
150
|
|
|
$this->serviceManager->setService('config', $config); |
151
|
|
|
$this->expectException(\Laminas\ServiceManager\Exception\InvalidArgumentException::class); |
|
|
|
|
152
|
|
|
$this->factory->createService($this->serviceManager); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testWillInstantiateConfigWithHydrationCacheSetting() |
156
|
|
|
{ |
157
|
|
|
$config = [ |
158
|
|
|
'doctrine' => [ |
159
|
|
|
'configuration' => [ |
160
|
|
|
'test_default' => [ |
161
|
|
|
'hydration_cache' => 'array', |
162
|
|
|
], |
163
|
|
|
], |
164
|
|
|
], |
165
|
|
|
]; |
166
|
|
|
$this->serviceManager->setService('config', $config); |
167
|
|
|
$factory = new ConfigurationFactory('test_default'); |
168
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
169
|
|
|
$this->assertInstanceOf(\Doctrine\Common\Cache\ArrayCache::class, $ormConfig->getHydrationCacheImpl()); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testCanSetDefaultRepositoryClass() |
173
|
|
|
{ |
174
|
|
|
$config = [ |
175
|
|
|
'doctrine' => [ |
176
|
|
|
'configuration' => [ |
177
|
|
|
'test_default' => [ |
178
|
|
|
|
179
|
|
|
'hydration_cache' => 'array', |
180
|
|
|
'default_repository_class_name' => \DoctrineORMModuleTest\Assets\RepositoryClass::class, |
181
|
|
|
], |
182
|
|
|
], |
183
|
|
|
], |
184
|
|
|
]; |
185
|
|
|
$this->serviceManager->setService('config', $config); |
186
|
|
|
|
187
|
|
|
$factory = new ConfigurationFactory('test_default'); |
188
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
189
|
|
|
$this->assertInstanceOf(\Doctrine\Common\Cache\ArrayCache::class, $ormConfig->getHydrationCacheImpl()); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testAcceptsMetadataFactory() |
193
|
|
|
{ |
194
|
|
|
$config = [ |
195
|
|
|
'doctrine' => [ |
196
|
|
|
'configuration' => [ |
197
|
|
|
'test_default' => [ |
198
|
|
|
'classMetadataFactoryName' => 'Factory', |
199
|
|
|
], |
200
|
|
|
], |
201
|
|
|
], |
202
|
|
|
]; |
203
|
|
|
$this->serviceManager->setService('config', $config); |
204
|
|
|
$factory = new ConfigurationFactory('test_default'); |
205
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
206
|
|
|
$this->assertEquals('Factory', $ormConfig->getClassMetadataFactoryName()); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testDefaultMetadatFactory() |
210
|
|
|
{ |
211
|
|
|
$config = [ |
212
|
|
|
'doctrine' => [ |
213
|
|
|
'configuration' => [ |
214
|
|
|
'test_default' => [ |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
], |
218
|
|
|
]; |
219
|
|
|
$this->serviceManager->setService('config', $config); |
220
|
|
|
$factory = new ConfigurationFactory('test_default'); |
221
|
|
|
$ormConfig = $factory->createService($this->serviceManager); |
222
|
|
|
$this->assertEquals( |
|
|
|
|
223
|
|
|
\Doctrine\ORM\Mapping\ClassMetadataFactory::class, |
224
|
|
|
$ormConfig->getClassMetadataFactoryName() |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testWillInstantiateConfigWithoutEntityListenerResolverSetting() |
229
|
|
|
{ |
230
|
|
|
$config = [ |
231
|
|
|
'doctrine' => [ |
232
|
|
|
'configuration' => [ |
233
|
|
|
'test_default' => [], |
234
|
|
|
], |
235
|
|
|
], |
236
|
|
|
]; |
237
|
|
|
|
238
|
|
|
$this->serviceManager->setService('config', $config); |
239
|
|
|
|
240
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
241
|
|
|
|
242
|
|
|
$this->assertInstanceOf( |
|
|
|
|
243
|
|
|
\Doctrine\ORM\Mapping\EntityListenerResolver::class, |
244
|
|
|
$ormConfig->getEntityListenerResolver() |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testWillInstantiateConfigWithEntityListenerResolverObject() |
249
|
|
|
{ |
250
|
|
|
$entityListenerResolver = $this->createMock(\Doctrine\ORM\Mapping\EntityListenerResolver::class); |
|
|
|
|
251
|
|
|
|
252
|
|
|
$config = [ |
253
|
|
|
'doctrine' => [ |
254
|
|
|
'configuration' => [ |
255
|
|
|
'test_default' => [ |
256
|
|
|
'entity_listener_resolver' => $entityListenerResolver, |
257
|
|
|
], |
258
|
|
|
], |
259
|
|
|
], |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
$this->serviceManager->setService('config', $config); |
263
|
|
|
|
264
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
265
|
|
|
|
266
|
|
|
$this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver()); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testWillInstantiateConfigWithEntityListenerResolverReference() |
270
|
|
|
{ |
271
|
|
|
$entityListenerResolver = $this->createMock(\Doctrine\ORM\Mapping\EntityListenerResolver::class); |
|
|
|
|
272
|
|
|
|
273
|
|
|
$config = [ |
274
|
|
|
'doctrine' => [ |
275
|
|
|
'configuration' => [ |
276
|
|
|
'test_default' => [ |
277
|
|
|
'entity_listener_resolver' => 'test_entity_listener_resolver', |
278
|
|
|
], |
279
|
|
|
], |
280
|
|
|
], |
281
|
|
|
]; |
282
|
|
|
|
283
|
|
|
$this->serviceManager->setService('config', $config); |
284
|
|
|
$this->serviceManager->setService('test_entity_listener_resolver', $entityListenerResolver); |
|
|
|
|
285
|
|
|
|
286
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
287
|
|
|
|
288
|
|
|
$this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver()); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function testDoNotCreateSecondLevelCacheByDefault() |
292
|
|
|
{ |
293
|
|
|
$config = [ |
294
|
|
|
'doctrine' => [ |
295
|
|
|
'configuration' => [ |
296
|
|
|
'test_default' => [], |
297
|
|
|
], |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
|
301
|
|
|
$this->serviceManager->setService('config', $config); |
302
|
|
|
|
303
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
304
|
|
|
|
305
|
|
|
$this->assertNull($ormConfig->getSecondLevelCacheConfiguration()); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testCanInstantiateWithSecondLevelCacheConfig() |
309
|
|
|
{ |
310
|
|
|
$config = [ |
311
|
|
|
'doctrine' => [ |
312
|
|
|
'configuration' => [ |
313
|
|
|
'test_default' => [ |
314
|
|
|
'result_cache' => 'array', |
315
|
|
|
|
316
|
|
|
'second_level_cache' => [ |
317
|
|
|
'enabled' => true, |
318
|
|
|
'default_lifetime' => 200, |
319
|
|
|
'default_lock_lifetime' => 500, |
320
|
|
|
'file_lock_region_directory' => 'my_dir', |
321
|
|
|
|
322
|
|
|
'regions' => [ |
323
|
|
|
'my_first_region' => [ |
324
|
|
|
'lifetime' => 800, |
325
|
|
|
'lock_lifetime' => 1000, |
326
|
|
|
], |
327
|
|
|
|
328
|
|
|
'my_second_region' => [ |
329
|
|
|
'lifetime' => 10, |
330
|
|
|
'lock_lifetime' => 20, |
331
|
|
|
], |
332
|
|
|
], |
333
|
|
|
], |
334
|
|
|
], |
335
|
|
|
], |
336
|
|
|
], |
337
|
|
|
]; |
338
|
|
|
|
339
|
|
|
$this->serviceManager->setService('config', $config); |
340
|
|
|
|
341
|
|
|
$ormConfig = $this->factory->createService($this->serviceManager); |
342
|
|
|
$secondLevelCache = $ormConfig->getSecondLevelCacheConfiguration(); |
343
|
|
|
|
344
|
|
|
$this->assertInstanceOf(\Doctrine\ORM\Cache\CacheConfiguration::class, $secondLevelCache); |
|
|
|
|
345
|
|
|
|
346
|
|
|
$cacheFactory = $secondLevelCache->getCacheFactory(); |
347
|
|
|
$this->assertInstanceOf(\Doctrine\ORM\Cache\DefaultCacheFactory::class, $cacheFactory); |
|
|
|
|
348
|
|
|
$this->assertEquals('my_dir', $cacheFactory->getFileLockRegionDirectory()); |
|
|
|
|
349
|
|
|
|
350
|
|
|
$regionsConfiguration = $secondLevelCache->getRegionsConfiguration(); |
351
|
|
|
$this->assertEquals(200, $regionsConfiguration->getDefaultLifetime()); |
|
|
|
|
352
|
|
|
$this->assertEquals(500, $regionsConfiguration->getDefaultLockLifetime()); |
|
|
|
|
353
|
|
|
|
354
|
|
|
$this->assertEquals(800, $regionsConfiguration->getLifetime('my_first_region')); |
|
|
|
|
355
|
|
|
$this->assertEquals(10, $regionsConfiguration->getLifetime('my_second_region')); |
|
|
|
|
356
|
|
|
|
357
|
|
|
$this->assertEquals(1000, $regionsConfiguration->getLockLifetime('my_first_region')); |
|
|
|
|
358
|
|
|
$this->assertEquals(20, $regionsConfiguration->getLockLifetime('my_second_region')); |
|
|
|
|
359
|
|
|
|
360
|
|
|
// Doctrine does not allow to retrieve the cache adapter from cache factory, so we are forced to use |
361
|
|
|
// reflection here |
362
|
|
|
$reflProperty = new \ReflectionProperty($cacheFactory, 'cache'); |
363
|
|
|
$reflProperty->setAccessible(true); |
364
|
|
|
$this->assertInstanceOf(\Doctrine\Common\Cache\ArrayCache::class, $reflProperty->getValue($cacheFactory)); |
|
|
|
|
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: