Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
31 | class UnitOfWorkTest extends OrmTestCase |
||
32 | { |
||
33 | /** |
||
34 | * SUT |
||
35 | * |
||
36 | * @var UnitOfWorkMock |
||
37 | */ |
||
38 | private $_unitOfWork; |
||
39 | |||
40 | /** |
||
41 | * Provides a sequence mock to the UnitOfWork |
||
42 | * |
||
43 | * @var ConnectionMock |
||
44 | */ |
||
45 | private $_connectionMock; |
||
46 | |||
47 | /** |
||
48 | * The EntityManager mock that provides the mock persisters |
||
49 | * |
||
50 | * @var EntityManagerMock |
||
51 | */ |
||
52 | private $_emMock; |
||
53 | |||
54 | /** |
||
55 | * @var EventManager|\PHPUnit_Framework_MockObject_MockObject |
||
56 | */ |
||
57 | private $eventManager; |
||
58 | |||
59 | protected function setUp() |
||
60 | { |
||
61 | parent::setUp(); |
||
62 | $this->_connectionMock = new ConnectionMock([], new DriverMock()); |
||
63 | $this->eventManager = $this->getMockBuilder(EventManager::class)->getMock(); |
||
64 | $this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager); |
||
65 | // SUT |
||
66 | $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); |
||
67 | $this->_emMock->setUnitOfWork($this->_unitOfWork); |
||
68 | } |
||
69 | |||
70 | public function testRegisterRemovedOnNewEntityIsIgnored() |
||
71 | { |
||
72 | $user = new ForumUser(); |
||
73 | $user->username = 'romanb'; |
||
74 | $this->assertFalse($this->_unitOfWork->isScheduledForDelete($user)); |
||
75 | $this->_unitOfWork->scheduleForDelete($user); |
||
76 | $this->assertFalse($this->_unitOfWork->isScheduledForDelete($user)); |
||
77 | } |
||
78 | |||
79 | |||
80 | /* Operational tests */ |
||
81 | |||
82 | public function testSavingSingleEntityWithIdentityColumnForcesInsert() |
||
83 | { |
||
84 | // Setup fake persister and id generator for identity generation |
||
85 | $userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(ForumUser::class)); |
||
86 | $this->_unitOfWork->setEntityPersister(ForumUser::class, $userPersister); |
||
87 | $userPersister->setMockIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); |
||
88 | |||
89 | // Test |
||
90 | $user = new ForumUser(); |
||
91 | $user->username = 'romanb'; |
||
92 | $this->_unitOfWork->persist($user); |
||
93 | |||
94 | // Check |
||
95 | $this->assertEquals(0, count($userPersister->getInserts())); |
||
96 | $this->assertEquals(0, count($userPersister->getUpdates())); |
||
97 | $this->assertEquals(0, count($userPersister->getDeletes())); |
||
98 | $this->assertFalse($this->_unitOfWork->isInIdentityMap($user)); |
||
99 | // should no longer be scheduled for insert |
||
100 | $this->assertTrue($this->_unitOfWork->isScheduledForInsert($user)); |
||
101 | |||
102 | // Now lets check whether a subsequent commit() does anything |
||
103 | $userPersister->reset(); |
||
104 | |||
105 | // Test |
||
106 | $this->_unitOfWork->commit(); |
||
107 | |||
108 | // Check. |
||
109 | $this->assertEquals(1, count($userPersister->getInserts())); |
||
110 | $this->assertEquals(0, count($userPersister->getUpdates())); |
||
111 | $this->assertEquals(0, count($userPersister->getDeletes())); |
||
112 | |||
113 | // should have an id |
||
114 | $this->assertTrue(is_numeric($user->id)); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Tests a scenario where a save() operation is cascaded from a ForumUser |
||
119 | * to its associated ForumAvatar, both entities using IDENTITY id generation. |
||
120 | */ |
||
121 | public function testCascadedIdentityColumnInsert() |
||
122 | { |
||
123 | // Setup fake persister and id generator for identity generation |
||
124 | //ForumUser |
||
125 | $userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(ForumUser::class)); |
||
126 | $this->_unitOfWork->setEntityPersister(ForumUser::class, $userPersister); |
||
127 | $userPersister->setMockIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); |
||
128 | // ForumAvatar |
||
129 | $avatarPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(ForumAvatar::class)); |
||
130 | $this->_unitOfWork->setEntityPersister(ForumAvatar::class, $avatarPersister); |
||
131 | $avatarPersister->setMockIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); |
||
132 | |||
133 | // Test |
||
134 | $user = new ForumUser(); |
||
135 | $user->username = 'romanb'; |
||
136 | $avatar = new ForumAvatar(); |
||
137 | $user->avatar = $avatar; |
||
138 | $this->_unitOfWork->persist($user); // save cascaded to avatar |
||
139 | |||
140 | $this->_unitOfWork->commit(); |
||
141 | |||
142 | $this->assertTrue(is_numeric($user->id)); |
||
143 | $this->assertTrue(is_numeric($avatar->id)); |
||
144 | |||
145 | $this->assertEquals(1, count($userPersister->getInserts())); |
||
146 | $this->assertEquals(0, count($userPersister->getUpdates())); |
||
147 | $this->assertEquals(0, count($userPersister->getDeletes())); |
||
148 | |||
149 | $this->assertEquals(1, count($avatarPersister->getInserts())); |
||
150 | $this->assertEquals(0, count($avatarPersister->getUpdates())); |
||
151 | $this->assertEquals(0, count($avatarPersister->getDeletes())); |
||
152 | } |
||
153 | |||
154 | public function testChangeTrackingNotify() |
||
155 | { |
||
156 | $persister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(NotifyChangedEntity::class)); |
||
157 | $this->_unitOfWork->setEntityPersister(NotifyChangedEntity::class, $persister); |
||
158 | $itemPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(NotifyChangedRelatedItem::class)); |
||
159 | $this->_unitOfWork->setEntityPersister(NotifyChangedRelatedItem::class, $itemPersister); |
||
160 | |||
161 | $entity = new NotifyChangedEntity; |
||
162 | $entity->setData('thedata'); |
||
163 | $this->_unitOfWork->persist($entity); |
||
164 | |||
165 | $this->_unitOfWork->commit(); |
||
166 | $this->assertEquals(1, count($persister->getInserts())); |
||
167 | $persister->reset(); |
||
168 | |||
169 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); |
||
170 | |||
171 | $entity->setData('newdata'); |
||
172 | $entity->setTransient('newtransientvalue'); |
||
173 | |||
174 | $this->assertTrue($this->_unitOfWork->isScheduledForDirtyCheck($entity)); |
||
175 | |||
176 | $this->assertEquals(['data' => ['thedata', 'newdata']], $this->_unitOfWork->getEntityChangeSet($entity)); |
||
177 | |||
178 | $item = new NotifyChangedRelatedItem(); |
||
179 | $entity->getItems()->add($item); |
||
180 | $item->setOwner($entity); |
||
181 | $this->_unitOfWork->persist($item); |
||
182 | |||
183 | $this->_unitOfWork->commit(); |
||
184 | $this->assertEquals(1, count($itemPersister->getInserts())); |
||
185 | $persister->reset(); |
||
186 | $itemPersister->reset(); |
||
187 | |||
188 | |||
189 | $entity->getItems()->removeElement($item); |
||
190 | $item->setOwner(null); |
||
191 | $this->assertTrue($entity->getItems()->isDirty()); |
||
|
|||
192 | $this->_unitOfWork->commit(); |
||
193 | $updates = $itemPersister->getUpdates(); |
||
194 | $this->assertEquals(1, count($updates)); |
||
195 | $this->assertTrue($updates[0] === $item); |
||
196 | } |
||
197 | |||
198 | View Code Duplication | public function testGetEntityStateOnVersionedEntityWithAssignedIdentifier() |
|
199 | { |
||
200 | $persister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(VersionedAssignedIdentifierEntity::class)); |
||
201 | $this->_unitOfWork->setEntityPersister(VersionedAssignedIdentifierEntity::class, $persister); |
||
202 | |||
203 | $e = new VersionedAssignedIdentifierEntity(); |
||
204 | $e->id = 42; |
||
205 | $this->assertEquals(UnitOfWork::STATE_NEW, $this->_unitOfWork->getEntityState($e)); |
||
206 | $this->assertFalse($persister->isExistsCalled()); |
||
207 | } |
||
208 | |||
209 | public function testGetEntityStateWithAssignedIdentity() |
||
210 | { |
||
211 | $persister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(CmsPhonenumber::class)); |
||
212 | $this->_unitOfWork->setEntityPersister(CmsPhonenumber::class, $persister); |
||
213 | |||
214 | $ph = new CmsPhonenumber(); |
||
215 | $ph->phonenumber = '12345'; |
||
216 | |||
217 | $this->assertEquals(UnitOfWork::STATE_NEW, $this->_unitOfWork->getEntityState($ph)); |
||
218 | $this->assertTrue($persister->isExistsCalled()); |
||
219 | |||
220 | $persister->reset(); |
||
221 | |||
222 | // if the entity is already managed the exists() check should be skipped |
||
223 | $this->_unitOfWork->registerManaged($ph, ['phonenumber' => '12345'], []); |
||
224 | $this->assertEquals(UnitOfWork::STATE_MANAGED, $this->_unitOfWork->getEntityState($ph)); |
||
225 | $this->assertFalse($persister->isExistsCalled()); |
||
226 | $ph2 = new CmsPhonenumber(); |
||
227 | $ph2->phonenumber = '12345'; |
||
228 | $this->assertEquals(UnitOfWork::STATE_DETACHED, $this->_unitOfWork->getEntityState($ph2)); |
||
229 | $this->assertFalse($persister->isExistsCalled()); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * DDC-2086 [GH-484] Prevented 'Undefined index' notice when updating. |
||
234 | */ |
||
235 | public function testNoUndefinedIndexNoticeOnScheduleForUpdateWithoutChanges() |
||
236 | { |
||
237 | // Setup fake persister and id generator |
||
238 | $userPersister = new EntityPersisterMock($this->_emMock, $this->_emMock->getClassMetadata(ForumUser::class)); |
||
239 | $userPersister->setMockIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY); |
||
240 | $this->_unitOfWork->setEntityPersister(ForumUser::class, $userPersister); |
||
241 | |||
242 | // Create a test user |
||
243 | $user = new ForumUser(); |
||
244 | $user->name = 'Jasper'; |
||
245 | $this->_unitOfWork->persist($user); |
||
246 | $this->_unitOfWork->commit(); |
||
247 | |||
248 | // Schedule user for update without changes |
||
249 | $this->_unitOfWork->scheduleForUpdate($user); |
||
250 | |||
251 | self::assertNotEmpty($this->_unitOfWork->getScheduledEntityUpdates()); |
||
252 | |||
253 | // This commit should not raise an E_NOTICE |
||
254 | $this->_unitOfWork->commit(); |
||
255 | |||
256 | self::assertEmpty($this->_unitOfWork->getScheduledEntityUpdates()); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @group DDC-1984 |
||
261 | */ |
||
262 | public function testLockWithoutEntityThrowsException() |
||
263 | { |
||
264 | $this->expectException(\InvalidArgumentException::class); |
||
265 | $this->_unitOfWork->lock(null, null, null); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @group DDC-3490 |
||
270 | * |
||
271 | * @dataProvider invalidAssociationValuesDataProvider |
||
272 | * |
||
273 | * @param mixed $invalidValue |
||
274 | */ |
||
275 | View Code Duplication | public function testRejectsPersistenceOfObjectsWithInvalidAssociationValue($invalidValue) |
|
276 | { |
||
277 | $this->_unitOfWork->setEntityPersister( |
||
278 | ForumUser::class, |
||
279 | new EntityPersisterMock( |
||
280 | $this->_emMock, |
||
281 | $this->_emMock->getClassMetadata(ForumUser::class) |
||
282 | ) |
||
283 | ); |
||
284 | |||
285 | $user = new ForumUser(); |
||
286 | $user->username = 'John'; |
||
287 | $user->avatar = $invalidValue; |
||
288 | |||
289 | $this->expectException(\Doctrine\ORM\ORMInvalidArgumentException::class); |
||
290 | |||
291 | $this->_unitOfWork->persist($user); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @group DDC-3490 |
||
296 | * |
||
297 | * @dataProvider invalidAssociationValuesDataProvider |
||
298 | * |
||
299 | * @param mixed $invalidValue |
||
300 | */ |
||
301 | public function testRejectsChangeSetComputationForObjectsWithInvalidAssociationValue($invalidValue) |
||
302 | { |
||
303 | $metadata = $this->_emMock->getClassMetadata(ForumUser::class); |
||
304 | |||
305 | $this->_unitOfWork->setEntityPersister( |
||
306 | ForumUser::class, |
||
307 | new EntityPersisterMock($this->_emMock, $metadata) |
||
308 | ); |
||
309 | |||
310 | $user = new ForumUser(); |
||
311 | |||
312 | $this->_unitOfWork->persist($user); |
||
313 | |||
314 | $user->username = 'John'; |
||
315 | $user->avatar = $invalidValue; |
||
316 | |||
317 | $this->expectException(\Doctrine\ORM\ORMInvalidArgumentException::class); |
||
318 | |||
319 | $this->_unitOfWork->computeChangeSet($metadata, $user); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @group DDC-3619 |
||
324 | * @group 1338 |
||
325 | */ |
||
326 | public function testRemovedAndRePersistedEntitiesAreInTheIdentityMapAndAreNotGarbageCollected() |
||
327 | { |
||
328 | $entity = new ForumUser(); |
||
329 | $entity->id = 123; |
||
330 | |||
331 | $this->_unitOfWork->registerManaged($entity, ['id' => 123], []); |
||
332 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); |
||
333 | |||
334 | $this->_unitOfWork->remove($entity); |
||
335 | $this->assertFalse($this->_unitOfWork->isInIdentityMap($entity)); |
||
336 | |||
337 | $this->_unitOfWork->persist($entity); |
||
338 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity)); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @group 5849 |
||
343 | * @group 5850 |
||
344 | */ |
||
345 | public function testPersistedEntityAndClearManager() |
||
346 | { |
||
347 | $entity1 = new City(123, 'London'); |
||
348 | $entity2 = new Country(456, 'United Kingdom'); |
||
349 | |||
350 | $this->_unitOfWork->persist($entity1); |
||
351 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); |
||
352 | |||
353 | $this->_unitOfWork->persist($entity2); |
||
354 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity2)); |
||
355 | |||
356 | $this->_unitOfWork->clear(Country::class); |
||
357 | $this->assertTrue($this->_unitOfWork->isInIdentityMap($entity1)); |
||
358 | $this->assertFalse($this->_unitOfWork->isInIdentityMap($entity2)); |
||
359 | $this->assertTrue($this->_unitOfWork->isScheduledForInsert($entity1)); |
||
360 | $this->assertFalse($this->_unitOfWork->isScheduledForInsert($entity2)); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Data Provider |
||
365 | * |
||
366 | * @return mixed[][] |
||
367 | */ |
||
368 | public function invalidAssociationValuesDataProvider() |
||
369 | { |
||
370 | return [ |
||
371 | ['foo'], |
||
372 | [['foo']], |
||
373 | [''], |
||
374 | [[]], |
||
375 | [new stdClass()], |
||
376 | [new ArrayCollection()], |
||
377 | ]; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @dataProvider entitiesWithValidIdentifiersProvider |
||
382 | * |
||
383 | * @param object $entity |
||
384 | * @param string $idHash |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | public function testAddToIdentityMapValidIdentifiers($entity, $idHash) |
||
389 | { |
||
390 | $this->_unitOfWork->persist($entity); |
||
391 | $this->_unitOfWork->addToIdentityMap($entity); |
||
392 | |||
393 | self::assertSame($entity, $this->_unitOfWork->getByIdHash($idHash, get_class($entity))); |
||
394 | } |
||
395 | |||
396 | public function entitiesWithValidIdentifiersProvider() |
||
397 | { |
||
398 | $emptyString = new EntityWithStringIdentifier(); |
||
399 | |||
400 | $emptyString->id = ''; |
||
401 | |||
402 | $nonEmptyString = new EntityWithStringIdentifier(); |
||
403 | |||
404 | $nonEmptyString->id = uniqid('id', true); |
||
405 | |||
406 | $emptyStrings = new EntityWithCompositeStringIdentifier(); |
||
407 | |||
408 | $emptyStrings->id1 = ''; |
||
409 | $emptyStrings->id2 = ''; |
||
410 | |||
411 | $nonEmptyStrings = new EntityWithCompositeStringIdentifier(); |
||
412 | |||
413 | $nonEmptyStrings->id1 = uniqid('id1', true); |
||
414 | $nonEmptyStrings->id2 = uniqid('id2', true); |
||
415 | |||
416 | $booleanTrue = new EntityWithBooleanIdentifier(); |
||
417 | |||
418 | $booleanTrue->id = true; |
||
419 | |||
420 | $booleanFalse = new EntityWithBooleanIdentifier(); |
||
421 | |||
422 | $booleanFalse->id = false; |
||
423 | |||
424 | return [ |
||
425 | 'empty string, single field' => [$emptyString, serialize([''])], |
||
426 | 'non-empty string, single field' => [$nonEmptyString, serialize([$nonEmptyString->id])], |
||
427 | 'empty strings, two fields' => [$emptyStrings, serialize(['', ''])], |
||
428 | 'non-empty strings, two fields' => [ |
||
429 | $nonEmptyStrings, |
||
430 | serialize([$nonEmptyStrings->id1, $nonEmptyStrings->id2]) |
||
431 | ], |
||
432 | 'boolean true' => [$booleanTrue, serialize(['1'])], |
||
433 | 'boolean false' => [$booleanFalse, serialize([''])], |
||
434 | ]; |
||
435 | } |
||
436 | |||
437 | public function testRegisteringAManagedInstanceRequiresANonEmptyIdentifier() |
||
443 | |||
444 | /** |
||
445 | * @dataProvider entitiesWithInvalidIdentifiersProvider |
||
446 | * |
||
447 | * @param object $entity |
||
448 | * @param array $identifier |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | public function testAddToIdentityMapInvalidIdentifiers($entity, array $identifier) |
||
458 | |||
459 | |||
460 | public function entitiesWithInvalidIdentifiersProvider() |
||
477 | |||
478 | /** |
||
479 | * @group 5689 |
||
480 | * @group 1465 |
||
481 | */ |
||
482 | public function testObjectHashesOfMergedEntitiesAreNotUsedInOriginalEntityDataMap() |
||
505 | |||
506 | /** |
||
507 | * @group DDC-1955 |
||
508 | * @group 5570 |
||
509 | * @group 6174 |
||
510 | */ |
||
511 | public function testMergeWithNewEntityWillPersistItAndTriggerPrePersistListenersWithMergedEntityData() |
||
512 | { |
||
513 | $entity = new EntityWithRandomlyGeneratedField(); |
||
514 | |||
515 | $generatedFieldValue = $entity->generatedField; |
||
516 | |||
517 | $this |
||
518 | ->eventManager |
||
519 | ->expects(self::any()) |
||
520 | ->method('hasListeners') |
||
521 | ->willReturnCallback(function ($eventName) { |
||
549 | |||
550 | /** |
||
551 | * @group DDC-1955 |
||
552 | * @group 5570 |
||
553 | * @group 6174 |
||
554 | */ |
||
555 | public function testMergeWithExistingEntityWillNotPersistItNorTriggerPrePersistListeners() |
||
587 | |||
588 | /** |
||
589 | * @group 5923 |
||
590 | */ |
||
591 | public function testUnitOfWorkDoesNotConcatenateIdentifierWithAmbiguousPatterns() |
||
616 | |||
617 | /** |
||
618 | * @group 5923 |
||
619 | */ |
||
620 | public function testUnitOfWorkStoresAlsoNonUtf8Identifiers() |
||
640 | |||
641 | /** |
||
642 | * @group 5923 |
||
643 | * |
||
644 | * Note: this test is introduced for BC compliance only. If we expect consumers to always |
||
645 | * pass in the keys of an identifier, then we should change this test so it fails, |
||
646 | * but we'd have to document the BC break. |
||
647 | */ |
||
648 | public function testUnitOfWorkCanFetchByIdentifierWithFlatListOfIdentifierValues() |
||
658 | } |
||
659 | |||
822 |
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.