Failed Conditions
Pull Request — develop (#6719)
by Marco
65:21
created

ConfigurationTest::testSetGetMetadataDriverImpl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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 testSetGetProxyDir()
46
    {
47
        self::assertSame(null, $this->configuration->getProxyDir()); // defaults
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configuration::getProxyDir() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48
49
        $this->configuration->setProxyDir(__DIR__);
50
        self::assertSame(__DIR__, $this->configuration->getProxyDir());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configuration::getProxyDir() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
    }
52
53
    public function testSetGetAutoGenerateProxyClasses()
54
    {
55
        self::assertSame(AbstractProxyFactory::AUTOGENERATE_ALWAYS, $this->configuration->getAutoGenerateProxyClasses()); // defaults
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configurati...oGenerateProxyClasses() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
57
        $this->configuration->setAutoGenerateProxyClasses(false);
58
        self::assertSame(AbstractProxyFactory::AUTOGENERATE_NEVER, $this->configuration->getAutoGenerateProxyClasses());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configurati...oGenerateProxyClasses() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
60
        $this->configuration->setAutoGenerateProxyClasses(true);
61
        self::assertSame(AbstractProxyFactory::AUTOGENERATE_ALWAYS, $this->configuration->getAutoGenerateProxyClasses());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configurati...oGenerateProxyClasses() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
63
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
64
        self::assertSame(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS, $this->configuration->getAutoGenerateProxyClasses());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configurati...oGenerateProxyClasses() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
    }
66
67
    public function testSetGetProxyNamespace()
68
    {
69
        self::assertSame(null, $this->configuration->getProxyNamespace()); // defaults
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configuration::getProxyNamespace() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
70
71
        $this->configuration->setProxyNamespace(__NAMESPACE__);
72
        self::assertSame(__NAMESPACE__, $this->configuration->getProxyNamespace());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\Configuration::getProxyNamespace() has been deprecated with message: please do not use this anymore

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
73
    }
74
75
    public function testSetGetMetadataDriverImpl()
76
    {
77
        self::assertSame(null, $this->configuration->getMetadataDriverImpl()); // defaults
78
79
        $metadataDriver = $this->createMock(MappingDriver::class);
80
        $this->configuration->setMetadataDriverImpl($metadataDriver);
81
        self::assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
82
    }
83
84
    public function testNewDefaultAnnotationDriver()
85
    {
86
        $paths = [__DIR__];
87
        $reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class);
88
89
        $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
90
        $reader = $annotationDriver->getReader();
91
        $annotation = $reader->getMethodAnnotation(
92
            $reflectionClass->getMethod('annotatedMethod'),
93
            AnnotationNamespace\PrePersist::class
94
        );
95
96
        self::assertInstanceOf(AnnotationNamespace\PrePersist::class, $annotation);
97
    }
98
99
    public function testSetGetEntityNamespace()
100
    {
101
        $this->configuration->addEntityNamespace('TestNamespace', __NAMESPACE__);
102
        self::assertSame(__NAMESPACE__, $this->configuration->getEntityNamespace('TestNamespace'));
103
        $namespaces = ['OtherNamespace' => __NAMESPACE__];
104
        $this->configuration->setEntityNamespaces($namespaces);
105
        self::assertSame($namespaces, $this->configuration->getEntityNamespaces());
106
        $this->expectException(ORMException::class);
107
        $this->configuration->getEntityNamespace('NonExistingNamespace');
108
    }
109
110 View Code Duplication
    public function testSetGetQueryCacheImpl()
111
    {
112
        self::assertSame(null, $this->configuration->getQueryCacheImpl()); // defaults
113
        $queryCacheImpl = $this->createMock(Cache::class);
114
        $this->configuration->setQueryCacheImpl($queryCacheImpl);
115
        self::assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
116
    }
117
118 View Code Duplication
    public function testSetGetHydrationCacheImpl()
119
    {
120
        self::assertSame(null, $this->configuration->getHydrationCacheImpl()); // defaults
121
        $queryCacheImpl = $this->createMock(Cache::class);
122
        $this->configuration->setHydrationCacheImpl($queryCacheImpl);
123
        self::assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl());
124
    }
125
126 View Code Duplication
    public function testSetGetMetadataCacheImpl()
127
    {
128
        self::assertSame(null, $this->configuration->getMetadataCacheImpl()); // defaults
129
        $queryCacheImpl = $this->createMock(Cache::class);
130
        $this->configuration->setMetadataCacheImpl($queryCacheImpl);
131
        self::assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
132
    }
133
134
    public function testAddGetNamedQuery()
135
    {
136
        $dql = 'SELECT u FROM User u';
137
        $this->configuration->addNamedQuery('QueryName', $dql);
138
        self::assertSame($dql, $this->configuration->getNamedQuery('QueryName'));
139
        $this->expectException(ORMException::class);
140
        $this->expectExceptionMessage('a named query');
141
        $this->configuration->getNamedQuery('NonExistingQuery');
142
    }
143
144
    public function testAddGetNamedNativeQuery()
145
    {
146
        $sql = 'SELECT * FROM user';
147
        $rsm = $this->createMock(ResultSetMapping::class);
148
        $this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm);
149
        $fetched = $this->configuration->getNamedNativeQuery('QueryName');
150
        self::assertSame($sql, $fetched[0]);
151
        self::assertSame($rsm, $fetched[1]);
152
        $this->expectException(ORMException::class);
153
        $this->expectExceptionMessage('a named native query');
154
        $this->configuration->getNamedNativeQuery('NonExistingQuery');
155
    }
156
157
    /**
158
     * Configures $this->configuration to use production settings.
159
     *
160
     * @param string $skipCache Do not configure a cache of this type, either "query" or "metadata".
161
     */
162
    protected function setProductionSettings($skipCache = false)
163
    {
164
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_NEVER);
165
166
        $cache = $this->createMock(Cache::class);
167
168
        if ('query' !== $skipCache) {
169
            $this->configuration->setQueryCacheImpl($cache);
170
        }
171
172
        if ('metadata' !== $skipCache) {
173
            $this->configuration->setMetadataCacheImpl($cache);
174
        }
175
    }
176
177
    public function testEnsureProductionSettings()
178
    {
179
        $this->setProductionSettings();
180
        $this->configuration->ensureProductionSettings();
181
182
        self::addToAssertionCount(1);
183
    }
184
185
    public function testEnsureProductionSettingsQueryCache()
186
    {
187
        $this->setProductionSettings('query');
188
189
        $this->expectException(ORMException::class);
190
        $this->expectExceptionMessage('Query Cache is not configured.');
191
192
        $this->configuration->ensureProductionSettings();
193
    }
194
195
    public function testEnsureProductionSettingsMetadataCache()
196
    {
197
        $this->setProductionSettings('metadata');
198
199
        $this->expectException(ORMException::class);
200
        $this->expectExceptionMessage('Metadata Cache is not configured.');
201
202
        $this->configuration->ensureProductionSettings();
203
    }
204
205 View Code Duplication
    public function testEnsureProductionSettingsQueryArrayCache()
206
    {
207
        $this->setProductionSettings();
208
        $this->configuration->setQueryCacheImpl(new ArrayCache());
209
210
        $this->expectException(ORMException::class);
211
        $this->expectExceptionMessage('Query Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
212
213
        $this->configuration->ensureProductionSettings();
214
    }
215
216 View Code Duplication
    public function testEnsureProductionSettingsMetadataArrayCache()
217
    {
218
        $this->setProductionSettings();
219
        $this->configuration->setMetadataCacheImpl(new ArrayCache());
220
221
        $this->expectException(ORMException::class);
222
        $this->expectExceptionMessage('Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
223
224
        $this->configuration->ensureProductionSettings();
225
    }
226
227 View Code Duplication
    public function testEnsureProductionSettingsAutoGenerateProxyClassesAlways()
228
    {
229
        $this->setProductionSettings();
230
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_ALWAYS);
231
232
        $this->expectException(ORMException::class);
233
        $this->expectExceptionMessage('Proxy Classes are always regenerating.');
234
235
        $this->configuration->ensureProductionSettings();
236
    }
237
238 View Code Duplication
    public function testEnsureProductionSettingsAutoGenerateProxyClassesFileNotExists()
239
    {
240
        $this->setProductionSettings();
241
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
242
243
        $this->expectException(ORMException::class);
244
        $this->expectExceptionMessage('Proxy Classes are always regenerating.');
245
246
        $this->configuration->ensureProductionSettings();
247
    }
248
249 View Code Duplication
    public function testEnsureProductionSettingsAutoGenerateProxyClassesEval()
250
    {
251
        $this->setProductionSettings();
252
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_EVAL);
253
254
        $this->expectException(ORMException::class);
255
        $this->expectExceptionMessage('Proxy Classes are always regenerating.');
256
257
        $this->configuration->ensureProductionSettings();
258
    }
259
260 View Code Duplication
    public function testAddGetCustomStringFunction()
261
    {
262
        $this->configuration->addCustomStringFunction('FunctionName', __CLASS__);
263
264
        self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('FunctionName'));
265
        self::assertSame(null, $this->configuration->getCustomStringFunction('NonExistingFunction'));
266
267
        $this->configuration->setCustomStringFunctions(['OtherFunctionName' => __CLASS__]);
268
269
        self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('OtherFunctionName'));
270
    }
271
272 View Code Duplication
    public function testAddGetCustomNumericFunction()
273
    {
274
        $this->configuration->addCustomNumericFunction('FunctionName', __CLASS__);
275
276
        self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('FunctionName'));
277
        self::assertSame(null, $this->configuration->getCustomNumericFunction('NonExistingFunction'));
278
279
        $this->configuration->setCustomNumericFunctions(['OtherFunctionName' => __CLASS__]);
280
281
        self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
282
    }
283
284 View Code Duplication
    public function testAddGetCustomDatetimeFunction()
285
    {
286
        $this->configuration->addCustomDatetimeFunction('FunctionName', __CLASS__);
287
288
        self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('FunctionName'));
289
        self::assertSame(null, $this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
290
291
        $this->configuration->setCustomDatetimeFunctions(['OtherFunctionName' => __CLASS__]);
292
293
        self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
294
    }
295
296
    public function testAddGetCustomHydrationMode()
297
    {
298
        self::assertSame(null, $this->configuration->getCustomHydrationMode('NonExisting'));
299
300
        $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
301
302
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
303
    }
304
305
    public function testSetCustomHydrationModes()
306
    {
307
        $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
308
309
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
310
311
        $this->configuration->setCustomHydrationModes(
312
            [
313
                'AnotherHydrationModeName' => __CLASS__
314
            ]
315
        );
316
317
        self::assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
318
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
319
    }
320
321
    public function testSetGetClassMetadataFactoryName()
322
    {
323
        self::assertSame(ClassMetadataFactory::class, $this->configuration->getClassMetadataFactoryName());
324
325
        $this->configuration->setClassMetadataFactoryName(__CLASS__);
326
327
        self::assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName());
328
    }
329
330
    public function testAddGetFilters()
331
    {
332
        self::assertSame(null, $this->configuration->getFilterClassName('NonExistingFilter'));
333
334
        $this->configuration->addFilter('FilterName', __CLASS__);
335
336
        self::assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName'));
337
    }
338
339
    public function setDefaultRepositoryClassName()
340
    {
341
        self::assertSame(EntityRepository::class, $this->configuration->getDefaultRepositoryClassName());
342
343
        $this->configuration->setDefaultRepositoryClassName(DDC753CustomRepository::class);
344
345
        self::assertSame(DDC753CustomRepository::class, $this->configuration->getDefaultRepositoryClassName());
346
347
        $this->expectException(ORMException::class);
348
        $this->configuration->setDefaultRepositoryClassName(__CLASS__);
349
    }
350
351
    public function testSetGetNamingStrategy()
352
    {
353
        self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy());
354
355
        $namingStrategy = $this->createMock(NamingStrategy::class);
356
357
        $this->configuration->setNamingStrategy($namingStrategy);
358
359
        self::assertSame($namingStrategy, $this->configuration->getNamingStrategy());
360
    }
361
362
    /**
363
     * @group DDC-1955
364
     */
365
    public function testSetGetEntityListenerResolver()
366
    {
367
        self::assertInstanceOf(EntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
368
        self::assertInstanceOf(DefaultEntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
369
370
        $resolver = $this->createMock(EntityListenerResolver::class);
371
372
        $this->configuration->setEntityListenerResolver($resolver);
373
374
        self::assertSame($resolver, $this->configuration->getEntityListenerResolver());
375
    }
376
377
    /**
378
     * @group DDC-2183
379
     */
380
    public function testSetGetSecondLevelCacheConfig()
381
    {
382
        $mockClass = $this->createMock(CacheConfiguration::class);
383
384
        self::assertNull($this->configuration->getSecondLevelCacheConfiguration());
385
        $this->configuration->setSecondLevelCacheConfiguration($mockClass);
386
        self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration());
387
    }
388
389
    public function testGetProxyManagerConfiguration() : void
390
    {
391
        self::assertInstanceOf(
392
            \ProxyManager\Configuration::class,
393
            $this->configuration->getProxyManagerConfiguration()
394
        );
395
    }
396
397
    public function testProxyManagerConfigurationContainsGivenProxyTargetDir() : void
398
    {
399
        $proxyPath = $this->makeTemporaryValidDirectory();
400
401
        $this->configuration->setProxyDir($proxyPath);
402
        self::assertSame($proxyPath, $this->configuration->getProxyManagerConfiguration()->getProxiesTargetDir());
403
    }
404
405
    public function testProxyManagerConfigurationContainsGivenProxyNamespace() : void
406
    {
407
        $namespace = \str_replace('.', '', \uniqid('Namespace', true));
408
409
        $this->configuration->setProxyNamespace($namespace);
410
        self::assertSame($namespace, $this->configuration->getProxyManagerConfiguration()->getProxiesNamespace());
411
    }
412
413
    /**
414
     * @dataProvider expectedGeneratorStrategies
415
     *
416
     * @param int|bool $proxyAutoGenerateFlag
417
     */
418
    public function testProxyManagerConfigurationWillBeUpdatedWithCorrectGeneratorStrategies(
419
        $proxyAutoGenerateFlag,
420
        string $expectedGeneratorStrategy
421
    ) : void {
422
        $this->configuration->setAutoGenerateProxyClasses($proxyAutoGenerateFlag);
423
424
        self::assertInstanceOf(
425
            $expectedGeneratorStrategy,
426
            $this->configuration->getProxyManagerConfiguration()->getGeneratorStrategy()
427
        );
428
    }
429
430
    public function expectedGeneratorStrategies() : array
431
    {
432
        return [
433
            [
434
                StaticProxyFactory::AUTOGENERATE_NEVER,
435
                EvaluatingGeneratorStrategy::class,
436
            ],
437
            [
438
                StaticProxyFactory::AUTOGENERATE_EVAL,
439
                EvaluatingGeneratorStrategy::class,
440
            ],
441
            [
442
                false,
443
                EvaluatingGeneratorStrategy::class,
444
            ],
445
            [
446
                StaticProxyFactory::AUTOGENERATE_ALWAYS,
447
                FileWriterGeneratorStrategy::class,
448
            ],
449
            [
450
                StaticProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS,
451
                FileWriterGeneratorStrategy::class,
452
            ],
453
            [
454
                true,
455
                FileWriterGeneratorStrategy::class,
456
            ],
457
        ];
458
    }
459
460
    private function makeTemporaryValidDirectory() : string
461
    {
462
        $path = \tempnam(\sys_get_temp_dir(), 'ProxyConfigurationTest');
463
464
        unlink($path);
465
        mkdir($path);
466
467
        return $path;
468
    }
469
470
    public function testWillProduceGhostObjectFactory() : void
471
    {
472
        $factory1 = $this->configuration->buildGhostObjectFactory();
473
        $factory2 = $this->configuration->buildGhostObjectFactory();
474
475
        $this->configuration->setProxyDir($this->makeTemporaryValidDirectory());
476
477
        $factory3 = $this->configuration->buildGhostObjectFactory();
478
479
        self::assertNotSame($factory1, $factory2);
480
        self::assertEquals($factory1, $factory2);
481
        self::assertNotEquals($factory2, $factory3);
482
    }
483
}
484
485
class ConfigurationTestAnnotationReaderChecker
486
{
487
    /** @AnnotationNamespace\PrePersist */
488
    public function annotatedMethod()
489
    {
490
    }
491
}
492