Completed
Pull Request — master (#478)
by Gianluca
08:28 queued 05:42
created

testWillInstantiateConfigWithQuoteStrategyObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Service;
21
22
use PHPUnit_Framework_TestCase;
23
use DoctrineORMModule\Service\ConfigurationFactory;
24
use Doctrine\Common\Cache\ArrayCache;
25
use Zend\ServiceManager\ServiceManager;
26
27
class ConfigurationFactoryTest extends PHPUnit_Framework_TestCase
28
{
29
    /**
30
     * @var ServiceManager
31
     */
32
    protected $serviceManager;
33
    /**
34
     * @var ConfigurationFactory
35
     */
36
    protected $factory;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function setUp()
42
    {
43
        $this->serviceManager = new ServiceManager();
44
        $this->factory = new ConfigurationFactory('test_default');
45
        $this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
46
        $this->serviceManager->setService(
47
            'doctrine.driver.orm_default',
48
            $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')
49
        );
50
    }
51
52
    public function testWillInstantiateConfigWithoutNamingStrategySetting()
53
    {
54
        $config = array(
55
            'doctrine' => array(
56
                'configuration' => array(
57
                    'test_default' => array(),
58
                ),
59
            ),
60
        );
61
        $this->serviceManager->setService('Config', $config);
62
        $ormConfig = $this->factory->createService($this->serviceManager);
63
        $this->assertInstanceOf('Doctrine\ORM\Mapping\NamingStrategy', $ormConfig->getNamingStrategy());
64
    }
65
66
    public function testWillInstantiateConfigWithNamingStrategyObject()
67
    {
68
        $namingStrategy = $this->getMock('Doctrine\ORM\Mapping\NamingStrategy');
69
70
        $config = array(
71
            'doctrine' => array(
72
                'configuration' => array(
73
                    'test_default' => array(
74
                        'naming_strategy' => $namingStrategy,
75
                    ),
76
                ),
77
            ),
78
        );
79
        $this->serviceManager->setService('Config', $config);
80
        $factory = new ConfigurationFactory('test_default');
81
        $ormConfig = $factory->createService($this->serviceManager);
82
        $this->assertSame($namingStrategy, $ormConfig->getNamingStrategy());
83
    }
84
85
    public function testWillInstantiateConfigWithNamingStrategyReference()
86
    {
87
        $namingStrategy = $this->getMock('Doctrine\ORM\Mapping\NamingStrategy');
88
        $config = array(
89
            'doctrine' => array(
90
                'configuration' => array(
91
                    'test_default' => array(
92
                        'naming_strategy' => 'test_naming_strategy',
93
                    ),
94
                ),
95
            ),
96
        );
97
        $this->serviceManager->setService('Config', $config);
98
        $this->serviceManager->setService('test_naming_strategy', $namingStrategy);
99
        $ormConfig = $this->factory->createService($this->serviceManager);
100
        $this->assertSame($namingStrategy, $ormConfig->getNamingStrategy());
101
    }
102
103
    public function testWillNotInstantiateConfigWithInvalidNamingStrategyReference()
104
    {
105
        $config = array(
106
            'doctrine' => array(
107
                'configuration' => array(
108
                    'test_default' => array(
109
                        'naming_strategy' => 'test_naming_strategy',
110
                    ),
111
                ),
112
            ),
113
        );
114
        $this->serviceManager->setService('Config', $config);
115
        $this->setExpectedException('Zend\ServiceManager\Exception\InvalidArgumentException');
116
        $this->factory->createService($this->serviceManager);
117
    }
118
119
    public function testWillInstantiateConfigWithQuoteStrategyObject()
120
    {
121
        $quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
122
123
        $config = array(
124
            'doctrine' => array(
125
                'configuration' => array(
126
                    'test_default' => array(
127
                        'quote_strategy' => $quoteStrategy,
128
                    ),
129
                ),
130
            ),
131
        );
132
        $this->serviceManager->setService('Config', $config);
133
        $factory = new ConfigurationFactory('test_default');
134
        $ormConfig = $factory->createService($this->serviceManager);
135
        $this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
136
    }
137
138
    public function testWillInstantiateConfigWithQuoteStrategyReference()
139
    {
140
        $quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
141
        $config = array(
142
            'doctrine' => array(
143
                'configuration' => array(
144
                    'test_default' => array(
145
                        'quote_strategy' => 'test_quote_strategy',
146
                    ),
147
                ),
148
            ),
149
        );
150
        $this->serviceManager->setService('Config', $config);
151
        $this->serviceManager->setService('test_quote_strategy', $quoteStrategy);
152
        $ormConfig = $this->factory->createService($this->serviceManager);
153
        $this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
154
    }
155
156
    public function testWillNotInstantiateConfigWithInvalidQuoteStrategyReference()
157
    {
158
        $config = array(
159
            'doctrine' => array(
160
                'configuration' => array(
161
                    'test_default' => array(
162
                        'quote_strategy' => 'test_quote_strategy',
163
                    ),
164
                ),
165
            ),
166
        );
167
        $this->serviceManager->setService('Config', $config);
168
        $this->setExpectedException('Zend\ServiceManager\Exception\InvalidArgumentException');
169
        $this->factory->createService($this->serviceManager);
170
    }
171
172
    public function testWillInstantiateConfigWithHydrationCacheSetting()
173
    {
174
        $config = array(
175
            'doctrine' => array(
176
                'configuration' => array(
177
                    'test_default' => array(
178
                        'hydration_cache' => 'array',
179
                    ),
180
                ),
181
            ),
182
        );
183
        $this->serviceManager->setService('Config', $config);
184
        $factory = new ConfigurationFactory('test_default');
185
        $ormConfig = $factory->createService($this->serviceManager);
186
        $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $ormConfig->getHydrationCacheImpl());
187
    }
188
189
    public function testCanSetDefaultRepositoryClass()
190
    {
191
        $repositoryClass = 'DoctrineORMModuleTest\Assets\RepositoryClass';
192
193
        $config = array(
194
            'doctrine' => array(
195
                'configuration' => array(
196
                    'test_default' => array(
197
198
                        'hydration_cache' => 'array',
199
                        'default_repository_class_name' => $repositoryClass,
200
                    ),
201
                ),
202
            ),
203
        );
204
        $this->serviceManager->setService('Config', $config);
205
206
        $factory = new ConfigurationFactory('test_default');
207
        $ormConfig = $factory->createService($this->serviceManager);
208
        $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $ormConfig->getHydrationCacheImpl());
209
    }
210
211
    public function testAcceptsMetadataFactory()
212
    {
213
        $config = array(
214
            'doctrine' => array(
215
                'configuration' => array(
216
                    'test_default' => array(
217
                        'classMetadataFactoryName' => 'Factory'
218
                    ),
219
                ),
220
            ),
221
        );
222
        $this->serviceManager->setService('Config', $config);
223
        $factory = new ConfigurationFactory('test_default');
224
        $ormConfig = $factory->createService($this->serviceManager);
225
        $this->assertEquals('Factory', $ormConfig->getClassMetadataFactoryName());
226
    }
227
228
    public function testDefaultMetadatFactory()
229
    {
230
        $config = array(
231
            'doctrine' => array(
232
                'configuration' => array(
233
                    'test_default' => array(
234
                    ),
235
                ),
236
            ),
237
        );
238
        $this->serviceManager->setService('Config', $config);
239
        $factory = new ConfigurationFactory('test_default');
240
        $ormConfig = $factory->createService($this->serviceManager);
241
        $this->assertEquals('Doctrine\ORM\Mapping\ClassMetadataFactory', $ormConfig->getClassMetadataFactoryName());
242
    }
243
244
    public function testWillInstantiateConfigWithoutEntityListenerResolverSetting()
245
    {
246
        $config = array(
247
            'doctrine' => array(
248
                'configuration' => array(
249
                    'test_default' => array(),
250
                ),
251
            ),
252
        );
253
254
        $this->serviceManager->setService('Config', $config);
255
256
        $ormConfig = $this->factory->createService($this->serviceManager);
257
258
        $this->assertInstanceOf('Doctrine\ORM\Mapping\EntityListenerResolver', $ormConfig->getEntityListenerResolver());
259
    }
260
261
    public function testWillInstantiateConfigWithEntityListenerResolverObject()
262
    {
263
        $entityListenerResolver = $this->getMock('Doctrine\ORM\Mapping\EntityListenerResolver');
264
265
        $config = array(
266
            'doctrine' => array(
267
                'configuration' => array(
268
                    'test_default' => array(
269
                        'entity_listener_resolver' => $entityListenerResolver,
270
                    ),
271
                ),
272
            ),
273
        );
274
275
        $this->serviceManager->setService('Config', $config);
276
277
        $ormConfig = $this->factory->createService($this->serviceManager);
278
279
        $this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver());
280
    }
281
282
    public function testWillInstantiateConfigWithEntityListenerResolverReference()
283
    {
284
        $entityListenerResolver = $this->getMock('Doctrine\ORM\Mapping\EntityListenerResolver');
285
286
        $config = array(
287
            'doctrine' => array(
288
                'configuration' => array(
289
                    'test_default' => array(
290
                        'entity_listener_resolver' => 'test_entity_listener_resolver',
291
                    ),
292
                ),
293
            ),
294
        );
295
296
        $this->serviceManager->setService('Config', $config);
297
        $this->serviceManager->setService('test_entity_listener_resolver', $entityListenerResolver);
298
299
        $ormConfig = $this->factory->createService($this->serviceManager);
300
301
        $this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver());
302
    }
303
304
    public function testDoNotCreateSecondLevelCacheByDefault()
305
    {
306
        $config = array(
307
            'doctrine' => array(
308
                'configuration' => array(
309
                    'test_default' => array(),
310
                ),
311
            ),
312
        );
313
314
        $this->serviceManager->setService('Config', $config);
315
316
        $ormConfig = $this->factory->createService($this->serviceManager);
317
318
        $this->assertNull($ormConfig->getSecondLevelCacheConfiguration());
319
    }
320
321
    public function testCanInstantiateWithSecondLevelCacheConfig()
322
    {
323
        $config = array(
324
            'doctrine' => array(
325
                'configuration' => array(
326
                    'test_default' => array(
327
                        'result_cache' => 'array',
328
329
                        'second_level_cache' => array(
330
                            'enabled'                    => true,
331
                            'default_lifetime'           => 200,
332
                            'default_lock_lifetime'      => 500,
333
                            'file_lock_region_directory' => 'my_dir',
334
335
                            'regions' => array(
336
                                'my_first_region' => array(
337
                                    'lifetime'      => 800,
338
                                    'lock_lifetime' => 1000
339
                                ),
340
341
                                'my_second_region' => array(
342
                                    'lifetime'      => 10,
343
                                    'lock_lifetime' => 20
344
                                )
345
                            )
346
                        )
347
                    ),
348
                ),
349
            ),
350
        );
351
352
        $this->serviceManager->setService('Config', $config);
353
354
        $ormConfig        = $this->factory->createService($this->serviceManager);
355
        $secondLevelCache = $ormConfig->getSecondLevelCacheConfiguration();
356
        
357
        $this->assertInstanceOf('Doctrine\ORM\Cache\CacheConfiguration', $secondLevelCache);
358
359
        $cacheFactory = $secondLevelCache->getCacheFactory();
360
        $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCacheFactory', $cacheFactory);
361
        $this->assertEquals('my_dir', $cacheFactory->getFileLockRegionDirectory());
362
363
        $regionsConfiguration = $secondLevelCache->getRegionsConfiguration();
364
        $this->assertEquals(200, $regionsConfiguration->getDefaultLifetime());
365
        $this->assertEquals(500, $regionsConfiguration->getDefaultLockLifetime());
366
367
        $this->assertEquals(800, $regionsConfiguration->getLifetime('my_first_region'));
368
        $this->assertEquals(10, $regionsConfiguration->getLifetime('my_second_region'));
369
370
        $this->assertEquals(1000, $regionsConfiguration->getLockLifetime('my_first_region'));
371
        $this->assertEquals(20, $regionsConfiguration->getLockLifetime('my_second_region'));
372
373
        // Doctrine does not allow to retrieve the cache adapter from cache factory, so we are forced to use
374
        // reflection here
375
        $reflProperty = new \ReflectionProperty($cacheFactory, 'cache');
376
        $reflProperty->setAccessible(true);
377
        $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $reflProperty->getValue($cacheFactory));
378
    }
379
}
380