Failed Conditions
Pull Request — develop (#6719)
by Marco
79:37 queued 14:59
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 testSetGetMetadataDriverImpl()
46
    {
47
        self::assertSame(null, $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 testSetGetEntityNamespace()
70
    {
71
        $this->configuration->addEntityNamespace('TestNamespace', __NAMESPACE__);
72
        self::assertSame(__NAMESPACE__, $this->configuration->getEntityNamespace('TestNamespace'));
73
        $namespaces = ['OtherNamespace' => __NAMESPACE__];
74
        $this->configuration->setEntityNamespaces($namespaces);
75
        self::assertSame($namespaces, $this->configuration->getEntityNamespaces());
76
        $this->expectException(ORMException::class);
77
        $this->configuration->getEntityNamespace('NonExistingNamespace');
78
    }
79
80 View Code Duplication
    public function testSetGetQueryCacheImpl()
81
    {
82
        self::assertSame(null, $this->configuration->getQueryCacheImpl()); // defaults
83
        $queryCacheImpl = $this->createMock(Cache::class);
84
        $this->configuration->setQueryCacheImpl($queryCacheImpl);
85
        self::assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
86
    }
87
88 View Code Duplication
    public function testSetGetHydrationCacheImpl()
89
    {
90
        self::assertSame(null, $this->configuration->getHydrationCacheImpl()); // defaults
91
        $queryCacheImpl = $this->createMock(Cache::class);
92
        $this->configuration->setHydrationCacheImpl($queryCacheImpl);
93
        self::assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl());
94
    }
95
96 View Code Duplication
    public function testSetGetMetadataCacheImpl()
97
    {
98
        self::assertSame(null, $this->configuration->getMetadataCacheImpl()); // defaults
99
        $queryCacheImpl = $this->createMock(Cache::class);
100
        $this->configuration->setMetadataCacheImpl($queryCacheImpl);
101
        self::assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
102
    }
103
104
    public function testAddGetNamedQuery()
105
    {
106
        $dql = 'SELECT u FROM User u';
107
        $this->configuration->addNamedQuery('QueryName', $dql);
108
        self::assertSame($dql, $this->configuration->getNamedQuery('QueryName'));
109
        $this->expectException(ORMException::class);
110
        $this->expectExceptionMessage('a named query');
111
        $this->configuration->getNamedQuery('NonExistingQuery');
112
    }
113
114
    public function testAddGetNamedNativeQuery()
115
    {
116
        $sql = 'SELECT * FROM user';
117
        $rsm = $this->createMock(ResultSetMapping::class);
118
        $this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm);
119
        $fetched = $this->configuration->getNamedNativeQuery('QueryName');
120
        self::assertSame($sql, $fetched[0]);
121
        self::assertSame($rsm, $fetched[1]);
122
        $this->expectException(ORMException::class);
123
        $this->expectExceptionMessage('a named native query');
124
        $this->configuration->getNamedNativeQuery('NonExistingQuery');
125
    }
126
127
    /**
128
     * Configures $this->configuration to use production settings.
129
     *
130
     * @param string $skipCache Do not configure a cache of this type, either "query" or "metadata".
131
     */
132
    protected function setProductionSettings($skipCache = false)
133
    {
134
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
135
136
        $cache = $this->createMock(Cache::class);
137
138
        if ('query' !== $skipCache) {
139
            $this->configuration->setQueryCacheImpl($cache);
140
        }
141
142
        if ('metadata' !== $skipCache) {
143
            $this->configuration->setMetadataCacheImpl($cache);
144
        }
145
    }
146
147
    public function testEnsureProductionSettings()
148
    {
149
        $this->setProductionSettings();
150
        $this->configuration->ensureProductionSettings();
151
152
        self::addToAssertionCount(1);
153
    }
154
155
    public function testEnsureProductionSettingsQueryCache()
156
    {
157
        $this->setProductionSettings('query');
158
159
        $this->expectException(ORMException::class);
160
        $this->expectExceptionMessage('Query Cache is not configured.');
161
162
        $this->configuration->ensureProductionSettings();
163
    }
164
165
    public function testEnsureProductionSettingsMetadataCache()
166
    {
167
        $this->setProductionSettings('metadata');
168
169
        $this->expectException(ORMException::class);
170
        $this->expectExceptionMessage('Metadata Cache is not configured.');
171
172
        $this->configuration->ensureProductionSettings();
173
    }
174
175 View Code Duplication
    public function testEnsureProductionSettingsQueryArrayCache()
176
    {
177
        $this->setProductionSettings();
178
        $this->configuration->setQueryCacheImpl(new ArrayCache());
179
180
        $this->expectException(ORMException::class);
181
        $this->expectExceptionMessage('Query Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
182
183
        $this->configuration->ensureProductionSettings();
184
    }
185
186 View Code Duplication
    public function testEnsureProductionSettingsMetadataArrayCache()
187
    {
188
        $this->setProductionSettings();
189
        $this->configuration->setMetadataCacheImpl(new ArrayCache());
190
191
        $this->expectException(ORMException::class);
192
        $this->expectExceptionMessage('Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
193
194
        $this->configuration->ensureProductionSettings();
195
    }
196
197
    public function testEnsureProductionSettingsAutoGenerateProxyClassesEval()
198
    {
199
        $this->setProductionSettings();
200
        $this->configuration->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_EVAL);
201
202
        $this->expectException(ORMException::class);
203
        $this->expectExceptionMessage('Proxy Classes are always regenerating.');
204
205
        $this->configuration->ensureProductionSettings();
206
    }
207
208 View Code Duplication
    public function testAddGetCustomStringFunction()
209
    {
210
        $this->configuration->addCustomStringFunction('FunctionName', __CLASS__);
211
212
        self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('FunctionName'));
213
        self::assertSame(null, $this->configuration->getCustomStringFunction('NonExistingFunction'));
214
215
        $this->configuration->setCustomStringFunctions(['OtherFunctionName' => __CLASS__]);
216
217
        self::assertSame(__CLASS__, $this->configuration->getCustomStringFunction('OtherFunctionName'));
218
    }
219
220 View Code Duplication
    public function testAddGetCustomNumericFunction()
221
    {
222
        $this->configuration->addCustomNumericFunction('FunctionName', __CLASS__);
223
224
        self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('FunctionName'));
225
        self::assertSame(null, $this->configuration->getCustomNumericFunction('NonExistingFunction'));
226
227
        $this->configuration->setCustomNumericFunctions(['OtherFunctionName' => __CLASS__]);
228
229
        self::assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
230
    }
231
232 View Code Duplication
    public function testAddGetCustomDatetimeFunction()
233
    {
234
        $this->configuration->addCustomDatetimeFunction('FunctionName', __CLASS__);
235
236
        self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('FunctionName'));
237
        self::assertSame(null, $this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
238
239
        $this->configuration->setCustomDatetimeFunctions(['OtherFunctionName' => __CLASS__]);
240
241
        self::assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
242
    }
243
244
    public function testAddGetCustomHydrationMode()
245
    {
246
        self::assertSame(null, $this->configuration->getCustomHydrationMode('NonExisting'));
247
248
        $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
249
250
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
251
    }
252
253
    public function testSetCustomHydrationModes()
254
    {
255
        $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
256
257
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
258
259
        $this->configuration->setCustomHydrationModes(
260
            [
0 ignored issues
show
Documentation introduced by
array('AnotherHydrationModeName' => __CLASS__) is of type array<string,string,{"An...ionModeName":"string"}>, but the function expects a object<Doctrine\ORM\iterable>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
261
                'AnotherHydrationModeName' => __CLASS__
262
            ]
263
        );
264
265
        self::assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
266
        self::assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
267
    }
268
269
    public function testSetGetClassMetadataFactoryName()
270
    {
271
        self::assertSame(ClassMetadataFactory::class, $this->configuration->getClassMetadataFactoryName());
272
273
        $this->configuration->setClassMetadataFactoryName(__CLASS__);
274
275
        self::assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName());
276
    }
277
278
    public function testAddGetFilters()
279
    {
280
        self::assertSame(null, $this->configuration->getFilterClassName('NonExistingFilter'));
281
282
        $this->configuration->addFilter('FilterName', __CLASS__);
283
284
        self::assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName'));
285
    }
286
287
    public function setDefaultRepositoryClassName()
288
    {
289
        self::assertSame(EntityRepository::class, $this->configuration->getDefaultRepositoryClassName());
290
291
        $this->configuration->setDefaultRepositoryClassName(DDC753CustomRepository::class);
292
293
        self::assertSame(DDC753CustomRepository::class, $this->configuration->getDefaultRepositoryClassName());
294
295
        $this->expectException(ORMException::class);
296
        $this->configuration->setDefaultRepositoryClassName(__CLASS__);
297
    }
298
299
    public function testSetGetNamingStrategy()
300
    {
301
        self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy());
302
303
        $namingStrategy = $this->createMock(NamingStrategy::class);
304
305
        $this->configuration->setNamingStrategy($namingStrategy);
306
307
        self::assertSame($namingStrategy, $this->configuration->getNamingStrategy());
308
    }
309
310
    /**
311
     * @group DDC-1955
312
     */
313
    public function testSetGetEntityListenerResolver()
314
    {
315
        self::assertInstanceOf(EntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
316
        self::assertInstanceOf(DefaultEntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
317
318
        $resolver = $this->createMock(EntityListenerResolver::class);
319
320
        $this->configuration->setEntityListenerResolver($resolver);
321
322
        self::assertSame($resolver, $this->configuration->getEntityListenerResolver());
323
    }
324
325
    /**
326
     * @group DDC-2183
327
     */
328
    public function testSetGetSecondLevelCacheConfig()
329
    {
330
        $mockClass = $this->createMock(CacheConfiguration::class);
331
332
        self::assertNull($this->configuration->getSecondLevelCacheConfiguration());
333
        $this->configuration->setSecondLevelCacheConfiguration($mockClass);
334
        self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration());
335
    }
336
337
    public function testGetProxyManagerConfiguration() : void
338
    {
339
        self::assertInstanceOf(
340
            \ProxyManager\Configuration::class,
341
            $this->configuration->getProxyManagerConfiguration()
342
        );
343
    }
344
345
    public function testProxyManagerConfigurationContainsGivenProxyTargetDir() : void
346
    {
347
        $proxyPath = $this->makeTemporaryValidDirectory();
348
349
        $this->configuration->setProxyDir($proxyPath);
350
        self::assertSame($proxyPath, $this->configuration->getProxyManagerConfiguration()->getProxiesTargetDir());
351
    }
352
353
    public function testProxyManagerConfigurationContainsGivenProxyNamespace() : void
354
    {
355
        $namespace = \str_replace('.', '', \uniqid('Namespace', true));
356
357
        $this->configuration->setProxyNamespace($namespace);
358
        self::assertSame($namespace, $this->configuration->getProxyManagerConfiguration()->getProxiesNamespace());
359
    }
360
361
    /**
362
     * @dataProvider expectedGeneratorStrategies
363
     *
364
     * @param int|bool $proxyAutoGenerateFlag
365
     */
366
    public function testProxyManagerConfigurationWillBeUpdatedWithCorrectGeneratorStrategies(
367
        $proxyAutoGenerateFlag,
368
        string $expectedGeneratorStrategy
369
    ) : void {
370
        $this->configuration->setAutoGenerateProxyClasses($proxyAutoGenerateFlag);
371
372
        self::assertInstanceOf(
373
            $expectedGeneratorStrategy,
374
            $this->configuration->getProxyManagerConfiguration()->getGeneratorStrategy()
375
        );
376
    }
377
378
    public function expectedGeneratorStrategies() : array
379
    {
380
        return [
381
            [
382
                StaticProxyFactory::AUTOGENERATE_NEVER,
383
                EvaluatingGeneratorStrategy::class,
384
            ],
385
            [
386
                StaticProxyFactory::AUTOGENERATE_EVAL,
387
                EvaluatingGeneratorStrategy::class,
388
            ],
389
            [
390
                false,
391
                EvaluatingGeneratorStrategy::class,
392
            ],
393
            [
394
                StaticProxyFactory::AUTOGENERATE_ALWAYS,
395
                FileWriterGeneratorStrategy::class,
396
            ],
397
            [
398
                StaticProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS,
399
                FileWriterGeneratorStrategy::class,
400
            ],
401
            [
402
                true,
403
                FileWriterGeneratorStrategy::class,
404
            ],
405
        ];
406
    }
407
408
    private function makeTemporaryValidDirectory() : string
409
    {
410
        $path = \tempnam(\sys_get_temp_dir(), 'ProxyConfigurationTest');
411
412
        unlink($path);
413
        mkdir($path);
414
415
        return $path;
416
    }
417
418
    public function testWillProduceGhostObjectFactory() : void
419
    {
420
        $factory1 = $this->configuration->buildGhostObjectFactory();
421
        $factory2 = $this->configuration->buildGhostObjectFactory();
422
423
        $this->configuration->setProxyDir($this->makeTemporaryValidDirectory());
424
425
        $factory3 = $this->configuration->buildGhostObjectFactory();
426
427
        self::assertNotSame($factory1, $factory2);
428
        self::assertEquals($factory1, $factory2);
429
        self::assertNotEquals($factory2, $factory3);
430
    }
431
}
432
433
class ConfigurationTestAnnotationReaderChecker
434
{
435
    /** @AnnotationNamespace\PrePersist */
436
    public function annotatedMethod()
437
    {
438
    }
439
}
440