Test Failed
Pull Request — master (#6646)
by Jocelyn
66:22
created

testInvalidNamespaceVersionCacheEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Cache;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\ORM\Cache\CacheFactory;
8
use Doctrine\ORM\Cache\DefaultCacheFactory;
9
use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister;
10
use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister;
11
use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister;
12
use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister;
13
use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister;
14
use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister;
15
use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister;
16
use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister;
17
use Doctrine\ORM\Cache\Region\DefaultMultiGetRegion;
18
use Doctrine\ORM\Cache\Region\DefaultRegion;
19
use Doctrine\ORM\Cache\RegionsConfiguration;
20
use Doctrine\ORM\Mapping\ClassMetadata;
21
use Doctrine\ORM\Persisters\Collection\OneToManyPersister;
22
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
23
use Doctrine\Tests\Mocks\ConcurrentRegionMock;
24
use Doctrine\Tests\Models\Cache\AttractionContactInfo;
25
use Doctrine\Tests\Models\Cache\AttractionLocationInfo;
26
use Doctrine\Tests\Models\Cache\City;
27
use Doctrine\Tests\Models\Cache\State;
28
use Doctrine\Tests\OrmTestCase;
29
30
/**
31
 * @group DDC-2183
32
 */
33
class DefaultCacheFactoryTest extends OrmTestCase
34
{
35
    /**
36
     * @var \Doctrine\ORM\Cache\CacheFactory
37
     */
38
    private $factory;
39
40
    /**
41
     * @var \Doctrine\ORM\EntityManager
42
     */
43
    private $em;
44
45
    /**
46
     * @var \Doctrine\ORM\Cache\RegionsConfiguration
47
     */
48
    private $regionsConfig;
49
50
    protected function setUp()
51
    {
52
        $this->enableSecondLevelCache();
53
        parent::setUp();
54
55
        $this->em            = $this->_getTestEntityManager();
56
        $this->regionsConfig = new RegionsConfiguration;
57
        $arguments           = [$this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()];
58
        $this->factory       = $this->getMockBuilder(DefaultCacheFactory::class)
59
                                    ->setMethods(['getRegion'])
60
                                    ->setConstructorArgs($arguments)
61
                                    ->getMock();
62
    }
63
64
    public function testImplementsCacheFactory()
65
    {
66
        $this->assertInstanceOf(CacheFactory::class, $this->factory);
67
    }
68
69 View Code Duplication
    public function testBuildCachedEntityPersisterReadOnly()
70
    {
71
        $em        = $this->em;
72
        $metadata  = clone $em->getClassMetadata(State::class);
73
        $persister = new BasicEntityPersister($em, $metadata);
74
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
75
76
        $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY;
77
78
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
            ->method('getRegion')
80
            ->with($this->equalTo($metadata->cache))
81
            ->will($this->returnValue($region));
82
83
        $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata);
84
85
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister);
86
        $this->assertInstanceOf(ReadOnlyCachedEntityPersister::class, $cachedPersister);
87
    }
88
89 View Code Duplication
    public function testBuildCachedEntityPersisterReadWrite()
90
    {
91
        $em        = $this->em;
92
        $metadata  = clone $em->getClassMetadata(State::class);
93
        $persister = new BasicEntityPersister($em, $metadata);
94
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
95
96
        $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE;
97
98
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            ->method('getRegion')
100
            ->with($this->equalTo($metadata->cache))
101
            ->will($this->returnValue($region));
102
103
        $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata);
104
105
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister);
106
        $this->assertInstanceOf(ReadWriteCachedEntityPersister::class, $cachedPersister);
107
    }
108
109 View Code Duplication
    public function testBuildCachedEntityPersisterNonStrictReadWrite()
110
    {
111
        $em        = $this->em;
112
        $metadata  = clone $em->getClassMetadata(State::class);
113
        $persister = new BasicEntityPersister($em, $metadata);
114
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
115
116
        $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE;
117
118
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
            ->method('getRegion')
120
            ->with($this->equalTo($metadata->cache))
121
            ->will($this->returnValue($region));
122
123
        $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata);
124
125
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister);
126
        $this->assertInstanceOf(NonStrictReadWriteCachedEntityPersister::class, $cachedPersister);
127
    }
128
129 View Code Duplication
    public function testBuildCachedCollectionPersisterReadOnly()
130
    {
131
        $em        = $this->em;
132
        $metadata  = $em->getClassMetadata(State::class);
133
        $mapping   = $metadata->associationMappings['cities'];
134
        $persister = new OneToManyPersister($em);
135
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
136
137
        $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY;
138
139
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
            ->method('getRegion')
141
            ->with($this->equalTo($mapping['cache']))
142
            ->will($this->returnValue($region));
143
144
145
        $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping);
146
147
        $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister);
148
        $this->assertInstanceOf(ReadOnlyCachedCollectionPersister::class, $cachedPersister);
149
    }
150
151 View Code Duplication
    public function testBuildCachedCollectionPersisterReadWrite()
152
    {
153
        $em        = $this->em;
154
        $metadata  = $em->getClassMetadata(State::class);
155
        $mapping   = $metadata->associationMappings['cities'];
156
        $persister = new OneToManyPersister($em);
157
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
158
159
        $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE;
160
161
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
            ->method('getRegion')
163
            ->with($this->equalTo($mapping['cache']))
164
            ->will($this->returnValue($region));
165
166
        $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping);
167
168
        $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister);
169
        $this->assertInstanceOf(ReadWriteCachedCollectionPersister::class, $cachedPersister);
170
    }
171
172 View Code Duplication
    public function testBuildCachedCollectionPersisterNonStrictReadWrite()
173
    {
174
        $em        = $this->em;
175
        $metadata  = $em->getClassMetadata(State::class);
176
        $mapping   = $metadata->associationMappings['cities'];
177
        $persister = new OneToManyPersister($em);
178
        $region    = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()));
179
180
        $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE;
181
182
        $this->factory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\ORM\Cache\CacheFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
183
            ->method('getRegion')
184
            ->with($this->equalTo($mapping['cache']))
185
            ->will($this->returnValue($region));
186
187
        $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping);
188
189
        $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister);
190
        $this->assertInstanceOf(NonStrictReadWriteCachedCollectionPersister::class, $cachedPersister);
191
    }
192
193 View Code Duplication
    public function testInheritedEntityCacheRegion()
194
    {
195
        $em         = $this->em;
196
        $metadata1  = clone $em->getClassMetadata(AttractionContactInfo::class);
197
        $metadata2  = clone $em->getClassMetadata(AttractionLocationInfo::class);
198
        $persister1 = new BasicEntityPersister($em, $metadata1);
199
        $persister2 = new BasicEntityPersister($em, $metadata2);
200
        $factory    = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
201
202
        $cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1);
203
        $cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2);
204
205
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister1);
206
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister2);
207
208
        $this->assertNotSame($cachedPersister1, $cachedPersister2);
209
        $this->assertSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion());
210
    }
211
212 View Code Duplication
    public function testCreateNewCacheDriver()
213
    {
214
        $em         = $this->em;
215
        $metadata1  = clone $em->getClassMetadata(State::class);
216
        $metadata2  = clone $em->getClassMetadata(City::class);
217
        $persister1 = new BasicEntityPersister($em, $metadata1);
218
        $persister2 = new BasicEntityPersister($em, $metadata2);
219
        $factory    = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
220
221
        $cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1);
222
        $cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2);
223
224
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister1);
225
        $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister2);
226
227
        $this->assertNotSame($cachedPersister1, $cachedPersister2);
228
        $this->assertNotSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion());
229
    }
230
231
    public function testBuildCachedEntityPersisterNonStrictException()
232
    {
233
        $em        = $this->em;
234
        $metadata  = clone $em->getClassMetadata(State::class);
235
        $persister = new BasicEntityPersister($em, $metadata);
236
237
        $metadata->cache['usage'] = -1;
238
239
        $this->expectException(\InvalidArgumentException::class);
240
        $this->expectExceptionMessage('Unrecognized access strategy type [-1]');
241
242
        $this->factory->buildCachedEntityPersister($em, $persister, $metadata);
243
    }
244
245
    public function testBuildCachedCollectionPersisterException()
246
    {
247
        $em        = $this->em;
248
        $metadata  = $em->getClassMetadata(State::class);
249
        $mapping   = $metadata->associationMappings['cities'];
250
        $persister = new OneToManyPersister($em);
251
252
        $mapping['cache']['usage'] = -1;
253
254
        $this->expectException(\InvalidArgumentException::class);
255
        $this->expectExceptionMessage('Unrecognized access strategy type [-1]');
256
257
        $this->factory->buildCachedCollectionPersister($em, $persister, $mapping);
258
    }
259
260 View Code Duplication
    public function testInvalidFileLockRegionDirectoryException()
261
    {
262
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
263
264
        $this->expectException(\LogicException::class);
265
        $this->expectExceptionMessage(
266
            'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" '
267
            . 'is required, The default implementation provided by doctrine is '
268
            . '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory'
269
        );
270
271
        $factory->getRegion(
272
            [
273
                'usage'   => ClassMetadata::CACHE_USAGE_READ_WRITE,
274
                'region'  => 'foo'
275
            ]
276
        );
277
    }
278
279 View Code Duplication
    public function testInvalidFileLockRegionDirectoryExceptionWithEmptyString()
280
    {
281
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
282
283
        $factory->setFileLockRegionDirectory('');
284
285
        $this->expectException(\LogicException::class);
286
        $this->expectExceptionMessage(
287
            'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" '
288
            . 'is required, The default implementation provided by doctrine is '
289
            . '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory'
290
        );
291
292
        $factory->getRegion(
293
            [
294
                'usage'   => ClassMetadata::CACHE_USAGE_READ_WRITE,
295
                'region'  => 'foo'
296
            ]
297
        );
298
    }
299
300
    public function testBuildsNewNamespacedCacheInstancePerRegionInstance()
301
    {
302
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
303
304
        $fooRegion = $factory->getRegion(
305
            [
306
                'region' => 'foo',
307
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
308
            ]
309
        );
310
        $barRegion = $factory->getRegion(
311
            [
312
                'region' => 'bar',
313
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
314
            ]
315
        );
316
317
        $this->assertSame('foo', $fooRegion->getCache()->getNamespace());
318
        $this->assertSame('bar', $barRegion->getCache()->getNamespace());
319
    }
320
321
    public function testInvalidNamespaceVersionCacheEntry()
322
    {
323
        $cache = clone $this->getSharedSecondLevelCacheDriverImpl();
324
        $reflexion = new \ReflectionClass('Doctrine\Common\Cache\ArrayCache');
325
        $doSave = $reflexion->getMethod('doSave');
326
        $doSave->setAccessible(true);
327
328
        $doctrineKey = sprintf(CacheProvider::DOCTRINE_NAMESPACE_CACHEKEY, '');
329
        $doSave->invokeArgs($cache, array($doctrineKey, 'corruptedStringKey'));
330
331
        // deleteAll call should succeed
332
        $this->assertEquals(true, $cache->deleteAll());
0 ignored issues
show
Bug introduced by
The method deleteAll() does not exist on Doctrine\Common\Cache\Cache. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
333
    }
334
335
    public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet()
336
    {
337
        $cache = clone $this->getSharedSecondLevelCacheDriverImpl();
338
        $cache->setNamespace('testing');
339
340
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
341
342
        $fooRegion = $factory->getRegion(
343
            [
344
                'region' => 'foo',
345
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
346
            ]
347
        );
348
        $barRegion = $factory->getRegion(
349
            [
350
                'region' => 'bar',
351
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
352
            ]
353
        );
354
355
        $this->assertSame('testing:foo', $fooRegion->getCache()->getNamespace());
356
        $this->assertSame('testing:bar', $barRegion->getCache()->getNamespace());
357
    }
358
359 View Code Duplication
    public function testBuildsDefaultCacheRegionFromGenericCacheRegion()
360
    {
361
        /* @var $cache \Doctrine\Common\Cache\Cache */
362
        $cache = $this->createMock(Cache::class);
363
364
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
365
366
        $this->assertInstanceOf(
367
            DefaultRegion::class,
368
            $factory->getRegion(
369
                [
370
                    'region' => 'bar',
371
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
372
                ]
373
            )
374
        );
375
    }
376
377 View Code Duplication
    public function testBuildsMultiGetCacheRegionFromGenericCacheRegion()
378
    {
379
        /* @var $cache \Doctrine\Common\Cache\CacheProvider */
380
        $cache = $this->getMockForAbstractClass(CacheProvider::class);
381
382
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
383
384
        $this->assertInstanceOf(
385
            DefaultMultiGetRegion::class,
386
            $factory->getRegion(
387
                [
388
                    'region' => 'bar',
389
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
390
                ]
391
            )
392
        );
393
    }
394
395
}
396