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 |
||
| 20 | class LocationHandlerTest extends HandlerTest |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::load |
||
| 24 | */ |
||
| 25 | View Code Duplication | public function testLoadCacheIsMiss() |
|
| 26 | { |
||
| 27 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 28 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 29 | $this->cacheMock |
||
| 30 | ->expects($this->once()) |
||
| 31 | ->method('getItem') |
||
| 32 | ->with('location', 33) |
||
| 33 | ->will($this->returnValue($cacheItemMock)); |
||
| 34 | |||
| 35 | $cacheItemMock |
||
| 36 | ->expects($this->once()) |
||
| 37 | ->method('get') |
||
| 38 | ->will($this->returnValue(null)); |
||
| 39 | |||
| 40 | $cacheItemMock |
||
| 41 | ->expects($this->once()) |
||
| 42 | ->method('isMiss') |
||
| 43 | ->will($this->returnValue(true)); |
||
| 44 | |||
| 45 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 46 | $this->persistenceHandlerMock |
||
| 47 | ->expects($this->once()) |
||
| 48 | ->method('locationHandler') |
||
| 49 | ->will($this->returnValue($innerHandlerMock)); |
||
| 50 | |||
| 51 | $innerHandlerMock |
||
| 52 | ->expects($this->once()) |
||
| 53 | ->method('load') |
||
| 54 | ->with(33) |
||
| 55 | ->will($this->returnValue(new Location(array('id' => 33)))); |
||
| 56 | |||
| 57 | $cacheItemMock |
||
| 58 | ->expects($this->once()) |
||
| 59 | ->method('set') |
||
| 60 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Location')) |
||
| 61 | ->will($this->returnValue($cacheItemMock)); |
||
| 62 | |||
| 63 | $cacheItemMock |
||
| 64 | ->expects($this->once()) |
||
| 65 | ->method('save') |
||
| 66 | ->with(); |
||
| 67 | |||
| 68 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
| 69 | $handler->load(33); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::load |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function testLoadHasCache() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadLocationsByContent |
||
| 109 | */ |
||
| 110 | public function testLoadLocationsByContentIsMiss() |
||
| 111 | { |
||
| 112 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 113 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 114 | $this->cacheMock |
||
| 115 | ->expects($this->once()) |
||
| 116 | ->method('getItem') |
||
| 117 | ->with('content', 'locations', 44) |
||
| 118 | ->will($this->returnValue($cacheItemMock)); |
||
| 119 | |||
| 120 | $cacheItemMock |
||
| 121 | ->expects($this->once()) |
||
| 122 | ->method('get') |
||
| 123 | ->will($this->returnValue(null)); |
||
| 124 | |||
| 125 | $cacheItemMock |
||
| 126 | ->expects($this->once()) |
||
| 127 | ->method('isMiss') |
||
| 128 | ->will($this->returnValue(true)); |
||
| 129 | |||
| 130 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 131 | $this->persistenceHandlerMock |
||
| 132 | ->expects($this->once()) |
||
| 133 | ->method('locationHandler') |
||
| 134 | ->will($this->returnValue($innerHandlerMock)); |
||
| 135 | |||
| 136 | $innerHandlerMock |
||
| 137 | ->expects($this->once()) |
||
| 138 | ->method('loadLocationsByContent') |
||
| 139 | ->with(44) |
||
| 140 | ->will($this->returnValue(array(new Location(array('id' => 33))))); |
||
| 141 | |||
| 142 | $cacheItemMock |
||
| 143 | ->expects($this->once()) |
||
| 144 | ->method('set') |
||
| 145 | ->with(array(33)) |
||
| 146 | ->will($this->returnValue($cacheItemMock)); |
||
| 147 | |||
| 148 | $cacheItemMock |
||
| 149 | ->expects($this->once()) |
||
| 150 | ->method('save') |
||
| 151 | ->with(); |
||
| 152 | |||
| 153 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
| 154 | $handler->loadLocationsByContent(44); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadLocationsByContent |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function testLoadLocationsByContentHasCache() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadParentLocationsForDraftContent |
||
| 217 | */ |
||
| 218 | View Code Duplication | public function testLoadParentLocationsForDraftContentIsMiss() |
|
| 219 | { |
||
| 220 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 221 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 222 | $this->cacheMock |
||
| 223 | ->expects($this->once()) |
||
| 224 | ->method('getItem') |
||
| 225 | ->with('content', 'locations', '44', 'parentLocationsForDraftContent') |
||
| 226 | ->will($this->returnValue($cacheItemMock)); |
||
| 227 | |||
| 228 | $cacheItemMock |
||
| 229 | ->expects($this->once()) |
||
| 230 | ->method('get') |
||
| 231 | ->will($this->returnValue(null)); |
||
| 232 | |||
| 233 | $cacheItemMock |
||
| 234 | ->expects($this->once()) |
||
| 235 | ->method('isMiss') |
||
| 236 | ->will($this->returnValue(true)); |
||
| 237 | |||
| 238 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 239 | $this->persistenceHandlerMock |
||
| 240 | ->expects($this->once()) |
||
| 241 | ->method('locationHandler') |
||
| 242 | ->will($this->returnValue($innerHandlerMock)); |
||
| 243 | |||
| 244 | $innerHandlerMock |
||
| 245 | ->expects($this->once()) |
||
| 246 | ->method('loadParentLocationsForDraftContent') |
||
| 247 | ->with(44) |
||
| 248 | ->will($this->returnValue(array(new Location(array('id' => 33))))); |
||
| 249 | |||
| 250 | $cacheItemMock |
||
| 251 | ->expects($this->once()) |
||
| 252 | ->method('set') |
||
| 253 | ->with(array(33)) |
||
| 254 | ->will($this->returnValue($cacheItemMock)); |
||
| 255 | |||
| 256 | $cacheItemMock |
||
| 257 | ->expects($this->once()) |
||
| 258 | ->method('save') |
||
| 259 | ->with(); |
||
| 260 | |||
| 261 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
| 262 | $handler->loadParentLocationsForDraftContent(44); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadParentLocationsForDraftContent |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function testLoadParentLocationsForDraftContentHasCache() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadLocationsByContent |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function testLoadLocationsByContentWithRootIsMiss() |
|
| 327 | { |
||
| 328 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 329 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 330 | $this->cacheMock |
||
| 331 | ->expects($this->once()) |
||
| 332 | ->method('getItem') |
||
| 333 | ->with('content', 'locations', '44', 'root', '2') |
||
| 334 | ->will($this->returnValue($cacheItemMock)); |
||
| 335 | |||
| 336 | $cacheItemMock |
||
| 337 | ->expects($this->once()) |
||
| 338 | ->method('get') |
||
| 339 | ->will($this->returnValue(null)); |
||
| 340 | |||
| 341 | $cacheItemMock |
||
| 342 | ->expects($this->once()) |
||
| 343 | ->method('isMiss') |
||
| 344 | ->will($this->returnValue(true)); |
||
| 345 | |||
| 346 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 347 | $this->persistenceHandlerMock |
||
| 348 | ->expects($this->once()) |
||
| 349 | ->method('locationHandler') |
||
| 350 | ->will($this->returnValue($innerHandlerMock)); |
||
| 351 | |||
| 352 | $innerHandlerMock |
||
| 353 | ->expects($this->once()) |
||
| 354 | ->method('loadLocationsByContent') |
||
| 355 | ->with(44, 2) |
||
| 356 | ->will($this->returnValue(array(new Location(array('id' => 33))))); |
||
| 357 | |||
| 358 | $cacheItemMock |
||
| 359 | ->expects($this->once()) |
||
| 360 | ->method('set') |
||
| 361 | ->with(array(33)) |
||
| 362 | ->will($this->returnValue($cacheItemMock)); |
||
| 363 | |||
| 364 | $cacheItemMock |
||
| 365 | ->expects($this->once()) |
||
| 366 | ->method('save') |
||
| 367 | ->with(); |
||
| 368 | |||
| 369 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
| 370 | $handler->loadLocationsByContent(44, 2); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadLocationsByContent |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function testLoadLocationsByContentWithRootHasCache() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::loadByRemoteId |
||
| 433 | */ |
||
| 434 | public function testLoadByRemoteId() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::copySubtree |
||
| 459 | */ |
||
| 460 | public function testCopySubtree() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::move |
||
| 485 | */ |
||
| 486 | View Code Duplication | public function testMove() |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::markSubtreeModified |
||
| 519 | */ |
||
| 520 | public function testMarkSubtreeModified() |
||
| 542 | /** |
||
| 543 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::hide |
||
| 544 | */ |
||
| 545 | public function testHide() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::unhide |
||
| 572 | */ |
||
| 573 | public function testUnhide() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::swap |
||
| 600 | */ |
||
| 601 | public function testSwap() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::update |
||
| 652 | */ |
||
| 653 | public function testUpdate() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::create |
||
| 689 | */ |
||
| 690 | public function testCreate() |
||
| 691 | { |
||
| 692 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 693 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
| 694 | $this->cacheMock |
||
| 695 | ->expects($this->once()) |
||
| 696 | ->method('getItem') |
||
| 697 | ->with('location', 33) |
||
| 698 | ->will($this->returnValue($cacheItemMock)); |
||
| 699 | |||
| 700 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 701 | $this->persistenceHandlerMock |
||
| 702 | ->expects($this->once()) |
||
| 703 | ->method('locationHandler') |
||
| 704 | ->will($this->returnValue($innerHandlerMock)); |
||
| 705 | |||
| 706 | $innerHandlerMock |
||
| 707 | ->expects($this->once()) |
||
| 708 | ->method('create') |
||
| 709 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\CreateStruct')) |
||
| 710 | ->will($this->returnValue(new Location(array('id' => 33, 'contentId' => 2)))); |
||
| 711 | |||
| 712 | $cacheItemMock |
||
| 713 | ->expects($this->once()) |
||
| 714 | ->method('set') |
||
| 715 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Location')) |
||
| 716 | ->will($this->returnValue($cacheItemMock)); |
||
| 717 | |||
| 718 | $cacheItemMock |
||
| 719 | ->expects($this->once()) |
||
| 720 | ->method('save') |
||
| 721 | ->with(); |
||
| 722 | |||
| 723 | $cacheItemMock |
||
| 724 | ->expects($this->never()) |
||
| 725 | ->method('get'); |
||
| 726 | |||
| 727 | $this->cacheMock |
||
| 728 | ->expects($this->at(1)) |
||
| 729 | ->method('clear') |
||
| 730 | ->with('location', 'subtree') |
||
| 731 | ->will($this->returnValue(true)); |
||
| 732 | |||
| 733 | $this->cacheMock |
||
| 734 | ->expects($this->at(2)) |
||
| 735 | ->method('clear') |
||
| 736 | ->with('content', 'locations', 2) |
||
| 737 | ->will($this->returnValue(true)); |
||
| 738 | |||
| 739 | $this->cacheMock |
||
| 740 | ->expects($this->at(3)) |
||
| 741 | ->method('clear') |
||
| 742 | ->with('content', 2) |
||
| 743 | ->will($this->returnValue(true)); |
||
| 744 | |||
| 745 | $this->cacheMock |
||
| 746 | ->expects($this->at(4)) |
||
| 747 | ->method('clear') |
||
| 748 | ->with('content', 'info', 2) |
||
| 749 | ->will($this->returnValue(true)); |
||
| 750 | |||
| 751 | $this->cacheMock |
||
| 752 | ->expects($this->at(5)) |
||
| 753 | ->method('clear') |
||
| 754 | ->with('user', 'role', 'assignments', 'byGroup', 2) |
||
| 755 | ->will($this->returnValue(true)); |
||
| 756 | |||
| 757 | $this->cacheMock |
||
| 758 | ->expects($this->at(6)) |
||
| 759 | ->method('clear') |
||
| 760 | ->with('user', 'role', 'assignments', 'byGroup', 'inherited', 2) |
||
| 761 | ->will($this->returnValue(true)); |
||
| 762 | |||
| 763 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
| 764 | $handler->create(new CreateStruct()); |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::removeSubtree |
||
| 769 | */ |
||
| 770 | public function testRemoveSubtree() |
||
| 806 | |||
| 807 | /** |
||
| 808 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::setSectionForSubtree |
||
| 809 | */ |
||
| 810 | View Code Duplication | public function testSetSectionForSubtree() |
|
| 833 | |||
| 834 | /** |
||
| 835 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::changeMainLocation |
||
| 836 | */ |
||
| 837 | View Code Duplication | public function testChangeMainLocation() |
|
| 868 | } |
||
| 869 |