1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use Doctrine\Common\Cache\Cache; |
9
|
|
|
use Doctrine\Common\Proxy\AbstractProxyFactory; |
10
|
|
|
use Doctrine\ORM\Annotation as AnnotationNamespace; |
11
|
|
|
use Doctrine\ORM\Cache\CacheConfiguration; |
12
|
|
|
use Doctrine\ORM\Configuration; |
13
|
|
|
use Doctrine\ORM\EntityRepository; |
14
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
15
|
|
|
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver; |
16
|
|
|
use Doctrine\ORM\Mapping\Driver\MappingDriver; |
17
|
|
|
use Doctrine\ORM\Mapping\EntityListenerResolver; |
18
|
|
|
use Doctrine\ORM\Mapping\Factory\NamingStrategy; |
19
|
|
|
use Doctrine\ORM\ORMException; |
20
|
|
|
use Doctrine\ORM\Proxy\Factory\StaticProxyFactory; |
21
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
22
|
|
|
use Doctrine\Tests\DoctrineTestCase; |
23
|
|
|
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository; |
24
|
|
|
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; |
25
|
|
|
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; |
26
|
|
|
use ReflectionClass; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Tests for the Configuration object |
30
|
|
|
* @author Marco Pivetta <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class ConfigurationTest extends DoctrineTestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Configuration |
36
|
|
|
*/ |
37
|
|
|
private $configuration; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
parent::setUp(); |
42
|
|
|
$this->configuration = new Configuration(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testSetGetMetadataDriverImpl() |
46
|
|
|
{ |
47
|
|
|
self::assertNull($this->configuration->getMetadataDriverImpl()); // defaults |
48
|
|
|
|
49
|
|
|
$metadataDriver = $this->createMock(MappingDriver::class); |
50
|
|
|
$this->configuration->setMetadataDriverImpl($metadataDriver); |
51
|
|
|
self::assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testNewDefaultAnnotationDriver() |
55
|
|
|
{ |
56
|
|
|
$paths = [__DIR__]; |
57
|
|
|
$reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class); |
58
|
|
|
|
59
|
|
|
$annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths); |
60
|
|
|
$reader = $annotationDriver->getReader(); |
61
|
|
|
$annotation = $reader->getMethodAnnotation( |
62
|
|
|
$reflectionClass->getMethod('annotatedMethod'), |
63
|
|
|
AnnotationNamespace\PrePersist::class |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testSetGetQueryCacheImpl() |
70
|
|
|
{ |
71
|
|
|
self::assertNull($this->configuration->getQueryCacheImpl()); // defaults |
72
|
|
|
$queryCacheImpl = $this->createMock(Cache::class); |
73
|
|
|
$this->configuration->setQueryCacheImpl($queryCacheImpl); |
74
|
|
|
self::assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testSetGetHydrationCacheImpl() |
78
|
|
|
{ |
79
|
|
|
self::assertNull($this->configuration->getHydrationCacheImpl()); // defaults |
80
|
|
|
$queryCacheImpl = $this->createMock(Cache::class); |
81
|
|
|
$this->configuration->setHydrationCacheImpl($queryCacheImpl); |
82
|
|
|
self::assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testSetGetMetadataCacheImpl() |
86
|
|
|
{ |
87
|
|
|
self::assertNull($this->configuration->getMetadataCacheImpl()); // defaults |
88
|
|
|
$queryCacheImpl = $this->createMock(Cache::class); |
89
|
|
|
$this->configuration->setMetadataCacheImpl($queryCacheImpl); |
90
|
|
|
self::assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testAddGetNamedQuery() |
94
|
|
|
{ |
95
|
|
|
$dql = 'SELECT u FROM User u'; |
96
|
|
|
$this->configuration->addNamedQuery('QueryName', $dql); |
97
|
|
|
self::assertSame($dql, $this->configuration->getNamedQuery('QueryName')); |
98
|
|
|
$this->expectException(ORMException::class); |
99
|
|
|
$this->expectExceptionMessage('a named query'); |
100
|
|
|
$this->configuration->getNamedQuery('NonExistingQuery'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testAddGetNamedNativeQuery() |
104
|
|
|
{ |
105
|
|
|
$sql = 'SELECT * FROM user'; |
106
|
|
|
$rsm = $this->createMock(ResultSetMapping::class); |
107
|
|
|
$this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm); |
108
|
|
|
$fetched = $this->configuration->getNamedNativeQuery('QueryName'); |
109
|
|
|
self::assertSame($sql, $fetched[0]); |
110
|
|
|
self::assertSame($rsm, $fetched[1]); |
111
|
|
|
$this->expectException(ORMException::class); |
112
|
|
|
$this->expectExceptionMessage('a named native query'); |
113
|
|
|
$this->configuration->getNamedNativeQuery('NonExistingQuery'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Configures $this->configuration to use production settings. |
118
|
|
|
* |
119
|
|
|
* @param string $skipCache Do not configure a cache of this type, either "query" or "metadata". |
120
|
|
|
*/ |
121
|
|
|
protected function setProductionSettings($skipCache = false) |
122
|
|
|
{ |
123
|
|
|
$this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); |
124
|
|
|
|
125
|
|
|
$cache = $this->createMock(Cache::class); |
126
|
|
|
|
127
|
|
|
if ('query' !== $skipCache) { |
128
|
|
|
$this->configuration->setQueryCacheImpl($cache); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ('metadata' !== $skipCache) { |
132
|
|
|
$this->configuration->setMetadataCacheImpl($cache); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testEnsureProductionSettings() |
137
|
|
|
{ |
138
|
|
|
$this->setProductionSettings(); |
139
|
|
|
$this->configuration->ensureProductionSettings(); |
140
|
|
|
|
141
|
|
|
self::addToAssertionCount(1); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testEnsureProductionSettingsQueryCache() |
145
|
|
|
{ |
146
|
|
|
$this->setProductionSettings('query'); |
147
|
|
|
|
148
|
|
|
$this->expectException(ORMException::class); |
149
|
|
|
$this->expectExceptionMessage('Query Cache is not configured.'); |
150
|
|
|
|
151
|
|
|
$this->configuration->ensureProductionSettings(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testEnsureProductionSettingsMetadataCache() |
155
|
|
|
{ |
156
|
|
|
$this->setProductionSettings('metadata'); |
157
|
|
|
|
158
|
|
|
$this->expectException(ORMException::class); |
159
|
|
|
$this->expectExceptionMessage('Metadata Cache is not configured.'); |
160
|
|
|
|
161
|
|
|
$this->configuration->ensureProductionSettings(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testEnsureProductionSettingsQueryArrayCache() |
165
|
|
|
{ |
166
|
|
|
$this->setProductionSettings(); |
167
|
|
|
$this->configuration->setQueryCacheImpl(new ArrayCache()); |
168
|
|
|
|
169
|
|
|
$this->expectException(ORMException::class); |
170
|
|
|
$this->expectExceptionMessage('Query Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.'); |
171
|
|
|
|
172
|
|
|
$this->configuration->ensureProductionSettings(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testEnsureProductionSettingsMetadataArrayCache() |
176
|
|
|
{ |
177
|
|
|
$this->setProductionSettings(); |
178
|
|
|
$this->configuration->setMetadataCacheImpl(new ArrayCache()); |
179
|
|
|
|
180
|
|
|
$this->expectException(ORMException::class); |
181
|
|
|
$this->expectExceptionMessage('Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.'); |
182
|
|
|
|
183
|
|
|
$this->configuration->ensureProductionSettings(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testEnsureProductionSettingsAutoGenerateProxyClassesEval() |
187
|
|
|
{ |
188
|
|
|
$this->setProductionSettings(); |
189
|
|
|
$this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_EVAL); |
190
|
|
|
|
191
|
|
|
$this->expectException(ORMException::class); |
192
|
|
|
$this->expectExceptionMessage('Proxy Classes are always regenerating.'); |
193
|
|
|
|
194
|
|
|
$this->configuration->ensureProductionSettings(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testAddGetCustomStringFunction() |
198
|
|
|
{ |
199
|
|
|
$this->configuration->addCustomStringFunction('FunctionName', __CLASS__); |
200
|
|
|
|
201
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('FunctionName')); |
202
|
|
|
self::assertNull($this->configuration->getCustomStringFunction('NonExistingFunction')); |
203
|
|
|
|
204
|
|
|
$this->configuration->setCustomStringFunctions(['OtherFunctionName' => __CLASS__]); |
205
|
|
|
|
206
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('OtherFunctionName')); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testAddGetCustomNumericFunction() |
210
|
|
|
{ |
211
|
|
|
$this->configuration->addCustomNumericFunction('FunctionName', __CLASS__); |
212
|
|
|
|
213
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('FunctionName')); |
214
|
|
|
self::assertNull($this->configuration->getCustomNumericFunction('NonExistingFunction')); |
215
|
|
|
|
216
|
|
|
$this->configuration->setCustomNumericFunctions(['OtherFunctionName' => __CLASS__]); |
217
|
|
|
|
218
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('OtherFunctionName')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testAddGetCustomDatetimeFunction() |
222
|
|
|
{ |
223
|
|
|
$this->configuration->addCustomDatetimeFunction('FunctionName', __CLASS__); |
224
|
|
|
|
225
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('FunctionName')); |
226
|
|
|
self::assertNull($this->configuration->getCustomDatetimeFunction('NonExistingFunction')); |
227
|
|
|
|
228
|
|
|
$this->configuration->setCustomDatetimeFunctions(['OtherFunctionName' => __CLASS__]); |
229
|
|
|
|
230
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('OtherFunctionName')); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testAddGetCustomHydrationMode() |
234
|
|
|
{ |
235
|
|
|
self::assertNull($this->configuration->getCustomHydrationMode('NonExisting')); |
236
|
|
|
|
237
|
|
|
$this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__); |
238
|
|
|
|
239
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName')); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testSetCustomHydrationModes() |
243
|
|
|
{ |
244
|
|
|
$this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__); |
245
|
|
|
|
246
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName')); |
247
|
|
|
|
248
|
|
|
$this->configuration->setCustomHydrationModes( |
249
|
|
|
[ |
250
|
|
|
'AnotherHydrationModeName' => __CLASS__ |
251
|
|
|
] |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
self::assertNull($this->configuration->getCustomHydrationMode('HydrationModeName')); |
255
|
|
|
self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName')); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testSetGetClassMetadataFactoryName() |
259
|
|
|
{ |
260
|
|
|
self::assertSame(ClassMetadataFactory::class, $this->configuration->getClassMetadataFactoryName()); |
261
|
|
|
|
262
|
|
|
$this->configuration->setClassMetadataFactoryName(__CLASS__); |
263
|
|
|
|
264
|
|
|
self::assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName()); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function testAddGetFilters() |
268
|
|
|
{ |
269
|
|
|
self::assertNull($this->configuration->getFilterClassName('NonExistingFilter')); |
270
|
|
|
|
271
|
|
|
$this->configuration->addFilter('FilterName', __CLASS__); |
272
|
|
|
|
273
|
|
|
self::assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName')); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function setDefaultRepositoryClassName() |
277
|
|
|
{ |
278
|
|
|
self::assertSame(EntityRepository::class, $this->configuration->getDefaultRepositoryClassName()); |
279
|
|
|
|
280
|
|
|
$this->configuration->setDefaultRepositoryClassName(DDC753CustomRepository::class); |
281
|
|
|
|
282
|
|
|
self::assertSame(DDC753CustomRepository::class, $this->configuration->getDefaultRepositoryClassName()); |
283
|
|
|
|
284
|
|
|
$this->expectException(ORMException::class); |
285
|
|
|
$this->configuration->setDefaultRepositoryClassName(__CLASS__); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testSetGetNamingStrategy() |
289
|
|
|
{ |
290
|
|
|
self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy()); |
291
|
|
|
|
292
|
|
|
$namingStrategy = $this->createMock(NamingStrategy::class); |
293
|
|
|
|
294
|
|
|
$this->configuration->setNamingStrategy($namingStrategy); |
295
|
|
|
|
296
|
|
|
self::assertSame($namingStrategy, $this->configuration->getNamingStrategy()); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @group DDC-1955 |
301
|
|
|
*/ |
302
|
|
|
public function testSetGetEntityListenerResolver() |
303
|
|
|
{ |
304
|
|
|
self::assertInstanceOf(EntityListenerResolver::class, $this->configuration->getEntityListenerResolver()); |
305
|
|
|
self::assertInstanceOf(DefaultEntityListenerResolver::class, $this->configuration->getEntityListenerResolver()); |
306
|
|
|
|
307
|
|
|
$resolver = $this->createMock(EntityListenerResolver::class); |
308
|
|
|
|
309
|
|
|
$this->configuration->setEntityListenerResolver($resolver); |
310
|
|
|
|
311
|
|
|
self::assertSame($resolver, $this->configuration->getEntityListenerResolver()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @group DDC-2183 |
316
|
|
|
*/ |
317
|
|
|
public function testSetGetSecondLevelCacheConfig() |
318
|
|
|
{ |
319
|
|
|
$mockClass = $this->createMock(CacheConfiguration::class); |
320
|
|
|
|
321
|
|
|
self::assertNull($this->configuration->getSecondLevelCacheConfiguration()); |
322
|
|
|
$this->configuration->setSecondLevelCacheConfiguration($mockClass); |
323
|
|
|
self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration()); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function testGetProxyManagerConfiguration() : void |
327
|
|
|
{ |
328
|
|
|
self::assertInstanceOf( |
329
|
|
|
\ProxyManager\Configuration::class, |
330
|
|
|
$this->configuration->getProxyManagerConfiguration() |
331
|
|
|
); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function testProxyManagerConfigurationContainsGivenProxyTargetDir() : void |
335
|
|
|
{ |
336
|
|
|
$proxyPath = $this->makeTemporaryValidDirectory(); |
337
|
|
|
|
338
|
|
|
$this->configuration->setProxyDir($proxyPath); |
339
|
|
|
self::assertSame($proxyPath, $this->configuration->getProxyManagerConfiguration()->getProxiesTargetDir()); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
public function testProxyManagerConfigurationContainsGivenProxyNamespace() : void |
343
|
|
|
{ |
344
|
|
|
$namespace = \str_replace('.', '', \uniqid('Namespace', true)); |
345
|
|
|
|
346
|
|
|
$this->configuration->setProxyNamespace($namespace); |
347
|
|
|
self::assertSame($namespace, $this->configuration->getProxyManagerConfiguration()->getProxiesNamespace()); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @dataProvider expectedGeneratorStrategies |
352
|
|
|
* |
353
|
|
|
* @param int|bool $proxyAutoGenerateFlag |
354
|
|
|
*/ |
355
|
|
|
public function testProxyManagerConfigurationWillBeUpdatedWithCorrectGeneratorStrategies( |
356
|
|
|
$proxyAutoGenerateFlag, |
357
|
|
|
string $expectedGeneratorStrategy |
358
|
|
|
) : void { |
359
|
|
|
$this->configuration->setAutoGenerateProxyClasses($proxyAutoGenerateFlag); |
360
|
|
|
|
361
|
|
|
self::assertInstanceOf( |
362
|
|
|
$expectedGeneratorStrategy, |
363
|
|
|
$this->configuration->getProxyManagerConfiguration()->getGeneratorStrategy() |
364
|
|
|
); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function expectedGeneratorStrategies() : array |
368
|
|
|
{ |
369
|
|
|
return [ |
370
|
|
|
[ |
371
|
|
|
StaticProxyFactory::AUTOGENERATE_NEVER, |
372
|
|
|
EvaluatingGeneratorStrategy::class, |
373
|
|
|
], |
374
|
|
|
[ |
375
|
|
|
StaticProxyFactory::AUTOGENERATE_EVAL, |
376
|
|
|
EvaluatingGeneratorStrategy::class, |
377
|
|
|
], |
378
|
|
|
[ |
379
|
|
|
false, |
380
|
|
|
EvaluatingGeneratorStrategy::class, |
381
|
|
|
], |
382
|
|
|
[ |
383
|
|
|
StaticProxyFactory::AUTOGENERATE_ALWAYS, |
384
|
|
|
FileWriterGeneratorStrategy::class, |
385
|
|
|
], |
386
|
|
|
[ |
387
|
|
|
StaticProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS, |
388
|
|
|
FileWriterGeneratorStrategy::class, |
389
|
|
|
], |
390
|
|
|
[ |
391
|
|
|
true, |
392
|
|
|
FileWriterGeneratorStrategy::class, |
393
|
|
|
], |
394
|
|
|
]; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
private function makeTemporaryValidDirectory() : string |
398
|
|
|
{ |
399
|
|
|
$path = \tempnam(\sys_get_temp_dir(), 'ProxyConfigurationTest'); |
400
|
|
|
|
401
|
|
|
unlink($path); |
402
|
|
|
mkdir($path); |
403
|
|
|
|
404
|
|
|
return $path; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testWillProduceGhostObjectFactory() : void |
408
|
|
|
{ |
409
|
|
|
$factory1 = $this->configuration->buildGhostObjectFactory(); |
410
|
|
|
$factory2 = $this->configuration->buildGhostObjectFactory(); |
411
|
|
|
|
412
|
|
|
$this->configuration->setProxyDir($this->makeTemporaryValidDirectory()); |
413
|
|
|
|
414
|
|
|
$factory3 = $this->configuration->buildGhostObjectFactory(); |
415
|
|
|
|
416
|
|
|
self::assertNotSame($factory1, $factory2); |
417
|
|
|
self::assertEquals($factory1, $factory2); |
418
|
|
|
self::assertNotEquals($factory2, $factory3); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
class ConfigurationTestAnnotationReaderChecker |
423
|
|
|
{ |
424
|
|
|
/** @AnnotationNamespace\PrePersist */ |
425
|
|
|
public function annotatedMethod() |
426
|
|
|
{ |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|