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() |
||
| 602 | { |
||
| 603 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 604 | $this->cacheMock |
||
| 605 | ->expects($this->at(0)) |
||
| 606 | ->method('clear') |
||
| 607 | ->with('location', 33) |
||
| 608 | ->will($this->returnValue(true)); |
||
| 609 | |||
| 610 | $this->cacheMock |
||
| 611 | ->expects($this->at(1)) |
||
| 612 | ->method('clear') |
||
| 613 | ->with('location', 66) |
||
| 614 | ->will($this->returnValue(true)); |
||
| 615 | |||
| 616 | $this->cacheMock |
||
| 617 | ->expects($this->at(2)) |
||
| 618 | ->method('clear') |
||
| 619 | ->with('location', 'subtree') |
||
| 620 | ->will($this->returnValue(true)); |
||
| 621 | |||
| 622 | $this->cacheMock |
||
| 623 | ->expects($this->at(3)) |
||
| 624 | ->method('clear') |
||
| 625 | ->with('content', 'locations') |
||
| 626 | ->will($this->returnValue(true)); |
||
| 627 | |||
| 628 | $this->cacheMock |
||
| 629 | ->expects($this->at(4)) |
||
| 630 | ->method('clear') |
||
| 631 | ->with('user', 'role', 'assignments', 'byGroup') |
||
| 632 | ->will($this->returnValue(true)); |
||
| 633 | |||
| 634 | $this->cacheMock |
||
| 635 | ->expects($this->at(5)) |
||
| 636 | ->method('clear') |
||
| 637 | ->with('content', $this->isType('integer')) |
||
| 638 | ->will($this->returnValue(true)); |
||
| 639 | |||
| 640 | $this->cacheMock |
||
| 641 | ->expects($this->at(6)) |
||
| 642 | ->method('clear') |
||
| 643 | ->with('content', $this->isType('integer')) |
||
| 644 | ->will($this->returnValue(true)); |
||
| 645 | |||
| 646 | $this->cacheMock |
||
| 647 | ->expects($this->at(7)) |
||
| 648 | ->method('clear') |
||
| 649 | ->with('content', 'info', $this->isType('integer')) |
||
| 650 | ->will($this->returnValue(true)); |
||
| 651 | |||
| 652 | $this->cacheMock |
||
| 653 | ->expects($this->at(8)) |
||
| 654 | ->method('clear') |
||
| 655 | ->with('content', 'info', $this->isType('integer')) |
||
| 656 | ->will($this->returnValue(true)); |
||
| 657 | |||
| 658 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
| 659 | $this->persistenceHandlerMock |
||
| 660 | ->expects($this->once()) |
||
| 661 | ->method('locationHandler') |
||
| 662 | ->will($this->returnValue($innerHandlerMock)); |
||
| 663 | |||
| 664 | $innerHandlerMock |
||
| 665 | ->expects($this->once()) |
||
| 666 | ->method('swap') |
||
| 667 | ->with(33, 66) |
||
| 668 | ->will($this->returnValue(true)); |
||
| 669 | |||
| 670 | $locationMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\Location'); |
||
| 671 | $locationMock |
||
| 672 | ->expects($this->any()) |
||
| 673 | ->method('__get') |
||
| 674 | ->with('contentId') |
||
| 675 | ->will($this->returnValue(42)); |
||
| 676 | |||
| 677 | /** @var \eZ\Publish\SPI\Persistence\Content\Location\Handler|\PHPUnit_Framework_MockObject_MockObject $handler */ |
||
| 678 | $handler = $this |
||
| 679 | ->getMockBuilder('eZ\\Publish\\Core\\Persistence\\Cache\\LocationHandler') |
||
| 680 | ->setMethods(['load']) |
||
| 681 | ->setConstructorArgs( |
||
| 682 | [ |
||
| 683 | $this->cacheMock, |
||
| 684 | $this->persistenceHandlerMock, |
||
| 685 | $this->loggerMock, |
||
| 686 | ] |
||
| 687 | ) |
||
| 688 | ->getMock(); |
||
| 689 | |||
| 690 | $handler |
||
| 691 | ->expects($this->any()) |
||
| 692 | ->method('load') |
||
| 693 | ->will($this->returnValue($locationMock)); |
||
| 694 | |||
| 695 | $handler->swap(33, 66); |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::update |
||
| 700 | */ |
||
| 701 | public function testUpdate() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::create |
||
| 737 | */ |
||
| 738 | public function testCreate() |
||
| 814 | |||
| 815 | /** |
||
| 816 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::removeSubtree |
||
| 817 | */ |
||
| 818 | public function testRemoveSubtree() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::setSectionForSubtree |
||
| 857 | */ |
||
| 858 | View Code Duplication | public function testSetSectionForSubtree() |
|
| 881 | |||
| 882 | /** |
||
| 883 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::changeMainLocation |
||
| 884 | */ |
||
| 885 | View Code Duplication | public function testChangeMainLocation() |
|
| 916 | } |
||
| 917 |