Completed
Pull Request — master (#5602)
by Benjamin
10:30
created

testAddGetCustomNumericFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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