1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Cache; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
9
|
|
|
use Doctrine\ORM\Cache\CacheFactory; |
10
|
|
|
use Doctrine\ORM\Cache\DefaultCacheFactory; |
11
|
|
|
use Doctrine\ORM\Cache\Persister\Collection\CachedCollectionPersister; |
12
|
|
|
use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister; |
13
|
|
|
use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister; |
14
|
|
|
use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister; |
15
|
|
|
use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; |
16
|
|
|
use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister; |
17
|
|
|
use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister; |
18
|
|
|
use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister; |
19
|
|
|
use Doctrine\ORM\Cache\Region\DefaultMultiGetRegion; |
20
|
|
|
use Doctrine\ORM\Cache\Region\DefaultRegion; |
21
|
|
|
use Doctrine\ORM\Cache\RegionsConfiguration; |
22
|
|
|
use Doctrine\ORM\Mapping\CacheMetadata; |
23
|
|
|
use Doctrine\ORM\Mapping\CacheUsage; |
24
|
|
|
use Doctrine\ORM\Persisters\Collection\OneToManyPersister; |
25
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
|
|
|
|
26
|
|
|
use Doctrine\Tests\Mocks\ConcurrentRegionMock; |
27
|
|
|
use Doctrine\Tests\Models\Cache\AttractionContactInfo; |
28
|
|
|
use Doctrine\Tests\Models\Cache\AttractionLocationInfo; |
29
|
|
|
use Doctrine\Tests\Models\Cache\City; |
30
|
|
|
use Doctrine\Tests\Models\Cache\State; |
31
|
|
|
use Doctrine\Tests\OrmTestCase; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @group DDC-2183 |
35
|
|
|
*/ |
36
|
|
|
class DefaultCacheFactoryTest extends OrmTestCase |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var \Doctrine\ORM\Cache\CacheFactory |
40
|
|
|
*/ |
41
|
|
|
private $factory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
45
|
|
|
*/ |
46
|
|
|
private $em; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Doctrine\ORM\Cache\RegionsConfiguration |
50
|
|
|
*/ |
51
|
|
|
private $regionsConfig; |
52
|
|
|
|
53
|
|
|
protected function setUp() |
54
|
|
|
{ |
55
|
|
|
$this->enableSecondLevelCache(); |
56
|
|
|
|
57
|
|
|
parent::setUp(); |
58
|
|
|
|
59
|
|
|
$this->em = $this->getTestEntityManager(); |
60
|
|
|
$this->regionsConfig = new RegionsConfiguration; |
61
|
|
|
|
62
|
|
|
$arguments = [ |
63
|
|
|
$this->regionsConfig, |
64
|
|
|
$this->getSharedSecondLevelCacheDriverImpl() |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->factory = $this->getMockBuilder(DefaultCacheFactory::class) |
|
|
|
|
68
|
|
|
->setMethods(['getRegion']) |
69
|
|
|
->setConstructorArgs($arguments) |
70
|
|
|
->getMock() |
71
|
|
|
; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testImplementsCacheFactory() |
75
|
|
|
{ |
76
|
|
|
self::assertInstanceOf(CacheFactory::class, $this->factory); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testBuildCachedEntityPersisterReadOnly() |
80
|
|
|
{ |
81
|
|
|
$em = $this->em; |
82
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
83
|
|
|
$persister = new BasicEntityPersister($em, $metadata); |
84
|
|
|
$region = new ConcurrentRegionMock( |
85
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$metadata->setCache( |
|
|
|
|
89
|
|
|
new CacheMetadata(CacheUsage::READ_ONLY, 'doctrine_tests_models_cache_state') |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->factory->expects($this->once()) |
|
|
|
|
93
|
|
|
->method('getRegion') |
94
|
|
|
->with($this->equalTo($metadata->getCache())) |
|
|
|
|
95
|
|
|
->will($this->returnValue($region)); |
96
|
|
|
|
97
|
|
|
$cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
|
|
|
|
98
|
|
|
|
99
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
100
|
|
|
self::assertInstanceOf(ReadOnlyCachedEntityPersister::class, $cachedPersister); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testBuildCachedEntityPersisterReadWrite() |
104
|
|
|
{ |
105
|
|
|
$em = $this->em; |
106
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
107
|
|
|
$persister = new BasicEntityPersister($em, $metadata); |
108
|
|
|
$region = new ConcurrentRegionMock( |
109
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$metadata->setCache( |
113
|
|
|
new CacheMetadata(CacheUsage::READ_WRITE, 'doctrine_tests_models_cache_state') |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->factory->expects($this->once()) |
117
|
|
|
->method('getRegion') |
118
|
|
|
->with($this->equalTo($metadata->getCache())) |
119
|
|
|
->will($this->returnValue($region)); |
120
|
|
|
|
121
|
|
|
$cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
|
|
|
|
122
|
|
|
|
123
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
124
|
|
|
self::assertInstanceOf(ReadWriteCachedEntityPersister::class, $cachedPersister); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testBuildCachedEntityPersisterNonStrictReadWrite() |
128
|
|
|
{ |
129
|
|
|
$em = $this->em; |
130
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
131
|
|
|
$persister = new BasicEntityPersister($em, $metadata); |
132
|
|
|
$region = new ConcurrentRegionMock( |
133
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$metadata->setCache( |
137
|
|
|
new CacheMetadata(CacheUsage::NONSTRICT_READ_WRITE, 'doctrine_tests_models_cache_state') |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$this->factory->expects($this->once()) |
141
|
|
|
->method('getRegion') |
142
|
|
|
->with($this->equalTo($metadata->getCache())) |
143
|
|
|
->will($this->returnValue($region)); |
144
|
|
|
|
145
|
|
|
$cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
|
|
|
|
146
|
|
|
|
147
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
148
|
|
|
self::assertInstanceOf(NonStrictReadWriteCachedEntityPersister::class, $cachedPersister); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
public function testBuildCachedCollectionPersisterReadOnly() |
152
|
|
|
{ |
153
|
|
|
$em = $this->em; |
154
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
155
|
|
|
$association = $metadata->getProperty('cities'); |
|
|
|
|
156
|
|
|
$persister = new OneToManyPersister($em); |
157
|
|
|
$region = new ConcurrentRegionMock( |
158
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$association->setCache( |
162
|
|
|
new CacheMetadata(CacheUsage::READ_ONLY, 'doctrine_tests_models_cache_state__cities') |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$this->factory->expects($this->once()) |
166
|
|
|
->method('getRegion') |
167
|
|
|
->with($this->equalTo($association->getCache())) |
168
|
|
|
->will($this->returnValue($region)); |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
172
|
|
|
|
173
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
174
|
|
|
self::assertInstanceOf(ReadOnlyCachedCollectionPersister::class, $cachedPersister); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
public function testBuildCachedCollectionPersisterReadWrite() |
178
|
|
|
{ |
179
|
|
|
$em = $this->em; |
180
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
181
|
|
|
$association = $metadata->getProperty('cities'); |
182
|
|
|
$persister = new OneToManyPersister($em); |
183
|
|
|
$region = new ConcurrentRegionMock( |
184
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$association->setCache( |
188
|
|
|
new CacheMetadata(CacheUsage::READ_WRITE, 'doctrine_tests_models_cache_state__cities') |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$this->factory->expects($this->once()) |
192
|
|
|
->method('getRegion') |
193
|
|
|
->with($this->equalTo($association->getCache())) |
194
|
|
|
->will($this->returnValue($region)); |
195
|
|
|
|
196
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
197
|
|
|
|
198
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
199
|
|
|
self::assertInstanceOf(ReadWriteCachedCollectionPersister::class, $cachedPersister); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
public function testBuildCachedCollectionPersisterNonStrictReadWrite() |
203
|
|
|
{ |
204
|
|
|
$em = $this->em; |
205
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
206
|
|
|
$association = $metadata->getProperty('cities'); |
207
|
|
|
$persister = new OneToManyPersister($em); |
208
|
|
|
$region = new ConcurrentRegionMock( |
209
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$association->setCache( |
213
|
|
|
new CacheMetadata(CacheUsage::NONSTRICT_READ_WRITE, 'doctrine_tests_models_cache_state__cities') |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
$this->factory->expects($this->once()) |
217
|
|
|
->method('getRegion') |
218
|
|
|
->with($this->equalTo($association->getCache())) |
219
|
|
|
->will($this->returnValue($region)); |
220
|
|
|
|
221
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
222
|
|
|
|
223
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
224
|
|
|
self::assertInstanceOf(NonStrictReadWriteCachedCollectionPersister::class, $cachedPersister); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
public function testInheritedEntityCacheRegion() |
228
|
|
|
{ |
229
|
|
|
$em = $this->em; |
230
|
|
|
$metadata1 = clone $em->getClassMetadata(AttractionContactInfo::class); |
231
|
|
|
$metadata2 = clone $em->getClassMetadata(AttractionLocationInfo::class); |
232
|
|
|
$persister1 = new BasicEntityPersister($em, $metadata1); |
233
|
|
|
$persister2 = new BasicEntityPersister($em, $metadata2); |
234
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
235
|
|
|
|
236
|
|
|
$cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
|
|
|
|
237
|
|
|
$cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
238
|
|
|
|
239
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
240
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
241
|
|
|
|
242
|
|
|
self::assertNotSame($cachedPersister1, $cachedPersister2); |
243
|
|
|
self::assertSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
View Code Duplication |
public function testCreateNewCacheDriver() |
247
|
|
|
{ |
248
|
|
|
$em = $this->em; |
249
|
|
|
$metadata1 = clone $em->getClassMetadata(State::class); |
250
|
|
|
$metadata2 = clone $em->getClassMetadata(City::class); |
251
|
|
|
$persister1 = new BasicEntityPersister($em, $metadata1); |
252
|
|
|
$persister2 = new BasicEntityPersister($em, $metadata2); |
253
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
254
|
|
|
|
255
|
|
|
$cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
|
|
|
|
256
|
|
|
$cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
257
|
|
|
|
258
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
259
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
260
|
|
|
|
261
|
|
|
self::assertNotSame($cachedPersister1, $cachedPersister2); |
262
|
|
|
self::assertNotSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
View Code Duplication |
public function testBuildCachedEntityPersisterNonStrictException() |
266
|
|
|
{ |
267
|
|
|
$em = $this->em; |
268
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
269
|
|
|
$persister = new BasicEntityPersister($em, $metadata); |
270
|
|
|
|
271
|
|
|
$metadata->setCache( |
272
|
|
|
new CacheMetadata('-1', 'doctrine_tests_models_cache_state') |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
$this->expectException(\InvalidArgumentException::class); |
276
|
|
|
$this->expectExceptionMessage('Unrecognized access strategy type [-1]'); |
277
|
|
|
|
278
|
|
|
$this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
View Code Duplication |
public function testBuildCachedCollectionPersisterException() |
282
|
|
|
{ |
283
|
|
|
$em = $this->em; |
284
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
285
|
|
|
$association = $metadata->getProperty('cities'); |
286
|
|
|
$persister = new OneToManyPersister($em); |
287
|
|
|
|
288
|
|
|
$association->setCache( |
289
|
|
|
new CacheMetadata('-1', 'doctrine_tests_models_cache_state__cities') |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
$this->expectException(\InvalidArgumentException::class); |
293
|
|
|
$this->expectExceptionMessage('Unrecognized access strategy type [-1]'); |
294
|
|
|
|
295
|
|
|
$this->factory->buildCachedCollectionPersister($em, $persister, $association); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
View Code Duplication |
public function testInvalidFileLockRegionDirectoryException() |
299
|
|
|
{ |
300
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
301
|
|
|
|
302
|
|
|
$this->expectException(\LogicException::class); |
303
|
|
|
$this->expectExceptionMessage( |
304
|
|
|
'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" ' |
305
|
|
|
. 'is required, The default implementation provided by doctrine is ' |
306
|
|
|
. '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory' |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_WRITE, 'foo'); |
310
|
|
|
$factory->getRegion($fooCache); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
View Code Duplication |
public function testInvalidFileLockRegionDirectoryExceptionWithEmptyString() |
314
|
|
|
{ |
315
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
316
|
|
|
|
317
|
|
|
$factory->setFileLockRegionDirectory(''); |
318
|
|
|
|
319
|
|
|
$this->expectException(\LogicException::class); |
320
|
|
|
$this->expectExceptionMessage( |
321
|
|
|
'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" ' |
322
|
|
|
. 'is required, The default implementation provided by doctrine is ' |
323
|
|
|
. '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory' |
324
|
|
|
); |
325
|
|
|
|
326
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_WRITE, 'foo'); |
327
|
|
|
$factory->getRegion($fooCache); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function testBuildsNewNamespacedCacheInstancePerRegionInstance() |
331
|
|
|
{ |
332
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
333
|
|
|
|
334
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_ONLY, 'foo'); |
335
|
|
|
$fooRegion = $factory->getRegion($fooCache); |
336
|
|
|
|
337
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
338
|
|
|
$barRegion = $factory->getRegion($barCache); |
339
|
|
|
|
340
|
|
|
self::assertSame('foo', $fooRegion->getCache()->getNamespace()); |
|
|
|
|
341
|
|
|
self::assertSame('bar', $barRegion->getCache()->getNamespace()); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet() |
345
|
|
|
{ |
346
|
|
|
$cache = clone $this->getSharedSecondLevelCacheDriverImpl(); |
347
|
|
|
$cache->setNamespace('testing'); |
348
|
|
|
|
349
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
350
|
|
|
|
351
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_ONLY, 'foo'); |
352
|
|
|
$fooRegion = $factory->getRegion($fooCache); |
353
|
|
|
|
354
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
355
|
|
|
$barRegion = $factory->getRegion($barCache); |
356
|
|
|
|
357
|
|
|
self::assertSame('testing:foo', $fooRegion->getCache()->getNamespace()); |
358
|
|
|
self::assertSame('testing:bar', $barRegion->getCache()->getNamespace()); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
View Code Duplication |
public function testBuildsDefaultCacheRegionFromGenericCacheRegion() |
362
|
|
|
{ |
363
|
|
|
/* @var $cache \Doctrine\Common\Cache\Cache */ |
364
|
|
|
$cache = $this->createMock(Cache::class); |
365
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
366
|
|
|
|
367
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
368
|
|
|
$barRegion = $factory->getRegion($barCache); |
369
|
|
|
|
370
|
|
|
self::assertInstanceOf(DefaultRegion::class, $barRegion); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
View Code Duplication |
public function testBuildsMultiGetCacheRegionFromGenericCacheRegion() |
374
|
|
|
{ |
375
|
|
|
/* @var $cache \Doctrine\Common\Cache\CacheProvider */ |
376
|
|
|
$cache = $this->getMockForAbstractClass(CacheProvider::class); |
377
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
378
|
|
|
|
379
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
380
|
|
|
$barRegion = $factory->getRegion($barCache); |
381
|
|
|
|
382
|
|
|
self::assertInstanceOf(DefaultMultiGetRegion::class, $barRegion); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths