1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Doctrine\Tests\ORM\Cache\Persister\Entity; |
||||
6 | |||||
7 | use Doctrine\Common\Collections\ArrayCollection; |
||||
8 | use Doctrine\Common\Collections\Criteria; |
||||
9 | use Doctrine\ORM\Cache\Persister\CachedPersister; |
||||
10 | use Doctrine\ORM\Cache\Persister\Entity\AbstractEntityPersister; |
||||
11 | use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister; |
||||
12 | use Doctrine\ORM\Cache\Region; |
||||
13 | use Doctrine\ORM\EntityManagerInterface; |
||||
14 | use Doctrine\ORM\Mapping\ClassMetadata; |
||||
15 | use Doctrine\ORM\Mapping\ManyToManyAssociationMetadata; |
||||
16 | use Doctrine\ORM\Mapping\OneToManyAssociationMetadata; |
||||
17 | use Doctrine\ORM\Mapping\OneToOneAssociationMetadata; |
||||
18 | use Doctrine\ORM\PersistentCollection; |
||||
19 | use Doctrine\ORM\Persisters\Entity\EntityPersister; |
||||
20 | use Doctrine\ORM\Query\ResultSetMappingBuilder; |
||||
21 | use Doctrine\Tests\Models\Cache\Country; |
||||
22 | use Doctrine\Tests\OrmTestCase; |
||||
23 | |||||
24 | /** |
||||
25 | * @group DDC-2183 |
||||
26 | */ |
||||
27 | abstract class AbstractEntityPersisterTest extends OrmTestCase |
||||
28 | { |
||||
29 | /** @var Region */ |
||||
30 | protected $region; |
||||
31 | |||||
32 | /** @var EntityPersister */ |
||||
33 | protected $entityPersister; |
||||
34 | |||||
35 | /** @var EntityManagerInterface */ |
||||
36 | protected $em; |
||||
37 | |||||
38 | /** @var array */ |
||||
39 | protected $regionMockMethods = [ |
||||
40 | 'getName', |
||||
41 | 'contains', |
||||
42 | 'get', |
||||
43 | 'getMultiple', |
||||
44 | 'put', |
||||
45 | 'evict', |
||||
46 | 'evictAll', |
||||
47 | ]; |
||||
48 | |||||
49 | /** @var array */ |
||||
50 | protected $entityPersisterMockMethods = [ |
||||
51 | 'getClassMetadata', |
||||
52 | 'getResultSetMapping', |
||||
53 | 'getInsertSQL', |
||||
54 | 'getSelectSQL', |
||||
55 | 'getCountSQL', |
||||
56 | 'expandParameters', |
||||
57 | 'expandCriteriaParameters', |
||||
58 | 'getSelectConditionStatementSQL', |
||||
59 | 'getIdentifier', |
||||
60 | 'setIdentifier', |
||||
61 | 'getColumnValue', |
||||
62 | 'insert', |
||||
63 | 'update', |
||||
64 | 'delete', |
||||
65 | 'getOwningTable', |
||||
66 | 'load', |
||||
67 | 'loadById', |
||||
68 | 'loadToOneEntity', |
||||
69 | 'count', |
||||
70 | 'refresh', |
||||
71 | 'loadCriteria', |
||||
72 | 'loadAll', |
||||
73 | 'getManyToManyCollection', |
||||
74 | 'loadManyToManyCollection', |
||||
75 | 'loadOneToManyCollection', |
||||
76 | 'lock', |
||||
77 | 'getOneToManyCollection', |
||||
78 | 'exists', |
||||
79 | ]; |
||||
80 | |||||
81 | /** |
||||
82 | * @return AbstractEntityPersister |
||||
83 | */ |
||||
84 | abstract protected function createPersister(EntityManagerInterface $em, EntityPersister $persister, Region $region, ClassMetadata $metadata); |
||||
85 | |||||
86 | protected function setUp() : void |
||||
87 | { |
||||
88 | $this->getSharedSecondLevelCacheDriverImpl()->flushAll(); |
||||
89 | $this->enableSecondLevelCache(); |
||||
90 | |||||
91 | parent::setUp(); |
||||
92 | |||||
93 | $this->em = $this->getTestEntityManager(); |
||||
94 | $this->region = $this->createRegion(); |
||||
95 | $this->entityPersister = $this->getMockBuilder(EntityPersister::class) |
||||
96 | ->setMethods($this->entityPersisterMockMethods) |
||||
97 | ->getMock(); |
||||
98 | } |
||||
99 | |||||
100 | /** |
||||
101 | * @return Region |
||||
102 | */ |
||||
103 | protected function createRegion() |
||||
104 | { |
||||
105 | return $this->getMockBuilder(Region::class) |
||||
106 | ->setMethods($this->regionMockMethods) |
||||
107 | ->getMock(); |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * @return AbstractEntityPersister |
||||
112 | */ |
||||
113 | protected function createPersisterDefault() |
||||
114 | { |
||||
115 | return $this->createPersister($this->em, $this->entityPersister, $this->region, $this->em->getClassMetadata(Country::class)); |
||||
116 | } |
||||
117 | |||||
118 | public function testImplementsEntityPersister() : void |
||||
119 | { |
||||
120 | $persister = $this->createPersisterDefault(); |
||||
121 | |||||
122 | self::assertInstanceOf(EntityPersister::class, $persister); |
||||
123 | self::assertInstanceOf(CachedPersister::class, $persister); |
||||
124 | self::assertInstanceOf(CachedEntityPersister::class, $persister); |
||||
125 | } |
||||
126 | |||||
127 | public function testInvokeGetSelectSQL() : void |
||||
128 | { |
||||
129 | $persister = $this->createPersisterDefault(); |
||||
130 | $assoc = new OneToManyAssociationMetadata('name'); |
||||
131 | |||||
132 | $this->entityPersister->expects($this->once()) |
||||
133 | ->method('getSelectSQL') |
||||
134 | ->with($this->equalTo(['name' => 'Foo']), $this->equalTo($assoc), $this->equalTo(1), $this->equalTo(2), $this->equalTo(3), $this->equalTo( |
||||
135 | [4] |
||||
136 | )) |
||||
137 | ->will($this->returnValue('SELECT * FROM foo WERE name = ?')); |
||||
138 | |||||
139 | self::assertEquals('SELECT * FROM foo WERE name = ?', $persister->getSelectSQL(['name' => 'Foo'], $assoc, 1, 2, 3, [4])); |
||||
140 | } |
||||
141 | |||||
142 | public function testInvokeGetInsertSQL() : void |
||||
143 | { |
||||
144 | $persister = $this->createPersisterDefault(); |
||||
145 | |||||
146 | $this->entityPersister->expects($this->once()) |
||||
147 | ->method('getInsertSQL') |
||||
148 | ->will($this->returnValue('INSERT INTO foo (?)')); |
||||
149 | |||||
150 | self::assertEquals('INSERT INTO foo (?)', $persister->getInsertSQL()); |
||||
151 | } |
||||
152 | |||||
153 | public function testInvokeExpandParameters() : void |
||||
154 | { |
||||
155 | $persister = $this->createPersisterDefault(); |
||||
156 | |||||
157 | $this->entityPersister->expects($this->once()) |
||||
158 | ->method('expandParameters') |
||||
159 | ->with($this->equalTo(['name' => 'Foo'])) |
||||
160 | ->will($this->returnValue(['name' => 'Foo'])); |
||||
161 | |||||
162 | self::assertEquals(['name' => 'Foo'], $persister->expandParameters(['name' => 'Foo'])); |
||||
163 | } |
||||
164 | |||||
165 | public function testInvokeExpandCriteriaParameters() : void |
||||
166 | { |
||||
167 | $persister = $this->createPersisterDefault(); |
||||
168 | $criteria = new Criteria(); |
||||
169 | |||||
170 | $this->entityPersister->expects($this->once()) |
||||
171 | ->method('expandCriteriaParameters') |
||||
172 | ->with($this->equalTo($criteria)) |
||||
173 | ->will($this->returnValue(['name' => 'Foo'])); |
||||
174 | |||||
175 | self::assertEquals(['name' => 'Foo'], $persister->expandCriteriaParameters($criteria)); |
||||
176 | } |
||||
177 | |||||
178 | public function testInvokeSelectConditionStatementSQL() : void |
||||
179 | { |
||||
180 | $persister = $this->createPersisterDefault(); |
||||
181 | $assoc = new OneToOneAssociationMetadata('id'); |
||||
182 | |||||
183 | $this->entityPersister->expects($this->once()) |
||||
184 | ->method('getSelectConditionStatementSQL') |
||||
185 | ->with($this->equalTo('id'), $this->equalTo(1), $this->equalTo($assoc), $this->equalTo('=')) |
||||
186 | ->will($this->returnValue('name = 1')); |
||||
187 | |||||
188 | self::assertEquals('name = 1', $persister->getSelectConditionStatementSQL('id', 1, $assoc, '=')); |
||||
189 | } |
||||
190 | |||||
191 | public function testInvokeInsert() : void |
||||
192 | { |
||||
193 | $persister = $this->createPersisterDefault(); |
||||
194 | $entity = new Country('Foo'); |
||||
195 | |||||
196 | $this->entityPersister->expects($this->once()) |
||||
197 | ->method('insert') |
||||
198 | ->with($this->equalTo($entity)); |
||||
199 | |||||
200 | self::assertNull($persister->insert($entity)); |
||||
201 | } |
||||
202 | |||||
203 | public function testInvokeUpdate() : void |
||||
204 | { |
||||
205 | $persister = $this->createPersisterDefault(); |
||||
206 | $entity = new Country('Foo'); |
||||
207 | |||||
208 | $this->entityPersister->expects($this->once()) |
||||
209 | ->method('update') |
||||
210 | ->with($this->equalTo($entity)); |
||||
211 | |||||
212 | $this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']); |
||||
213 | |||||
214 | self::assertNull($persister->update($entity)); |
||||
215 | } |
||||
216 | |||||
217 | public function testInvokeDelete() : void |
||||
218 | { |
||||
219 | $persister = $this->createPersisterDefault(); |
||||
220 | $entity = new Country('Foo'); |
||||
221 | |||||
222 | $this->entityPersister->expects($this->once()) |
||||
223 | ->method('delete') |
||||
224 | ->with($this->equalTo($entity)); |
||||
225 | |||||
226 | $this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']); |
||||
227 | |||||
228 | self::assertNull($persister->delete($entity)); |
||||
229 | } |
||||
230 | |||||
231 | public function testInvokeGetOwningTable() : void |
||||
232 | { |
||||
233 | $persister = $this->createPersisterDefault(); |
||||
234 | |||||
235 | $this->entityPersister->expects($this->once()) |
||||
236 | ->method('getOwningTable') |
||||
237 | ->with($this->equalTo('name')) |
||||
238 | ->will($this->returnValue('t')); |
||||
239 | |||||
240 | self::assertEquals('t', $persister->getOwningTable('name')); |
||||
241 | } |
||||
242 | |||||
243 | public function testInvokeLoad() : void |
||||
244 | { |
||||
245 | $persister = $this->createPersisterDefault(); |
||||
246 | $assoc = new OneToOneAssociationMetadata('id'); |
||||
247 | $entity = new Country('Foo'); |
||||
248 | |||||
249 | $this->entityPersister->expects($this->once()) |
||||
250 | ->method('load') |
||||
251 | ->with( |
||||
252 | $this->equalTo(['id' => 1]), |
||||
253 | $this->equalTo($entity), |
||||
254 | $this->equalTo($assoc), |
||||
255 | $this->equalTo([1]), |
||||
256 | $this->equalTo(2), |
||||
257 | $this->equalTo(3), |
||||
258 | $this->equalTo([4]) |
||||
259 | ) |
||||
260 | ->will($this->returnValue($entity)); |
||||
261 | |||||
262 | self::assertEquals($entity, $persister->load(['id' => 1], $entity, $assoc, [1], 2, 3, [4])); |
||||
263 | } |
||||
264 | |||||
265 | public function testInvokeLoadAll() : void |
||||
266 | { |
||||
267 | $rsm = new ResultSetMappingBuilder($this->em); |
||||
268 | $persister = $this->createPersisterDefault(); |
||||
269 | $entity = new Country('Foo'); |
||||
270 | |||||
271 | $rsm->addEntityResult(Country::class, 'c'); |
||||
272 | |||||
273 | $this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']); |
||||
274 | |||||
275 | $this->entityPersister->expects($this->once()) |
||||
276 | ->method('loadAll') |
||||
277 | ->with($this->equalTo(['id' => 1]), $this->equalTo([0]), $this->equalTo(1), $this->equalTo(2)) |
||||
278 | ->will($this->returnValue([$entity])); |
||||
279 | |||||
280 | $this->entityPersister->expects($this->once()) |
||||
281 | ->method('getResultSetMapping') |
||||
282 | ->will($this->returnValue($rsm)); |
||||
283 | |||||
284 | self::assertEquals([$entity], $persister->loadAll(['id' => 1], [0], 1, 2)); |
||||
285 | } |
||||
286 | |||||
287 | public function testInvokeLoadById() : void |
||||
288 | { |
||||
289 | $persister = $this->createPersisterDefault(); |
||||
290 | $entity = new Country('Foo'); |
||||
291 | |||||
292 | $this->entityPersister->expects($this->once()) |
||||
293 | ->method('loadById') |
||||
294 | ->with($this->equalTo(['id' => 1]), $this->equalTo($entity)) |
||||
295 | ->will($this->returnValue($entity)); |
||||
296 | |||||
297 | self::assertEquals($entity, $persister->loadById(['id' => 1], $entity)); |
||||
298 | } |
||||
299 | |||||
300 | public function testInvokeLoadToOneEntity() : void |
||||
301 | { |
||||
302 | $persister = $this->createPersisterDefault(); |
||||
303 | $assoc = new OneToOneAssociationMetadata('name'); |
||||
304 | $entity = new Country('Foo'); |
||||
305 | |||||
306 | $this->entityPersister->expects($this->once()) |
||||
307 | ->method('loadToOneEntity') |
||||
308 | ->with($this->equalTo($assoc), $this->equalTo('foo'), $this->equalTo(['id' => 11])) |
||||
309 | ->will($this->returnValue($entity)); |
||||
310 | |||||
311 | self::assertEquals($entity, $persister->loadToOneEntity($assoc, 'foo', ['id' => 11])); |
||||
312 | } |
||||
313 | |||||
314 | public function testInvokeRefresh() : void |
||||
315 | { |
||||
316 | $persister = $this->createPersisterDefault(); |
||||
317 | $entity = new Country('Foo'); |
||||
318 | |||||
319 | $this->entityPersister->expects($this->once()) |
||||
320 | ->method('refresh') |
||||
321 | ->with($this->equalTo(['id' => 1]), $this->equalTo($entity), $this->equalTo(0)) |
||||
322 | ->will($this->returnValue($entity)); |
||||
323 | |||||
324 | self::assertNull($persister->refresh(['id' => 1], $entity)); |
||||
325 | } |
||||
326 | |||||
327 | public function testInvokeLoadCriteria() : void |
||||
328 | { |
||||
329 | $rsm = new ResultSetMappingBuilder($this->em); |
||||
330 | $persister = $this->createPersisterDefault(); |
||||
331 | $entity = new Country('Foo'); |
||||
332 | $criteria = new Criteria(); |
||||
333 | |||||
334 | $this->em->getUnitOfWork()->registerManaged($entity, ['id' => 1], ['id' => 1, 'name' => 'Foo']); |
||||
335 | $rsm->addEntityResult(Country::class, 'c'); |
||||
336 | |||||
337 | $this->entityPersister->expects($this->once()) |
||||
338 | ->method('getResultSetMapping') |
||||
339 | ->will($this->returnValue($rsm)); |
||||
340 | |||||
341 | $this->entityPersister->expects($this->once()) |
||||
342 | ->method('loadCriteria') |
||||
343 | ->with($this->equalTo($criteria)) |
||||
344 | ->will($this->returnValue([$entity])); |
||||
345 | |||||
346 | self::assertEquals([$entity], $persister->loadCriteria($criteria)); |
||||
347 | } |
||||
348 | |||||
349 | public function testInvokeGetManyToManyCollection() : void |
||||
350 | { |
||||
351 | $persister = $this->createPersisterDefault(); |
||||
352 | $assoc = new ManyToManyAssociationMetadata('name'); |
||||
353 | $entity = new Country('Foo'); |
||||
354 | |||||
355 | $this->entityPersister->expects($this->once()) |
||||
356 | ->method('getManyToManyCollection') |
||||
357 | ->with($this->equalTo($assoc), $this->equalTo('Foo'), $this->equalTo(1), $this->equalTo(2)) |
||||
358 | ->will($this->returnValue([$entity])); |
||||
359 | |||||
360 | self::assertEquals([$entity], $persister->getManyToManyCollection($assoc, 'Foo', 1, 2)); |
||||
361 | } |
||||
362 | |||||
363 | public function testInvokeGetOneToManyCollection() : void |
||||
364 | { |
||||
365 | $persister = $this->createPersisterDefault(); |
||||
366 | $assoc = new OneToManyAssociationMetadata('name'); |
||||
367 | $entity = new Country('Foo'); |
||||
368 | |||||
369 | $this->entityPersister->expects($this->once()) |
||||
370 | ->method('getOneToManyCollection') |
||||
371 | ->with($this->equalTo($assoc), $this->equalTo('Foo'), $this->equalTo(1), $this->equalTo(2)) |
||||
372 | ->will($this->returnValue([$entity])); |
||||
373 | |||||
374 | self::assertEquals([$entity], $persister->getOneToManyCollection($assoc, 'Foo', 1, 2)); |
||||
375 | } |
||||
376 | |||||
377 | public function testInvokeLoadManyToManyCollection() : void |
||||
378 | { |
||||
379 | $mapping = $this->em->getClassMetadata(Country::class); |
||||
380 | $assoc = new ManyToManyAssociationMetadata('name'); |
||||
381 | $coll = new PersistentCollection($this->em, $mapping, new ArrayCollection()); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
382 | $persister = $this->createPersisterDefault(); |
||||
383 | $entity = new Country('Foo'); |
||||
384 | |||||
385 | $this->entityPersister->expects($this->once()) |
||||
386 | ->method('loadManyToManyCollection') |
||||
387 | ->with($this->equalTo($assoc), $this->equalTo('Foo'), $coll) |
||||
388 | ->will($this->returnValue([$entity])); |
||||
389 | |||||
390 | self::assertEquals([$entity], $persister->loadManyToManyCollection($assoc, 'Foo', $coll)); |
||||
391 | } |
||||
392 | |||||
393 | public function testInvokeLoadOneToManyCollection() : void |
||||
394 | { |
||||
395 | $mapping = $this->em->getClassMetadata(Country::class); |
||||
396 | $assoc = new OneToManyAssociationMetadata('name'); |
||||
397 | $coll = new PersistentCollection($this->em, $mapping, new ArrayCollection()); |
||||
0 ignored issues
–
show
$mapping of type Doctrine\Common\Persistence\Mapping\ClassMetadata is incompatible with the type Doctrine\ORM\Mapping\ClassMetadata expected by parameter $class of Doctrine\ORM\PersistentCollection::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
398 | $persister = $this->createPersisterDefault(); |
||||
399 | $entity = new Country('Foo'); |
||||
400 | |||||
401 | $this->entityPersister->expects($this->once()) |
||||
402 | ->method('loadOneToManyCollection') |
||||
403 | ->with($this->equalTo($assoc), $this->equalTo('Foo'), $coll) |
||||
404 | ->will($this->returnValue([$entity])); |
||||
405 | |||||
406 | self::assertEquals([$entity], $persister->loadOneToManyCollection($assoc, 'Foo', $coll)); |
||||
407 | } |
||||
408 | |||||
409 | public function testInvokeLock() : void |
||||
410 | { |
||||
411 | $identifier = ['id' => 1]; |
||||
412 | $persister = $this->createPersisterDefault(); |
||||
413 | |||||
414 | $this->entityPersister->expects($this->once()) |
||||
415 | ->method('lock') |
||||
416 | ->with($this->equalTo($identifier), $this->equalTo(1)); |
||||
417 | |||||
418 | self::assertNull($persister->lock($identifier, 1)); |
||||
419 | } |
||||
420 | |||||
421 | public function testInvokeExists() : void |
||||
422 | { |
||||
423 | $entity = new Country('Foo'); |
||||
424 | $persister = $this->createPersisterDefault(); |
||||
425 | |||||
426 | $this->entityPersister->expects($this->once()) |
||||
427 | ->method('exists') |
||||
428 | ->with($this->equalTo($entity), $this->equalTo(null)); |
||||
429 | |||||
430 | self::assertNull($persister->exists($entity)); |
||||
431 | } |
||||
432 | } |
||||
433 |