Completed
Pull Request — master (#5800)
by Herberto
10:37
created

ConfigurationTest::testAddGetCustomHydrationMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

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