Completed
Push — master ( 4e5730...4e304d )
by Marco
17s
created

testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 12
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
    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())
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
    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())
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
    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())
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
    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())
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
    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())
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
    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())
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
    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
    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
    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
    public function testBuildsNewNamespacedCacheInstancePerRegionInstance()
279
    {
280
        $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl());
281
282
        $fooRegion = $factory->getRegion(
283
            [
284
                'region' => 'foo',
285
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
286
            ]
287
        );
288
        $barRegion = $factory->getRegion(
289
            [
290
                'region' => 'bar',
291
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
292
            ]
293
        );
294
295
        $this->assertSame('foo', $fooRegion->getCache()->getNamespace());
296
        $this->assertSame('bar', $barRegion->getCache()->getNamespace());
297
    }
298
299
    public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet()
300
    {
301
        $cache = clone $this->getSharedSecondLevelCacheDriverImpl();
302
        $cache->setNamespace('testing');
303
304
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
305
306
        $fooRegion = $factory->getRegion(
307
            [
308
                'region' => 'foo',
309
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
310
            ]
311
        );
312
        $barRegion = $factory->getRegion(
313
            [
314
                'region' => 'bar',
315
                'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
316
            ]
317
        );
318
319
        $this->assertSame('testing:foo', $fooRegion->getCache()->getNamespace());
320
        $this->assertSame('testing:bar', $barRegion->getCache()->getNamespace());
321
    }
322
323
    public function testBuildsDefaultCacheRegionFromGenericCacheRegion()
324
    {
325
        /* @var $cache \Doctrine\Common\Cache\Cache */
326
        $cache = $this->createMock(Cache::class);
327
328
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
329
330
        $this->assertInstanceOf(
331
            DefaultRegion::class,
332
            $factory->getRegion(
333
                [
334
                    'region' => 'bar',
335
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
336
                ]
337
            )
338
        );
339
    }
340
341
    public function testBuildsMultiGetCacheRegionFromGenericCacheRegion()
342
    {
343
        /* @var $cache \Doctrine\Common\Cache\CacheProvider */
344
        $cache = $this->getMockForAbstractClass(CacheProvider::class);
345
346
        $factory = new DefaultCacheFactory($this->regionsConfig, $cache);
347
348
        $this->assertInstanceOf(
349
            DefaultMultiGetRegion::class,
350
            $factory->getRegion(
351
                [
352
                    'region' => 'bar',
353
                    'usage'  => ClassMetadata::CACHE_USAGE_READ_ONLY,
354
                ]
355
            )
356
        );
357
    }
358
359
}
360