Passed
Push — master ( fda770...94640a )
by Marco
23:51 queued 14:09
created

testInvalidFileLockRegionDirectoryExceptionWithEmptyString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
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
    /**
232
     * @expectedException InvalidArgumentException
233
     * @expectedExceptionMessage Unrecognized access strategy type [-1]
234
     */
235
    public function testBuildCachedEntityPersisterNonStrictException()
236
    {
237
        $em        = $this->em;
238
        $metadata  = clone $em->getClassMetadata(State::class);
239
        $persister = new BasicEntityPersister($em, $metadata);
240
241
        $metadata->cache['usage'] = -1;
242
243
        $this->factory->buildCachedEntityPersister($em, $persister, $metadata);
244
    }
245
246
    /**
247
     * @expectedException InvalidArgumentException
248
     * @expectedExceptionMessage Unrecognized access strategy type [-1]
249
     */
250
    public function testBuildCachedCollectionPersisterException()
251
    {
252
        $em        = $this->em;
253
        $metadata  = $em->getClassMetadata(State::class);
254
        $mapping   = $metadata->associationMappings['cities'];
255
        $persister = new OneToManyPersister($em);
256
257
        $mapping['cache']['usage'] = -1;
258
259
        $this->factory->buildCachedCollectionPersister($em, $persister, $mapping);
260
    }
261
262
    /**
263
     * @expectedException LogicException
264
     * @expectedExceptionMessage If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory
265
     */
266 View Code Duplication
    public function testInvalidFileLockRegionDirectoryException()
267
    {
268
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
269
270
        $factory->getRegion(
271
            [
272
                'usage'   => ClassMetadata::CACHE_USAGE_READ_WRITE,
273
                'region'  => 'foo'
274
            ]
275
        );
276
    }
277
278
    /**
279
     * @expectedException LogicException
280
     * @expectedExceptionMessage If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory
281
     */
282 View Code Duplication
    public function testInvalidFileLockRegionDirectoryExceptionWithEmptyString()
283
    {
284
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
285
286
        $factory->setFileLockRegionDirectory('');
287
288
        $factory->getRegion(
289
            [
290
                'usage'   => ClassMetadata::CACHE_USAGE_READ_WRITE,
291
                'region'  => 'foo'
292
            ]
293
        );
294
    }
295
296
    public function testBuildsNewNamespacedCacheInstancePerRegionInstance()
297
    {
298
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
299
300
        $fooRegion = $factory->getRegion(
301
            [
302
                'region' => 'foo',
303
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
304
            ]
305
        );
306
        $barRegion = $factory->getRegion(
307
            [
308
                'region' => 'bar',
309
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
310
            ]
311
        );
312
313
        $this->assertSame('foo', $fooRegion->getCache()->getNamespace());
314
        $this->assertSame('bar', $barRegion->getCache()->getNamespace());
315
    }
316
317
    public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet()
318
    {
319
        $cache = clone $this->getSharedSecondLevelCacheDriverImpl();
320
        $cache->setNamespace('testing');
321
322
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
323
324
        $fooRegion = $factory->getRegion(
325
            [
326
                'region' => 'foo',
327
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
328
            ]
329
        );
330
        $barRegion = $factory->getRegion(
331
            [
332
                'region' => 'bar',
333
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
334
            ]
335
        );
336
337
        $this->assertSame('testing:foo', $fooRegion->getCache()->getNamespace());
338
        $this->assertSame('testing:bar', $barRegion->getCache()->getNamespace());
339
    }
340
341 View Code Duplication
    public function testBuildsDefaultCacheRegionFromGenericCacheRegion()
342
    {
343
        /* @var $cache \Doctrine\Common\Cache\Cache */
344
        $cache = $this->createMock(Cache::class);
345
346
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
347
348
        $this->assertInstanceOf(
349
            DefaultRegion::class,
350
            $factory->getRegion(
351
                [
352
                    'region' => 'bar',
353
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
354
                ]
355
            )
356
        );
357
    }
358
359 View Code Duplication
    public function testBuildsMultiGetCacheRegionFromGenericCacheRegion()
360
    {
361
        /* @var $cache \Doctrine\Common\Cache\CacheProvider */
362
        $cache = $this->getMockForAbstractClass(CacheProvider::class);
363
364
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
365
366
        $this->assertInstanceOf(
367
            DefaultMultiGetRegion::class,
368
            $factory->getRegion(
369
                [
370
                    'region' => 'bar',
371
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
372
                ]
373
            )
374
        );
375
    }
376
377
}
378