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:
Complex classes like TrashServiceTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TrashServiceTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class TrashServiceTest extends BaseTrashServiceTest |
||
35 | { |
||
36 | /** |
||
37 | * Test for the trash() method. |
||
38 | * |
||
39 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
40 | * @depends eZ\Publish\API\Repository\Tests\LocationServiceTest::testLoadLocationByRemoteId |
||
41 | */ |
||
42 | public function testTrash() |
||
43 | { |
||
44 | /* BEGIN: Use Case */ |
||
45 | $trashItem = $this->createTrashItem(); |
||
46 | /* END: Use Case */ |
||
47 | |||
48 | $this->assertInstanceOf( |
||
49 | '\\eZ\\Publish\\API\\Repository\\Values\\Content\\TrashItem', |
||
50 | $trashItem |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Test for the trash() method. |
||
56 | * |
||
57 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
58 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
59 | */ |
||
60 | public function testTrashSetsExpectedTrashItemProperties() |
||
61 | { |
||
62 | $repository = $this->getRepository(); |
||
63 | |||
64 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
65 | |||
66 | // Load the location that will be trashed |
||
67 | $location = $repository->getLocationService() |
||
68 | ->loadLocationByRemoteId($mediaRemoteId); |
||
69 | |||
70 | $expected = [ |
||
71 | 'id' => $location->id, |
||
72 | 'depth' => $location->depth, |
||
73 | 'hidden' => $location->hidden, |
||
74 | 'invisible' => $location->invisible, |
||
75 | 'parentLocationId' => $location->parentLocationId, |
||
76 | 'pathString' => $location->pathString, |
||
77 | 'priority' => $location->priority, |
||
78 | 'remoteId' => $location->remoteId, |
||
79 | 'sortField' => $location->sortField, |
||
80 | 'sortOrder' => $location->sortOrder, |
||
81 | ]; |
||
82 | |||
83 | $trashItem = $this->createTrashItem(); |
||
84 | |||
85 | $this->assertPropertiesCorrect($expected, $trashItem); |
||
|
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Test for the trash() method. |
||
90 | * |
||
91 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
92 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
93 | */ |
||
94 | View Code Duplication | public function testTrashRemovesLocationFromMainStorage() |
|
95 | { |
||
96 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class); |
||
97 | |||
98 | $repository = $this->getRepository(); |
||
99 | |||
100 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
101 | |||
102 | /* BEGIN: Use Case */ |
||
103 | $this->createTrashItem(); |
||
104 | |||
105 | // Load the location service |
||
106 | $locationService = $repository->getLocationService(); |
||
107 | |||
108 | // This call will fail with a "NotFoundException", because the media |
||
109 | // location was marked as trashed in the main storage |
||
110 | $locationService->loadLocationByRemoteId($mediaRemoteId); |
||
111 | /* END: Use Case */ |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Test for the trash() method. |
||
116 | * |
||
117 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
118 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
119 | */ |
||
120 | public function testTrashRemovesChildLocationsFromMainStorage() |
||
121 | { |
||
122 | $repository = $this->getRepository(); |
||
123 | |||
124 | /* BEGIN: Use Case */ |
||
125 | $remoteIds = $this->createRemoteIdList(); |
||
126 | |||
127 | $this->createTrashItem(); |
||
128 | |||
129 | // All invocations to loadLocationByRemoteId() to one of the above |
||
130 | // collected remoteIds will return in an "NotFoundException" |
||
131 | /* END: Use Case */ |
||
132 | |||
133 | $locationService = $repository->getLocationService(); |
||
134 | foreach ($remoteIds as $remoteId) { |
||
135 | try { |
||
136 | $locationService->loadLocationByRemoteId($remoteId); |
||
137 | $this->fail("Location '{$remoteId}' should exist.'"); |
||
138 | } catch (NotFoundException $e) { |
||
139 | // echo $e->getFile(), ' +', $e->getLine(), PHP_EOL; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | $this->assertGreaterThan( |
||
144 | 0, |
||
145 | count($remoteIds), |
||
146 | "There should be at least one 'Community' child location." |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Test for the trash() method. |
||
152 | * |
||
153 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
154 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
155 | */ |
||
156 | public function testTrashDecrementsChildCountOnParentLocation() |
||
157 | { |
||
158 | $repository = $this->getRepository(); |
||
159 | $locationService = $repository->getLocationService(); |
||
160 | |||
161 | $baseLocationId = $this->generateId('location', 1); |
||
162 | |||
163 | $location = $locationService->loadLocation($baseLocationId); |
||
164 | |||
165 | $childCount = $locationService->getLocationChildCount($location); |
||
166 | |||
167 | $this->createTrashItem(); |
||
168 | |||
169 | $this->refreshSearch($repository); |
||
170 | |||
171 | $this->assertEquals( |
||
172 | $childCount - 1, |
||
173 | $locationService->getLocationChildCount($location) |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Test sending a location to trash updates Content mainLocation. |
||
179 | * |
||
180 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
181 | */ |
||
182 | public function testTrashUpdatesMainLocation() |
||
183 | { |
||
184 | $repository = $this->getRepository(); |
||
185 | $contentService = $repository->getContentService(); |
||
186 | $locationService = $repository->getLocationService(); |
||
187 | $trashService = $repository->getTrashService(); |
||
188 | |||
189 | $contentInfo = $contentService->loadContentInfo(42); |
||
190 | |||
191 | // Create additional location that will become new main location |
||
192 | $location = $locationService->createLocation( |
||
193 | $contentInfo, |
||
194 | new LocationCreateStruct(['parentLocationId' => 2]) |
||
195 | ); |
||
196 | |||
197 | $trashService->trash( |
||
198 | $locationService->loadLocation($contentInfo->mainLocationId) |
||
199 | ); |
||
200 | |||
201 | self::assertEquals( |
||
202 | $location->id, |
||
203 | $contentService->loadContentInfo(42)->mainLocationId |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Test sending a location to trash. |
||
209 | * |
||
210 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
211 | */ |
||
212 | public function testTrashReturnsNull() |
||
213 | { |
||
214 | $repository = $this->getRepository(); |
||
215 | $contentService = $repository->getContentService(); |
||
216 | $locationService = $repository->getLocationService(); |
||
217 | $trashService = $repository->getTrashService(); |
||
218 | |||
219 | // Create additional location to trash |
||
220 | $location = $locationService->createLocation( |
||
221 | $contentService->loadContentInfo(42), |
||
222 | new LocationCreateStruct(['parentLocationId' => 2]) |
||
223 | ); |
||
224 | |||
225 | $trashItem = $trashService->trash($location); |
||
226 | |||
227 | self::assertNull($trashItem); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Test for the loadTrashItem() method. |
||
232 | * |
||
233 | * @covers \eZ\Publish\API\Repository\TrashService::loadTrashItem |
||
234 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
235 | */ |
||
236 | public function testLoadTrashItem() |
||
237 | { |
||
238 | $repository = $this->getRepository(); |
||
239 | $trashService = $repository->getTrashService(); |
||
240 | |||
241 | /* BEGIN: Use Case */ |
||
242 | $trashItem = $this->createTrashItem(); |
||
243 | |||
244 | // Reload the trash item |
||
245 | $trashItemReloaded = $trashService->loadTrashItem($trashItem->id); |
||
246 | /* END: Use Case */ |
||
247 | |||
248 | $this->assertInstanceOf( |
||
249 | APITrashItem::class, |
||
250 | $trashItemReloaded |
||
251 | ); |
||
252 | |||
253 | $this->assertEquals( |
||
254 | $trashItem->pathString, |
||
255 | $trashItemReloaded->pathString |
||
256 | ); |
||
257 | |||
258 | $this->assertEquals( |
||
259 | $trashItem, |
||
260 | $trashItemReloaded |
||
261 | ); |
||
262 | |||
263 | $this->assertInstanceOf( |
||
264 | DateTime::class, |
||
265 | $trashItemReloaded->trashed |
||
266 | ); |
||
267 | |||
268 | $this->assertEquals( |
||
269 | $trashItem->trashed->getTimestamp(), |
||
270 | $trashItemReloaded->trashed->getTimestamp() |
||
271 | ); |
||
272 | |||
273 | $this->assertGreaterThan( |
||
274 | 0, |
||
275 | $trashItemReloaded->trashed->getTimestamp() |
||
276 | ); |
||
277 | |||
278 | $this->assertInstanceOf( |
||
279 | Content::class, |
||
280 | $content = $trashItemReloaded->getContent() |
||
281 | ); |
||
282 | $this->assertEquals($trashItem->contentId, $content->contentInfo->id); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Test for the loadTrashItem() method. |
||
287 | * |
||
288 | * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem() |
||
289 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem |
||
290 | */ |
||
291 | public function testLoadTrashItemThrowsNotFoundException() |
||
292 | { |
||
293 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class); |
||
294 | |||
295 | $repository = $this->getRepository(); |
||
296 | |||
297 | $nonExistingTrashId = $this->generateId('trash', 2342); |
||
298 | /* BEGIN: Use Case */ |
||
299 | $trashService = $repository->getTrashService(); |
||
300 | |||
301 | // This call will fail with a "NotFoundException", because no trash item |
||
302 | // with the ID 1342 should exist in an eZ Publish demo installation |
||
303 | $trashService->loadTrashItem($nonExistingTrashId); |
||
304 | /* END: Use Case */ |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Test for the recover() method. |
||
309 | * |
||
310 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
311 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
312 | */ |
||
313 | public function testRecover() |
||
314 | { |
||
315 | $repository = $this->getRepository(); |
||
316 | $trashService = $repository->getTrashService(); |
||
317 | $locationService = $repository->getLocationService(); |
||
318 | |||
319 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
320 | |||
321 | /* BEGIN: Use Case */ |
||
322 | $trashItem = $this->createTrashItem(); |
||
323 | |||
324 | // Recover the trashed item |
||
325 | $location = $trashService->recover($trashItem); |
||
326 | |||
327 | // Load the recovered location |
||
328 | $locationReloaded = $locationService->loadLocationByRemoteId( |
||
329 | $mediaRemoteId |
||
330 | ); |
||
331 | /* END: Use Case */ |
||
332 | |||
333 | $this->assertInstanceOf( |
||
334 | APILocation::class, |
||
335 | $location |
||
336 | ); |
||
337 | |||
338 | $this->assertEquals( |
||
339 | $location, |
||
340 | $locationReloaded |
||
341 | ); |
||
342 | |||
343 | try { |
||
344 | $trashService->loadTrashItem($trashItem->id); |
||
345 | $this->fail('Trash item was not removed after being recovered.'); |
||
346 | } catch (NotFoundException $e) { |
||
347 | // All well |
||
348 | } |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Test recovering a non existing trash item results in a NotFoundException. |
||
353 | * |
||
354 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
355 | */ |
||
356 | View Code Duplication | public function testRecoverThrowsNotFoundExceptionForNonExistingTrashItem() |
|
357 | { |
||
358 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\NotFoundException::class); |
||
359 | |||
360 | $repository = $this->getRepository(); |
||
361 | $trashService = $repository->getTrashService(); |
||
362 | |||
363 | $trashService->recover( |
||
364 | $this->getTrashItemDouble( |
||
365 | 12364, |
||
366 | 12345, |
||
367 | 12363 |
||
368 | ) |
||
369 | ); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Test for the trash() method. |
||
374 | * |
||
375 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
376 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
377 | */ |
||
378 | public function testNotFoundAliasAfterRemoveIt() |
||
398 | |||
399 | /** |
||
400 | * Test for the recover() method. |
||
401 | * |
||
402 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
403 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
404 | */ |
||
405 | public function testAliasesForRemovedItems() |
||
441 | |||
442 | /** |
||
443 | * Test for the recover() method. |
||
444 | * |
||
445 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
446 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
447 | */ |
||
448 | public function testRecoverDoesNotRestoreChildLocations() |
||
491 | |||
492 | /** |
||
493 | * Test for the recover() method. |
||
494 | * |
||
495 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
496 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
497 | * |
||
498 | * @todo Fix naming |
||
499 | */ |
||
500 | public function testRecoverWithLocationCreateStructParameter() |
||
543 | |||
544 | /** |
||
545 | * Test for the recover() method. |
||
546 | * |
||
547 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem) |
||
548 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
549 | */ |
||
550 | public function testRecoverIncrementsChildCountOnOriginalParent() |
||
583 | |||
584 | /** |
||
585 | * Test for the recover() method. |
||
586 | * |
||
587 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
588 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter |
||
589 | */ |
||
590 | public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent() |
||
629 | |||
630 | /** |
||
631 | * Test recovering a location from trash to non existing location. |
||
632 | * |
||
633 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
634 | */ |
||
635 | public function testRecoverToNonExistingLocation() |
||
654 | |||
655 | /** |
||
656 | * Test for the findTrashItems() method. |
||
657 | * |
||
658 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
659 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
660 | */ |
||
661 | public function testFindTrashItems() |
||
690 | |||
691 | /** |
||
692 | * Test for the findTrashItems() method for it's result structure. |
||
693 | * |
||
694 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
695 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
696 | */ |
||
697 | public function testFindTrashItemsLimits() |
||
698 | { |
||
699 | $repository = $this->getRepository(); |
||
700 | $trashService = $repository->getTrashService(); |
||
701 | |||
702 | $this->createTrashItem(); |
||
703 | |||
704 | // Create a search query for all trashed items |
||
705 | $query = new Query(); |
||
706 | $query->limit = 2; |
||
707 | |||
708 | // Load all trashed locations |
||
709 | $searchResult = $trashService->findTrashItems($query); |
||
710 | |||
711 | $this->assertInstanceOf( |
||
712 | SearchResult::class, |
||
713 | $searchResult |
||
714 | ); |
||
715 | |||
716 | // 4 trashed locations from the sub tree, but only 2 in results |
||
717 | $this->assertCount(2, $searchResult->items); |
||
718 | $this->assertEquals(4, $searchResult->count); |
||
719 | $this->assertEquals(4, $searchResult->totalCount); |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * Test for the findTrashItems() method. |
||
724 | * |
||
725 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
726 | * @depends \eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
727 | */ |
||
728 | public function testFindTrashItemsLimitedAccess() |
||
762 | |||
763 | /** |
||
764 | * Test for the emptyTrash() method. |
||
765 | * |
||
766 | * @see \eZ\Publish\API\Repository\TrashService::emptyTrash() |
||
767 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
768 | */ |
||
769 | public function testEmptyTrash() |
||
798 | |||
799 | /** |
||
800 | * Test for the emptyTrash() method with user which has subtree limitations. |
||
801 | * |
||
802 | * @see \eZ\Publish\API\Repository\TrashService::emptyTrash() |
||
803 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
804 | */ |
||
805 | public function testEmptyTrashForUserWithSubtreeLimitation() |
||
847 | |||
848 | /** |
||
849 | * Test for the deleteTrashItem() method. |
||
850 | * |
||
851 | * @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem() |
||
852 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
853 | */ |
||
854 | public function testDeleteTrashItem() |
||
904 | |||
905 | /** |
||
906 | * Test deleting a non existing trash item. |
||
907 | * |
||
908 | * @covers \eZ\Publish\API\Repository\TrashService::deleteTrashItem |
||
909 | */ |
||
910 | View Code Duplication | public function testDeleteThrowsNotFoundExceptionForNonExistingTrashItem() |
|
923 | |||
924 | /** |
||
925 | * Returns an array with the remoteIds of all child locations of the |
||
926 | * <b>Community</b> location. It is stored in a local variable named |
||
927 | * <b>$remoteIds</b>. |
||
928 | * |
||
929 | * @return string[] |
||
930 | */ |
||
931 | private function createRemoteIdList() |
||
954 | |||
955 | /** |
||
956 | * @param Repository $repository |
||
957 | * @param int $parentLocationId |
||
958 | * |
||
959 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
960 | */ |
||
961 | View Code Duplication | protected function createNewContentInPlaceTrashedOne(Repository $repository, $parentLocationId) |
|
977 | |||
978 | /** |
||
979 | * @param URLAliasService $urlAliasService |
||
980 | * @param string $urlPath Url alias path |
||
981 | * |
||
982 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
983 | */ |
||
984 | private function assertAliasExists(URLAliasService $urlAliasService, $urlPath) |
||
992 | |||
993 | /** |
||
994 | * @param URLAliasService $urlAliasService |
||
995 | * @param string $urlPath Url alias path |
||
996 | */ |
||
997 | private function assertAliasNotExists(URLAliasService $urlAliasService, $urlPath) |
||
1006 | |||
1007 | /** |
||
1008 | * Get Test Double for TrashItem for exception testing and similar. |
||
1009 | * |
||
1010 | * @param int $trashId |
||
1011 | * @param int $contentId |
||
1012 | * @param int $parentLocationId |
||
1013 | * |
||
1014 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
1015 | */ |
||
1016 | private function getTrashItemDouble(int $trashId, int $contentId = 44, int $parentLocationId = 2): APITrashItem |
||
1024 | } |
||
1025 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: