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 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 93 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 94 | */ |
||
| 95 | public function testTrashRemovesLocationFromMainStorage() |
||
| 96 | { |
||
| 97 | $repository = $this->getRepository(); |
||
| 98 | |||
| 99 | $mediaRemoteId = '75c715a51699d2d309a924eca6a95145'; |
||
| 100 | |||
| 101 | /* BEGIN: Use Case */ |
||
| 102 | $this->createTrashItem(); |
||
| 103 | |||
| 104 | // Load the location service |
||
| 105 | $locationService = $repository->getLocationService(); |
||
| 106 | |||
| 107 | // This call will fail with a "NotFoundException", because the media |
||
| 108 | // location was marked as trashed in the main storage |
||
| 109 | $locationService->loadLocationByRemoteId($mediaRemoteId); |
||
| 110 | /* END: Use Case */ |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Test for the trash() method. |
||
| 115 | * |
||
| 116 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 117 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 118 | */ |
||
| 119 | public function testTrashRemovesChildLocationsFromMainStorage() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Test for the trash() method. |
||
| 151 | * |
||
| 152 | * @see \eZ\Publish\API\Repository\TrashService::trash() |
||
| 153 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 154 | */ |
||
| 155 | public function testTrashDecrementsChildCountOnParentLocation() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Test sending a location to trash updates Content mainLocation. |
||
| 178 | * |
||
| 179 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
| 180 | */ |
||
| 181 | public function testTrashUpdatesMainLocation() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Test sending a location to trash. |
||
| 208 | * |
||
| 209 | * @covers \eZ\Publish\API\Repository\TrashService::trash |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function testTrashReturnsNull() |
|
| 212 | { |
||
| 213 | $repository = $this->getRepository(); |
||
| 214 | $contentService = $repository->getContentService(); |
||
| 215 | $locationService = $repository->getLocationService(); |
||
| 216 | $trashService = $repository->getTrashService(); |
||
| 217 | |||
| 218 | // Create additional location to trash |
||
| 219 | $location = $locationService->createLocation( |
||
| 220 | $contentService->loadContentInfo(42), |
||
| 221 | new LocationCreateStruct(['parentLocationId' => 2]) |
||
| 222 | ); |
||
| 223 | |||
| 224 | $trashItem = $trashService->trash($location); |
||
| 225 | |||
| 226 | self::assertNull($trashItem); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Test for the loadTrashItem() method. |
||
| 231 | * |
||
| 232 | * @covers \eZ\Publish\API\Repository\TrashService::loadTrashItem |
||
| 233 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 234 | */ |
||
| 235 | public function testLoadTrashItem() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Test for the loadTrashItem() method. |
||
| 286 | * |
||
| 287 | * @see \eZ\Publish\API\Repository\TrashService::loadTrashItem() |
||
| 288 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 289 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testLoadTrashItem |
||
| 290 | */ |
||
| 291 | public function testLoadTrashItemThrowsNotFoundException() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Test for the recover() method. |
||
| 307 | * |
||
| 308 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 309 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 310 | */ |
||
| 311 | public function testRecover() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Test recovering a non existing trash item results in a NotFoundException. |
||
| 351 | * |
||
| 352 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 353 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 354 | */ |
||
| 355 | public function testRecoverThrowsNotFoundExceptionForNonExistingTrashItem() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Test for the trash() method. |
||
| 371 | * |
||
| 372 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 373 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 374 | * |
||
| 375 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 376 | */ |
||
| 377 | public function testNotFoundAliasAfterRemoveIt() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Test for the recover() method. |
||
| 398 | * |
||
| 399 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 400 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 401 | */ |
||
| 402 | public function testAliasesForRemovedItems() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Test for the recover() method. |
||
| 441 | * |
||
| 442 | * @see \eZ\Publish\API\Repository\TrashService::recover() |
||
| 443 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 444 | */ |
||
| 445 | public function testRecoverDoesNotRestoreChildLocations() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Test for the recover() method. |
||
| 491 | * |
||
| 492 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
| 493 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 494 | * |
||
| 495 | * @todo Fix naming |
||
| 496 | */ |
||
| 497 | public function testRecoverWithLocationCreateStructParameter() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Test for the recover() method. |
||
| 543 | * |
||
| 544 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem) |
||
| 545 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecover |
||
| 546 | */ |
||
| 547 | public function testRecoverIncrementsChildCountOnOriginalParent() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Test for the recover() method. |
||
| 583 | * |
||
| 584 | * @see \eZ\Publish\API\Repository\TrashService::recover($trashItem, $newParentLocation) |
||
| 585 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testRecoverWithLocationCreateStructParameter |
||
| 586 | */ |
||
| 587 | public function testRecoverWithLocationCreateStructParameterIncrementsChildCountOnNewParent() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Test recovering a location from trash to non existing location. |
||
| 629 | * |
||
| 630 | * @covers \eZ\Publish\API\Repository\TrashService::recover |
||
| 631 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 632 | */ |
||
| 633 | View Code Duplication | public function testRecoverToNonExistingLocation() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Test for the findTrashItems() method. |
||
| 653 | * |
||
| 654 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
| 655 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testTrash |
||
| 656 | */ |
||
| 657 | public function testFindTrashItems() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Test for the findTrashItems() method. |
||
| 689 | * |
||
| 690 | * @see \eZ\Publish\API\Repository\TrashService::findTrashItems() |
||
| 691 | * @depends \eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 692 | */ |
||
| 693 | public function testFindTrashItemsLimitedAccess() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Test for the emptyTrash() method. |
||
| 730 | * |
||
| 731 | * @see \eZ\Publish\API\Repository\TrashService::emptyTrash() |
||
| 732 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 733 | */ |
||
| 734 | public function testEmptyTrash() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Test for the emptyTrash() method with user which has subtree limitations. |
||
| 766 | * |
||
| 767 | * @see \eZ\Publish\API\Repository\TrashService::emptyTrash() |
||
| 768 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 769 | */ |
||
| 770 | public function testEmptyTrashForUserWithSubtreeLimitation() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Test for the deleteTrashItem() method. |
||
| 815 | * |
||
| 816 | * @see \eZ\Publish\API\Repository\TrashService::deleteTrashItem() |
||
| 817 | * @depends eZ\Publish\API\Repository\Tests\TrashServiceTest::testFindTrashItems |
||
| 818 | */ |
||
| 819 | public function testDeleteTrashItem() |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Test deleting a non existing trash item. |
||
| 872 | * |
||
| 873 | * @covers \eZ\Publish\API\Repository\TrashService::deleteTrashItem |
||
| 874 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 875 | */ |
||
| 876 | public function testDeleteThrowsNotFoundExceptionForNonExistingTrashItem() |
||
| 887 | |||
| 888 | /** |
||
| 889 | * Returns an array with the remoteIds of all child locations of the |
||
| 890 | * <b>Community</b> location. It is stored in a local variable named |
||
| 891 | * <b>$remoteIds</b>. |
||
| 892 | * |
||
| 893 | * @return string[] |
||
| 894 | */ |
||
| 895 | private function createRemoteIdList() |
||
| 918 | |||
| 919 | /** |
||
| 920 | * @param Repository $repository |
||
| 921 | * @param int $parentLocationId |
||
| 922 | * |
||
| 923 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 924 | */ |
||
| 925 | protected function createNewContentInPlaceTrashedOne(Repository $repository, $parentLocationId) |
||
| 926 | { |
||
| 927 | $contentService = $repository->getContentService(); |
||
| 928 | $locationService = $repository->getLocationService(); |
||
| 929 | $contentTypeService = $repository->getContentTypeService(); |
||
| 930 | |||
| 931 | $contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
||
| 932 | $newContent = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
||
| 933 | $newContent->setField('name', 'Media'); |
||
| 934 | |||
| 935 | $location = $locationService->newLocationCreateStruct($parentLocationId); |
||
| 936 | |||
| 937 | $draftContent = $contentService->createContent($newContent, [$location]); |
||
| 938 | |||
| 939 | return $contentService->publishVersion($draftContent->versionInfo); |
||
| 940 | } |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @param URLAliasService $urlAliasService |
||
| 944 | * @param string $urlPath Url alias path |
||
| 945 | * |
||
| 946 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 947 | */ |
||
| 948 | private function assertAliasExists(URLAliasService $urlAliasService, $urlPath) |
||
| 956 | |||
| 957 | /** |
||
| 958 | * @param URLAliasService $urlAliasService |
||
| 959 | * @param string $urlPath Url alias path |
||
| 960 | */ |
||
| 961 | private function assertAliasNotExists(URLAliasService $urlAliasService, $urlPath) |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Get Test Double for TrashItem for exception testing and similar. |
||
| 973 | * |
||
| 974 | * @param int $trashId |
||
| 975 | * @param int $contentId |
||
| 976 | * @param int $parentLocationId |
||
| 977 | * |
||
| 978 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 979 | */ |
||
| 980 | private function getTrashItemDouble(int $trashId, int $contentId = 44, int $parentLocationId = 2): APITrashItem |
||
| 988 | } |
||
| 989 |
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: