Completed
Pull Request — master (#602)
by Tom
07:01
created

ConfigurationFactoryTest::testCanInstantiateWithSecondLevelCacheConfig()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 8.9163
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModuleTest\Service;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
9
use Doctrine\ORM\Cache\CacheConfiguration;
10
use Doctrine\ORM\Cache\DefaultCacheFactory;
11
use Doctrine\ORM\Mapping\ClassMetadataFactory;
12
use Doctrine\ORM\Mapping\EntityListenerResolver;
13
use Doctrine\ORM\Mapping\NamingStrategy;
14
use Doctrine\ORM\Mapping\QuoteStrategy;
15
use DoctrineORMModule\Service\ConfigurationFactory;
16
use DoctrineORMModuleTest\Assets\RepositoryClass;
17
use Laminas\ServiceManager\Exception\InvalidArgumentException;
18
use Laminas\ServiceManager\ServiceManager;
19
use PHPUnit\Framework\TestCase;
20
use ReflectionProperty;
21
22
class ConfigurationFactoryTest extends TestCase
23
{
24
    protected ServiceManager $serviceManager;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
    protected ConfigurationFactory $factory;
27
28
    protected function setUp() : void
29
    {
30
        $this->serviceManager = new ServiceManager();
31
        $this->factory        = new ConfigurationFactory('test_default');
32
        $this->serviceManager->setService('doctrine.cache.array', new ArrayCache());
33
        $this->serviceManager->setService(
34
            'doctrine.driver.orm_default',
35
            $this->createMock(MappingDriver::class)
36
        );
37
    }
38
39
    public function testWillInstantiateConfigWithoutNamingStrategySetting() : void
40
    {
41
        $config = [
42
            'doctrine' => [
43
                'configuration' => [
44
                    'test_default' => [],
45
                ],
46
            ],
47
        ];
48
        $this->serviceManager->setService('config', $config);
49
        $ormConfig = $this->factory->createService($this->serviceManager);
50
        $this->assertInstanceOf(NamingStrategy::class, $ormConfig->getNamingStrategy());
51
    }
52
53
    public function testWillInstantiateConfigWithNamingStrategyObject() : void
54
    {
55
        $namingStrategy = $this->createMock(NamingStrategy::class);
56
57
        $config = [
58
            'doctrine' => [
59
                'configuration' => [
60
                    'test_default' => ['naming_strategy' => $namingStrategy],
61
                ],
62
            ],
63
        ];
64
        $this->serviceManager->setService('config', $config);
65
        $factory   = new ConfigurationFactory('test_default');
66
        $ormConfig = $factory->createService($this->serviceManager);
67
        $this->assertSame($namingStrategy, $ormConfig->getNamingStrategy());
68
    }
69
70
    public function testWillInstantiateConfigWithNamingStrategyReference() : void
71
    {
72
        $namingStrategy = $this->createMock(NamingStrategy::class);
73
        $config         = [
74
            'doctrine' => [
75
                'configuration' => [
76
                    'test_default' => ['naming_strategy' => 'test_naming_strategy'],
77
                ],
78
            ],
79
        ];
80
        $this->serviceManager->setService('config', $config);
81
        $this->serviceManager->setService('test_naming_strategy', $namingStrategy);
82
        $ormConfig = $this->factory->createService($this->serviceManager);
83
        $this->assertSame($namingStrategy, $ormConfig->getNamingStrategy());
84
    }
85
86
    public function testWillNotInstantiateConfigWithInvalidNamingStrategyReference() : void
87
    {
88
        $config = [
89
            'doctrine' => [
90
                'configuration' => [
91
                    'test_default' => ['naming_strategy' => 'test_naming_strategy'],
92
                ],
93
            ],
94
        ];
95
        $this->serviceManager->setService('config', $config);
96
        $this->expectException(InvalidArgumentException::class);
97
        $this->factory->createService($this->serviceManager);
98
    }
99
100
    public function testWillInstantiateConfigWithQuoteStrategyObject() : void
101
    {
102
        $quoteStrategy = $this->createMock(QuoteStrategy::class);
103
104
        $config = [
105
            'doctrine' => [
106
                'configuration' => [
107
                    'test_default' => ['quote_strategy' => $quoteStrategy],
108
                ],
109
            ],
110
        ];
111
        $this->serviceManager->setService('config', $config);
112
        $factory   = new ConfigurationFactory('test_default');
113
        $ormConfig = $factory->createService($this->serviceManager);
114
        $this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
115
    }
116
117
    public function testWillInstantiateConfigWithQuoteStrategyReference() : void
118
    {
119
        $quoteStrategy = $this->createMock(QuoteStrategy::class);
120
        $config        = [
121
            'doctrine' => [
122
                'configuration' => [
123
                    'test_default' => ['quote_strategy' => 'test_quote_strategy'],
124
                ],
125
            ],
126
        ];
127
        $this->serviceManager->setService('config', $config);
128
        $this->serviceManager->setService('test_quote_strategy', $quoteStrategy);
129
        $ormConfig = $this->factory->createService($this->serviceManager);
130
        $this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
131
    }
132
133
    public function testWillNotInstantiateConfigWithInvalidQuoteStrategyReference() : void
134
    {
135
        $config = [
136
            'doctrine' => [
137
                'configuration' => [
138
                    'test_default' => ['quote_strategy' => 'test_quote_strategy'],
139
                ],
140
            ],
141
        ];
142
        $this->serviceManager->setService('config', $config);
143
        $this->expectException(InvalidArgumentException::class);
144
        $this->factory->createService($this->serviceManager);
145
    }
146
147
    public function testWillInstantiateConfigWithHydrationCacheSetting() : void
148
    {
149
        $config = [
150
            'doctrine' => [
151
                'configuration' => [
152
                    'test_default' => ['hydration_cache' => 'array'],
153
                ],
154
            ],
155
        ];
156
        $this->serviceManager->setService('config', $config);
157
        $factory   = new ConfigurationFactory('test_default');
158
        $ormConfig = $factory->createService($this->serviceManager);
159
        $this->assertInstanceOf(ArrayCache::class, $ormConfig->getHydrationCacheImpl());
160
    }
161
162
    public function testCanSetDefaultRepositoryClass() : void
163
    {
164
        $config = [
165
            'doctrine' => [
166
                'configuration' => [
167
                    'test_default' => [
168
169
                        'hydration_cache' => 'array',
170
                        'default_repository_class_name' => RepositoryClass::class,
171
                    ],
172
                ],
173
            ],
174
        ];
175
        $this->serviceManager->setService('config', $config);
176
177
        $factory   = new ConfigurationFactory('test_default');
178
        $ormConfig = $factory->createService($this->serviceManager);
179
        $this->assertInstanceOf(ArrayCache::class, $ormConfig->getHydrationCacheImpl());
180
    }
181
182
    public function testAcceptsMetadataFactory() : void
183
    {
184
        $config = [
185
            'doctrine' => [
186
                'configuration' => [
187
                    'test_default' => ['classMetadataFactoryName' => 'Factory'],
188
                ],
189
            ],
190
        ];
191
        $this->serviceManager->setService('config', $config);
192
        $factory   = new ConfigurationFactory('test_default');
193
        $ormConfig = $factory->createService($this->serviceManager);
194
        $this->assertEquals('Factory', $ormConfig->getClassMetadataFactoryName());
195
    }
196
197
    public function testDefaultMetadatFactory() : void
198
    {
199
        $config = [
200
            'doctrine' => [
201
                'configuration' => [
202
                    'test_default' => [],
203
                ],
204
            ],
205
        ];
206
        $this->serviceManager->setService('config', $config);
207
        $factory   = new ConfigurationFactory('test_default');
208
        $ormConfig = $factory->createService($this->serviceManager);
209
        $this->assertEquals(
210
            ClassMetadataFactory::class,
211
            $ormConfig->getClassMetadataFactoryName()
212
        );
213
    }
214
215
    public function testWillInstantiateConfigWithoutEntityListenerResolverSetting() : void
216
    {
217
        $config = [
218
            'doctrine' => [
219
                'configuration' => [
220
                    'test_default' => [],
221
                ],
222
            ],
223
        ];
224
225
        $this->serviceManager->setService('config', $config);
226
227
        $ormConfig = $this->factory->createService($this->serviceManager);
228
229
        $this->assertInstanceOf(
230
            EntityListenerResolver::class,
231
            $ormConfig->getEntityListenerResolver()
232
        );
233
    }
234
235
    public function testWillInstantiateConfigWithEntityListenerResolverObject() : void
236
    {
237
        $entityListenerResolver = $this->createMock(EntityListenerResolver::class);
238
239
        $config = [
240
            'doctrine' => [
241
                'configuration' => [
242
                    'test_default' => ['entity_listener_resolver' => $entityListenerResolver],
243
                ],
244
            ],
245
        ];
246
247
        $this->serviceManager->setService('config', $config);
248
249
        $ormConfig = $this->factory->createService($this->serviceManager);
250
251
        $this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver());
252
    }
253
254
    public function testWillInstantiateConfigWithEntityListenerResolverReference() : void
255
    {
256
        $entityListenerResolver = $this->createMock(EntityListenerResolver::class);
257
258
        $config = [
259
            'doctrine' => [
260
                'configuration' => [
261
                    'test_default' => ['entity_listener_resolver' => 'test_entity_listener_resolver'],
262
                ],
263
            ],
264
        ];
265
266
        $this->serviceManager->setService('config', $config);
267
        $this->serviceManager->setService('test_entity_listener_resolver', $entityListenerResolver);
268
269
        $ormConfig = $this->factory->createService($this->serviceManager);
270
271
        $this->assertSame($entityListenerResolver, $ormConfig->getEntityListenerResolver());
272
    }
273
274
    public function testDoNotCreateSecondLevelCacheByDefault() : void
275
    {
276
        $config = [
277
            'doctrine' => [
278
                'configuration' => [
279
                    'test_default' => [],
280
                ],
281
            ],
282
        ];
283
284
        $this->serviceManager->setService('config', $config);
285
286
        $ormConfig = $this->factory->createService($this->serviceManager);
287
288
        $this->assertNull($ormConfig->getSecondLevelCacheConfiguration());
289
    }
290
291
    public function testCanInstantiateWithSecondLevelCacheConfig() : void
292
    {
293
        $config = [
294
            'doctrine' => [
295
                'configuration' => [
296
                    'test_default' => [
297
                        'result_cache' => 'array',
298
299
                        'second_level_cache' => [
300
                            'enabled'                    => true,
301
                            'default_lifetime'           => 200,
302
                            'default_lock_lifetime'      => 500,
303
                            'file_lock_region_directory' => 'my_dir',
304
305
                            'regions' => [
306
                                'my_first_region' => [
307
                                    'lifetime'      => 800,
308
                                    'lock_lifetime' => 1000,
309
                                ],
310
311
                                'my_second_region' => [
312
                                    'lifetime'      => 10,
313
                                    'lock_lifetime' => 20,
314
                                ],
315
                            ],
316
                        ],
317
                    ],
318
                ],
319
            ],
320
        ];
321
322
        $this->serviceManager->setService('config', $config);
323
324
        $ormConfig        = $this->factory->createService($this->serviceManager);
325
        $secondLevelCache = $ormConfig->getSecondLevelCacheConfiguration();
326
327
        $this->assertInstanceOf(CacheConfiguration::class, $secondLevelCache);
328
329
        $cacheFactory = $secondLevelCache->getCacheFactory();
330
        $this->assertInstanceOf(DefaultCacheFactory::class, $cacheFactory);
331
        $this->assertEquals('my_dir', $cacheFactory->getFileLockRegionDirectory());
332
333
        $regionsConfiguration = $secondLevelCache->getRegionsConfiguration();
334
        $this->assertEquals(200, $regionsConfiguration->getDefaultLifetime());
335
        $this->assertEquals(500, $regionsConfiguration->getDefaultLockLifetime());
336
337
        $this->assertEquals(800, $regionsConfiguration->getLifetime('my_first_region'));
338
        $this->assertEquals(10, $regionsConfiguration->getLifetime('my_second_region'));
339
340
        $this->assertEquals(1000, $regionsConfiguration->getLockLifetime('my_first_region'));
341
        $this->assertEquals(20, $regionsConfiguration->getLockLifetime('my_second_region'));
342
343
        // Doctrine does not allow to retrieve the cache adapter from cache factory, so we are forced to use
344
        // reflection here
345
        $reflProperty = new ReflectionProperty($cacheFactory, 'cache');
346
        $reflProperty->setAccessible(true);
347
        $this->assertInstanceOf(ArrayCache::class, $reflProperty->getValue($cacheFactory));
348
    }
349
}
350