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
|
|
|
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
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
171
|
|
|
|
172
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
173
|
|
|
self::assertInstanceOf(ReadOnlyCachedCollectionPersister::class, $cachedPersister); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testBuildCachedCollectionPersisterReadWrite() |
177
|
|
|
{ |
178
|
|
|
$em = $this->em; |
179
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
180
|
|
|
$association = $metadata->getProperty('cities'); |
181
|
|
|
$persister = new OneToManyPersister($em); |
182
|
|
|
$region = new ConcurrentRegionMock( |
183
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$association->setCache( |
187
|
|
|
new CacheMetadata(CacheUsage::READ_WRITE, 'doctrine_tests_models_cache_state__cities') |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->factory->expects($this->once()) |
191
|
|
|
->method('getRegion') |
192
|
|
|
->with($this->equalTo($association->getCache())) |
193
|
|
|
->will($this->returnValue($region)); |
194
|
|
|
|
195
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
196
|
|
|
|
197
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
198
|
|
|
self::assertInstanceOf(ReadWriteCachedCollectionPersister::class, $cachedPersister); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testBuildCachedCollectionPersisterNonStrictReadWrite() |
202
|
|
|
{ |
203
|
|
|
$em = $this->em; |
204
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
205
|
|
|
$association = $metadata->getProperty('cities'); |
206
|
|
|
$persister = new OneToManyPersister($em); |
207
|
|
|
$region = new ConcurrentRegionMock( |
208
|
|
|
new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl()) |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$association->setCache( |
212
|
|
|
new CacheMetadata(CacheUsage::NONSTRICT_READ_WRITE, 'doctrine_tests_models_cache_state__cities') |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
$this->factory->expects($this->once()) |
216
|
|
|
->method('getRegion') |
217
|
|
|
->with($this->equalTo($association->getCache())) |
218
|
|
|
->will($this->returnValue($region)); |
219
|
|
|
|
220
|
|
|
$cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $association); |
221
|
|
|
|
222
|
|
|
self::assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
223
|
|
|
self::assertInstanceOf(NonStrictReadWriteCachedCollectionPersister::class, $cachedPersister); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testInheritedEntityCacheRegion() |
227
|
|
|
{ |
228
|
|
|
$em = $this->em; |
229
|
|
|
$metadata1 = clone $em->getClassMetadata(AttractionContactInfo::class); |
230
|
|
|
$metadata2 = clone $em->getClassMetadata(AttractionLocationInfo::class); |
231
|
|
|
$persister1 = new BasicEntityPersister($em, $metadata1); |
|
|
|
|
232
|
|
|
$persister2 = new BasicEntityPersister($em, $metadata2); |
233
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
234
|
|
|
|
235
|
|
|
$cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
|
|
|
|
236
|
|
|
$cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
237
|
|
|
|
238
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
239
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
240
|
|
|
|
241
|
|
|
self::assertNotSame($cachedPersister1, $cachedPersister2); |
242
|
|
|
self::assertSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testCreateNewCacheDriver() |
246
|
|
|
{ |
247
|
|
|
$em = $this->em; |
248
|
|
|
$metadata1 = clone $em->getClassMetadata(State::class); |
249
|
|
|
$metadata2 = clone $em->getClassMetadata(City::class); |
250
|
|
|
$persister1 = new BasicEntityPersister($em, $metadata1); |
|
|
|
|
251
|
|
|
$persister2 = new BasicEntityPersister($em, $metadata2); |
252
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
253
|
|
|
|
254
|
|
|
$cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
|
|
|
|
255
|
|
|
$cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
256
|
|
|
|
257
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
258
|
|
|
self::assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
259
|
|
|
|
260
|
|
|
self::assertNotSame($cachedPersister1, $cachedPersister2); |
261
|
|
|
self::assertNotSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testBuildCachedEntityPersisterNonStrictException() |
265
|
|
|
{ |
266
|
|
|
$em = $this->em; |
267
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
268
|
|
|
$persister = new BasicEntityPersister($em, $metadata); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$metadata->setCache( |
271
|
|
|
new CacheMetadata('-1', 'doctrine_tests_models_cache_state') |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$this->expectException(\InvalidArgumentException::class); |
275
|
|
|
$this->expectExceptionMessage('Unrecognized access strategy type [-1]'); |
276
|
|
|
|
277
|
|
|
$this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testBuildCachedCollectionPersisterException() |
281
|
|
|
{ |
282
|
|
|
$em = $this->em; |
283
|
|
|
$metadata = clone $em->getClassMetadata(State::class); |
284
|
|
|
$association = $metadata->getProperty('cities'); |
285
|
|
|
$persister = new OneToManyPersister($em); |
286
|
|
|
|
287
|
|
|
$association->setCache( |
288
|
|
|
new CacheMetadata('-1', 'doctrine_tests_models_cache_state__cities') |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
$this->expectException(\InvalidArgumentException::class); |
292
|
|
|
$this->expectExceptionMessage('Unrecognized access strategy type [-1]'); |
293
|
|
|
|
294
|
|
|
$this->factory->buildCachedCollectionPersister($em, $persister, $association); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testInvalidFileLockRegionDirectoryException() |
298
|
|
|
{ |
299
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
300
|
|
|
|
301
|
|
|
$this->expectException(\LogicException::class); |
302
|
|
|
$this->expectExceptionMessage( |
303
|
|
|
'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" ' |
304
|
|
|
. 'is required, The default implementation provided by doctrine is ' |
305
|
|
|
. '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory' |
306
|
|
|
); |
307
|
|
|
|
308
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_WRITE, 'foo'); |
309
|
|
|
$factory->getRegion($fooCache); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testInvalidFileLockRegionDirectoryExceptionWithEmptyString() |
313
|
|
|
{ |
314
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
315
|
|
|
|
316
|
|
|
$factory->setFileLockRegionDirectory(''); |
317
|
|
|
|
318
|
|
|
$this->expectException(\LogicException::class); |
319
|
|
|
$this->expectExceptionMessage( |
320
|
|
|
'If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" ' |
321
|
|
|
. 'is required, The default implementation provided by doctrine is ' |
322
|
|
|
. '"Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory' |
323
|
|
|
); |
324
|
|
|
|
325
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_WRITE, 'foo'); |
326
|
|
|
$factory->getRegion($fooCache); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function testBuildsNewNamespacedCacheInstancePerRegionInstance() |
330
|
|
|
{ |
331
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
332
|
|
|
|
333
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_ONLY, 'foo'); |
334
|
|
|
$fooRegion = $factory->getRegion($fooCache); |
335
|
|
|
|
336
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
337
|
|
|
$barRegion = $factory->getRegion($barCache); |
338
|
|
|
|
339
|
|
|
self::assertSame('foo', $fooRegion->getCache()->getNamespace()); |
|
|
|
|
340
|
|
|
self::assertSame('bar', $barRegion->getCache()->getNamespace()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet() |
344
|
|
|
{ |
345
|
|
|
$cache = clone $this->getSharedSecondLevelCacheDriverImpl(); |
346
|
|
|
$cache->setNamespace('testing'); |
347
|
|
|
|
348
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
349
|
|
|
|
350
|
|
|
$fooCache = new CacheMetadata(CacheUsage::READ_ONLY, 'foo'); |
351
|
|
|
$fooRegion = $factory->getRegion($fooCache); |
352
|
|
|
|
353
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
354
|
|
|
$barRegion = $factory->getRegion($barCache); |
355
|
|
|
|
356
|
|
|
self::assertSame('testing:foo', $fooRegion->getCache()->getNamespace()); |
357
|
|
|
self::assertSame('testing:bar', $barRegion->getCache()->getNamespace()); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function testBuildsDefaultCacheRegionFromGenericCacheRegion() |
361
|
|
|
{ |
362
|
|
|
/* @var $cache \Doctrine\Common\Cache\Cache */ |
363
|
|
|
$cache = $this->createMock(Cache::class); |
364
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
365
|
|
|
|
366
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
367
|
|
|
$barRegion = $factory->getRegion($barCache); |
368
|
|
|
|
369
|
|
|
self::assertInstanceOf(DefaultRegion::class, $barRegion); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function testBuildsMultiGetCacheRegionFromGenericCacheRegion() |
373
|
|
|
{ |
374
|
|
|
/* @var $cache \Doctrine\Common\Cache\CacheProvider */ |
375
|
|
|
$cache = $this->getMockForAbstractClass(CacheProvider::class); |
376
|
|
|
$factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
377
|
|
|
|
378
|
|
|
$barCache = new CacheMetadata(CacheUsage::READ_ONLY, 'bar'); |
379
|
|
|
$barRegion = $factory->getRegion($barCache); |
380
|
|
|
|
381
|
|
|
self::assertInstanceOf(DefaultMultiGetRegion::class, $barRegion); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..