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('content', $this->isType('integer')) |
||
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', 'info', $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('user', 'role', 'assignments', 'byGroup') |
||
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 | $innerHandlerMock |
||
678 | ->expects($this->any()) |
||
679 | ->method('load') |
||
680 | ->will($this->returnValue($locationMock)); |
||
681 | |||
682 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
683 | $handler->swap(33, 66); |
||
684 | } |
||
685 | |||
686 | /** |
||
687 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::update |
||
688 | */ |
||
689 | public function testUpdate() |
||
722 | |||
723 | /** |
||
724 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::create |
||
725 | */ |
||
726 | public function testCreate() |
||
727 | { |
||
728 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
729 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
730 | $this->cacheMock |
||
731 | ->expects($this->once()) |
||
732 | ->method('getItem') |
||
733 | ->with('location', 33) |
||
734 | ->will($this->returnValue($cacheItemMock)); |
||
735 | |||
736 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\Handler'); |
||
737 | $this->persistenceHandlerMock |
||
738 | ->expects($this->once()) |
||
739 | ->method('locationHandler') |
||
740 | ->will($this->returnValue($innerHandlerMock)); |
||
741 | |||
742 | $innerHandlerMock |
||
743 | ->expects($this->once()) |
||
744 | ->method('create') |
||
745 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Location\\CreateStruct')) |
||
746 | ->will($this->returnValue(new Location(array('id' => 33, 'contentId' => 2)))); |
||
747 | |||
748 | $cacheItemMock |
||
749 | ->expects($this->once()) |
||
750 | ->method('set') |
||
751 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Location')) |
||
752 | ->will($this->returnValue($cacheItemMock)); |
||
753 | |||
754 | $cacheItemMock |
||
755 | ->expects($this->once()) |
||
756 | ->method('save') |
||
757 | ->with(); |
||
758 | |||
759 | $cacheItemMock |
||
760 | ->expects($this->never()) |
||
761 | ->method('get'); |
||
762 | |||
763 | $this->cacheMock |
||
764 | ->expects($this->at(1)) |
||
765 | ->method('clear') |
||
766 | ->with('location', 'subtree') |
||
767 | ->will($this->returnValue(true)); |
||
768 | |||
769 | $this->cacheMock |
||
770 | ->expects($this->at(2)) |
||
771 | ->method('clear') |
||
772 | ->with('content', 'locations', 2) |
||
773 | ->will($this->returnValue(true)); |
||
774 | |||
775 | $this->cacheMock |
||
776 | ->expects($this->at(3)) |
||
777 | ->method('clear') |
||
778 | ->with('content', 2) |
||
779 | ->will($this->returnValue(true)); |
||
780 | |||
781 | $this->cacheMock |
||
782 | ->expects($this->at(4)) |
||
783 | ->method('clear') |
||
784 | ->with('content', 'info', 2) |
||
785 | ->will($this->returnValue(true)); |
||
786 | |||
787 | $this->cacheMock |
||
788 | ->expects($this->at(5)) |
||
789 | ->method('clear') |
||
790 | ->with('user', 'role', 'assignments', 'byGroup', 2) |
||
791 | ->will($this->returnValue(true)); |
||
792 | |||
793 | $this->cacheMock |
||
794 | ->expects($this->at(6)) |
||
795 | ->method('clear') |
||
796 | ->with('user', 'role', 'assignments', 'byGroup', 'inherited', 2) |
||
797 | ->will($this->returnValue(true)); |
||
798 | |||
799 | $handler = $this->persistenceCacheHandler->locationHandler(); |
||
800 | $handler->create(new CreateStruct()); |
||
801 | } |
||
802 | |||
803 | /** |
||
804 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::removeSubtree |
||
805 | */ |
||
806 | public function testRemoveSubtree() |
||
842 | |||
843 | /** |
||
844 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::setSectionForSubtree |
||
845 | */ |
||
846 | View Code Duplication | public function testSetSectionForSubtree() |
|
869 | |||
870 | /** |
||
871 | * @covers eZ\Publish\Core\Persistence\Cache\LocationHandler::changeMainLocation |
||
872 | */ |
||
873 | View Code Duplication | public function testChangeMainLocation() |
|
904 | } |
||
905 |