Passed
Pull Request — master (#7770)
by
unknown
09:22
created

ConfigurationTest::testGetProxyDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\ORM\Annotation as ORM;
10
use Doctrine\ORM\Cache\CacheConfiguration;
11
use Doctrine\ORM\Cache\Exception\MetadataCacheNotConfigured;
12
use Doctrine\ORM\Cache\Exception\MetadataCacheUsesNonPersistentCache;
13
use Doctrine\ORM\Cache\Exception\QueryCacheNotConfigured;
14
use Doctrine\ORM\Cache\Exception\QueryCacheUsesNonPersistentCache;
15
use Doctrine\ORM\Configuration;
16
use Doctrine\ORM\EntityRepository;
17
use Doctrine\ORM\Exception\ORMException;
18
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
19
use Doctrine\ORM\Mapping\ClassMetadataFactory;
20
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
21
use Doctrine\ORM\Mapping\Driver\MappingDriver;
22
use Doctrine\ORM\Mapping\EntityListenerResolver;
23
use Doctrine\ORM\Mapping\Factory\NamingStrategy;
24
use Doctrine\ORM\Proxy\Factory\ProxyFactory;
25
use Doctrine\Tests\DoctrineTestCase;
26
use Doctrine\Tests\Models\DDC753\DDC753CustomRepository;
27
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
28
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
29
use ReflectionClass;
30
use function mkdir;
31
use function str_replace;
32
use function sys_get_temp_dir;
33
use function tempnam;
34
use function uniqid;
35
use function unlink;
36
37
/**
38
 * Tests for the Configuration object
39
 */
40
class ConfigurationTest extends DoctrineTestCase
41
{
42
    /** @var Configuration */
43
    private $configuration;
44
45
    protected function setUp() : void
46
    {
47
        parent::setUp();
48
        $this->configuration = new Configuration();
49
    }
50
51
    public function testSetGetMetadataDriverImpl() : void
52
    {
53
        self::assertNull($this->configuration->getMetadataDriverImpl()); // defaults
54
55
        $metadataDriver = $this->createMock(MappingDriver::class);
56
        $this->configuration->setMetadataDriverImpl($metadataDriver);
57
        self::assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
58
    }
59
60
    public function testNewDefaultAnnotationDriver() : void
61
    {
62
        $paths           = [__DIR__];
63
        $reflectionClass = new ReflectionClass(ConfigurationTestAnnotationReaderChecker::class);
64
65
        $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
66
        $reader           = $annotationDriver->getReader();
67
        $annotation       = $reader->getMethodAnnotation(
68
            $reflectionClass->getMethod('annotatedMethod'),
69
            ORM\PrePersist::class
70
        );
71
72
        self::assertInstanceOf(ORM\PrePersist::class, $annotation);
73
    }
74
75
    public function testSetGetQueryCacheImpl() : void
76
    {
77
        self::assertNull($this->configuration->getQueryCacheImpl()); // defaults
78
        $queryCacheImpl = $this->createMock(Cache::class);
79
        $this->configuration->setQueryCacheImpl($queryCacheImpl);
80
        self::assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
81
    }
82
83
    public function testSetGetHydrationCacheImpl() : void
84
    {
85
        self::assertNull($this->configuration->getHydrationCacheImpl()); // defaults
86
        $queryCacheImpl = $this->createMock(Cache::class);
87
        $this->configuration->setHydrationCacheImpl($queryCacheImpl);
88
        self::assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl());
89
    }
90
91
    public function testSetGetMetadataCacheImpl() : void
92
    {
93
        self::assertNull($this->configuration->getMetadataCacheImpl()); // defaults
94
        $queryCacheImpl = $this->createMock(Cache::class);
95
        $this->configuration->setMetadataCacheImpl($queryCacheImpl);
96
        self::assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
97
    }
98
99
    /**
100
     * Configures $this->configuration to use production settings.
101
     *
102
     * @param string $skipCache Do not configure a cache of this type, either "query" or "metadata".
103
     */
104
    protected function setProductionSettings($skipCache = false)
105
    {
106
        $this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
107
108
        $cache = $this->createMock(Cache::class);
109
110
        if ($skipCache !== 'query') {
111
            $this->configuration->setQueryCacheImpl($cache);
112
        }
113
114
        if ($skipCache !== 'metadata') {
115
            $this->configuration->setMetadataCacheImpl($cache);
116
        }
117
    }
118
119
    public function testEnsureProductionSettings() : void
120
    {
121
        $this->setProductionSettings();
122
        $this->configuration->ensureProductionSettings();
123
124
        self::addToAssertionCount(1);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::addToAssertionCount() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        self::/** @scrutinizer ignore-call */ 
125
              addToAssertionCount(1);
Loading history...
125
    }
126
127
    public function testEnsureProductionSettingsQueryCache() : void
128
    {
129
        $this->setProductionSettings('query');
130
131
        $this->expectException(QueryCacheNotConfigured::class);
132
        $this->expectExceptionMessage('Query Cache is not configured.');
133
134
        $this->configuration->ensureProductionSettings();
135
    }
136
137
    public function testEnsureProductionSettingsMetadataCache() : void
138
    {
139
        $this->setProductionSettings('metadata');
140
141
        $this->expectException(MetadataCacheNotConfigured::class);
142
        $this->expectExceptionMessage('Metadata Cache is not configured.');
143
144
        $this->configuration->ensureProductionSettings();
145
    }
146
147
    public function testEnsureProductionSettingsQueryArrayCache() : void
148
    {
149
        $this->setProductionSettings();
150
        $this->configuration->setQueryCacheImpl(new ArrayCache());
151
152
        $this->expectException(QueryCacheUsesNonPersistentCache::class);
153
        $this->expectExceptionMessage('Query Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
154
155
        $this->configuration->ensureProductionSettings();
156
    }
157
158
    public function testEnsureProductionSettingsMetadataArrayCache() : void
159
    {
160
        $this->setProductionSettings();
161
        $this->configuration->setMetadataCacheImpl(new ArrayCache());
162
163
        $this->expectException(MetadataCacheUsesNonPersistentCache::class);
164
        $this->expectExceptionMessage('Metadata Cache uses a non-persistent cache driver, Doctrine\Common\Cache\ArrayCache.');
165
166
        $this->configuration->ensureProductionSettings();
167
    }
168
169
    public function testEnsureProductionSettingsAutoGenerateProxyClassesEval() : void
170
    {
171
        $this->setProductionSettings();
172
        $this->configuration->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
173
174
        $this->expectException(ProxyClassesAlwaysRegenerating::class);
175
        $this->expectExceptionMessage('Proxy Classes are always regenerating.');
176
177
        $this->configuration->ensureProductionSettings();
178
    }
179
180
    public function testAddGetCustomStringFunction() : void
181
    {
182
        $this->configuration->addCustomStringFunction('FunctionName', self::class);
183
184
        self::assertSame(self::class, $this->configuration->getCustomStringFunction('FunctionName'));
185
        self::assertNull($this->configuration->getCustomStringFunction('NonExistingFunction'));
186
187
        $this->configuration->setCustomStringFunctions(['OtherFunctionName' => self::class]);
188
189
        self::assertSame(self::class, $this->configuration->getCustomStringFunction('OtherFunctionName'));
190
    }
191
192
    public function testAddGetCustomNumericFunction() : void
193
    {
194
        $this->configuration->addCustomNumericFunction('FunctionName', self::class);
195
196
        self::assertSame(self::class, $this->configuration->getCustomNumericFunction('FunctionName'));
197
        self::assertNull($this->configuration->getCustomNumericFunction('NonExistingFunction'));
198
199
        $this->configuration->setCustomNumericFunctions(['OtherFunctionName' => self::class]);
200
201
        self::assertSame(self::class, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
202
    }
203
204
    public function testAddGetCustomDatetimeFunction() : void
205
    {
206
        $this->configuration->addCustomDatetimeFunction('FunctionName', self::class);
207
208
        self::assertSame(self::class, $this->configuration->getCustomDatetimeFunction('FunctionName'));
209
        self::assertNull($this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
210
211
        $this->configuration->setCustomDatetimeFunctions(['OtherFunctionName' => self::class]);
212
213
        self::assertSame(self::class, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
214
    }
215
216
    public function testAddGetCustomHydrationMode() : void
217
    {
218
        self::assertNull($this->configuration->getCustomHydrationMode('NonExisting'));
219
220
        $this->configuration->addCustomHydrationMode('HydrationModeName', self::class);
221
222
        self::assertSame(self::class, $this->configuration->getCustomHydrationMode('HydrationModeName'));
223
    }
224
225
    public function testSetCustomHydrationModes() : void
226
    {
227
        $this->configuration->addCustomHydrationMode('HydrationModeName', self::class);
228
229
        self::assertSame(self::class, $this->configuration->getCustomHydrationMode('HydrationModeName'));
230
231
        $this->configuration->setCustomHydrationModes(
232
            ['AnotherHydrationModeName' => self::class]
233
        );
234
235
        self::assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
236
        self::assertSame(self::class, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
237
    }
238
239
    public function testSetGetClassMetadataFactoryName() : void
240
    {
241
        self::assertSame(ClassMetadataFactory::class, $this->configuration->getClassMetadataFactoryName());
242
243
        $this->configuration->setClassMetadataFactoryName(self::class);
244
245
        self::assertSame(self::class, $this->configuration->getClassMetadataFactoryName());
246
    }
247
248
    public function testAddGetFilters() : void
249
    {
250
        self::assertNull($this->configuration->getFilterClassName('NonExistingFilter'));
251
252
        $this->configuration->addFilter('FilterName', self::class);
253
254
        self::assertSame(self::class, $this->configuration->getFilterClassName('FilterName'));
255
    }
256
257
    public function setDefaultRepositoryClassName()
258
    {
259
        self::assertSame(EntityRepository::class, $this->configuration->getDefaultRepositoryClassName());
260
261
        $this->configuration->setDefaultRepositoryClassName(DDC753CustomRepository::class);
262
263
        self::assertSame(DDC753CustomRepository::class, $this->configuration->getDefaultRepositoryClassName());
264
265
        $this->expectException(ORMException::class);
266
        $this->configuration->setDefaultRepositoryClassName(self::class);
267
    }
268
269
    public function testSetGetNamingStrategy() : void
270
    {
271
        self::assertInstanceOf(NamingStrategy::class, $this->configuration->getNamingStrategy());
272
273
        $namingStrategy = $this->createMock(NamingStrategy::class);
274
275
        $this->configuration->setNamingStrategy($namingStrategy);
276
277
        self::assertSame($namingStrategy, $this->configuration->getNamingStrategy());
278
    }
279
280
    /**
281
     * @group DDC-1955
282
     */
283
    public function testSetGetEntityListenerResolver() : void
284
    {
285
        self::assertInstanceOf(EntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
286
        self::assertInstanceOf(DefaultEntityListenerResolver::class, $this->configuration->getEntityListenerResolver());
287
288
        $resolver = $this->createMock(EntityListenerResolver::class);
289
290
        $this->configuration->setEntityListenerResolver($resolver);
291
292
        self::assertSame($resolver, $this->configuration->getEntityListenerResolver());
293
    }
294
295
    /**
296
     * @group DDC-2183
297
     */
298
    public function testSetGetSecondLevelCacheConfig() : void
299
    {
300
        $mockClass = $this->createMock(CacheConfiguration::class);
301
302
        self::assertNull($this->configuration->getSecondLevelCacheConfiguration());
303
        $this->configuration->setSecondLevelCacheConfiguration($mockClass);
304
        self::assertEquals($mockClass, $this->configuration->getSecondLevelCacheConfiguration());
305
    }
306
307
    public function testGetProxyManagerConfiguration() : void
308
    {
309
        self::assertInstanceOf(
310
            \ProxyManager\Configuration::class,
311
            $this->configuration->getProxyManagerConfiguration()
312
        );
313
    }
314
315
    public function testProxyManagerConfigurationContainsGivenProxyTargetDir() : void
316
    {
317
        $proxyPath = $this->makeTemporaryValidDirectory();
318
319
        $this->configuration->setProxyDir($proxyPath);
320
        self::assertSame($proxyPath, $this->configuration->getProxyManagerConfiguration()->getProxiesTargetDir());
321
    }
322
323
    public function testProxyManagerConfigurationContainsGivenProxyNamespace() : void
324
    {
325
        $namespace = str_replace('.', '', uniqid('Namespace', true));
326
327
        $this->configuration->setProxyNamespace($namespace);
328
        self::assertSame($namespace, $this->configuration->getProxyManagerConfiguration()->getProxiesNamespace());
329
    }
330
331
    public function testGetProxyDir(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
332
    {
333
        $proxyDir = $this->makeTemporaryValidDirectory();
334
335
        $this->configuration->setProxyDir($proxyDir);
336
        self::assertSame($proxyDir, $this->configuration->getProxyDir());
337
    }
338
339
    /**
340
     * @param int|bool $proxyAutoGenerateFlag
341
     *
342
     * @dataProvider expectedGeneratorStrategies
343
     */
344
    public function testProxyManagerConfigurationWillBeUpdatedWithCorrectGeneratorStrategies(
345
        $proxyAutoGenerateFlag,
346
        string $expectedGeneratorStrategy
347
    ) : void {
348
        $this->configuration->setAutoGenerateProxyClasses($proxyAutoGenerateFlag);
349
350
        self::assertInstanceOf(
351
            $expectedGeneratorStrategy,
352
            $this->configuration->getProxyManagerConfiguration()->getGeneratorStrategy()
353
        );
354
    }
355
356
    public function expectedGeneratorStrategies() : array
357
    {
358
        return [
359
            [
360
                ProxyFactory::AUTOGENERATE_NEVER,
361
                EvaluatingGeneratorStrategy::class,
362
            ],
363
            [
364
                ProxyFactory::AUTOGENERATE_EVAL,
365
                EvaluatingGeneratorStrategy::class,
366
            ],
367
            [
368
                false,
369
                EvaluatingGeneratorStrategy::class,
370
            ],
371
            [
372
                ProxyFactory::AUTOGENERATE_ALWAYS,
373
                FileWriterGeneratorStrategy::class,
374
            ],
375
            [
376
                ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS,
377
                FileWriterGeneratorStrategy::class,
378
            ],
379
            [
380
                true,
381
                FileWriterGeneratorStrategy::class,
382
            ],
383
        ];
384
    }
385
386
    private function makeTemporaryValidDirectory() : string
387
    {
388
        $path = tempnam(sys_get_temp_dir(), 'ProxyConfigurationTest');
389
390
        unlink($path);
391
        mkdir($path);
392
393
        return $path;
394
    }
395
396
    public function testWillProduceGhostObjectFactory() : void
397
    {
398
        $factory1 = $this->configuration->buildGhostObjectFactory();
399
        $factory2 = $this->configuration->buildGhostObjectFactory();
400
401
        $this->configuration->setProxyDir($this->makeTemporaryValidDirectory());
402
403
        $factory3 = $this->configuration->buildGhostObjectFactory();
404
405
        self::assertNotSame($factory1, $factory2);
406
        self::assertEquals($factory1, $factory2);
407
        self::assertNotEquals($factory2, $factory3);
408
    }
409
}
410
411
class ConfigurationTestAnnotationReaderChecker
412
{
413
    /** @ORM\PrePersist */
414
    public function annotatedMethod()
415
    {
416
    }
417
}
418