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 ObjectStateHandlerTest extends HandlerTest |
||
21 | { |
||
22 | /** |
||
23 | * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::createGroup |
||
24 | */ |
||
25 | public function testCreateGroup() |
||
26 | { |
||
27 | $inputStruct = new SPIInputStruct( |
||
28 | array('identifier' => 'test_state_group', 'name' => 'Test State Group') |
||
29 | ); |
||
30 | $expectedGroup = new SPIObjectStateGroup( |
||
31 | array( |
||
32 | 'id' => 1, |
||
33 | 'identifier' => 'test_state_group', |
||
34 | 'name' => 'Test State Group', |
||
35 | ) |
||
36 | ); |
||
37 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
38 | |||
39 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
40 | $this->persistenceHandlerMock |
||
41 | ->expects($this->once()) |
||
42 | ->method('objectStateHandler') |
||
43 | ->will($this->returnValue($innerHandler)); |
||
44 | |||
45 | $innerHandler |
||
46 | ->expects($this->once()) |
||
47 | ->method('createGroup') |
||
48 | ->with($inputStruct) |
||
49 | ->will( |
||
50 | $this->returnValue($expectedGroup) |
||
51 | ); |
||
52 | |||
53 | $this->cacheMock |
||
54 | ->expects($this->once()) |
||
55 | ->method('clear') |
||
56 | ->with('objectstategroup', 'all') |
||
57 | ->will($this->returnValue(null)); |
||
58 | |||
59 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
60 | $this->cacheMock |
||
61 | ->expects($this->once()) |
||
62 | ->method('getItem') |
||
63 | ->with('objectstategroup', 1) |
||
64 | ->will($this->returnValue($cacheItemMock)); |
||
65 | |||
66 | $cacheItemMock |
||
67 | ->expects($this->once()) |
||
68 | ->method('set') |
||
69 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Group')) |
||
70 | ->will($this->returnValue($cacheItemMock)); |
||
71 | |||
72 | $cacheItemMock |
||
73 | ->expects($this->once()) |
||
74 | ->method('save') |
||
75 | ->with(); |
||
76 | |||
77 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
78 | $group = $handler->createGroup($inputStruct); |
||
79 | $this->assertEquals($group, $expectedGroup); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup |
||
84 | */ |
||
85 | View Code Duplication | public function testLoadGroup() |
|
86 | { |
||
87 | $expectedGroup = new SPIObjectStateGroup( |
||
88 | array( |
||
89 | 'id' => 1, |
||
90 | 'identifier' => 'test_state_group', |
||
91 | 'name' => 'Test State Group', |
||
92 | ) |
||
93 | ); |
||
94 | |||
95 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
96 | $this->cacheMock |
||
97 | ->expects($this->once()) |
||
98 | ->method('getItem') |
||
99 | ->with('objectstategroup', 1) |
||
100 | ->will($this->returnValue($cacheItemMock)); |
||
101 | $cacheItemMock |
||
102 | ->expects($this->once()) |
||
103 | ->method('get') |
||
104 | ->will($this->returnValue(null)); |
||
105 | $cacheItemMock |
||
106 | ->expects($this->once()) |
||
107 | ->method('isMiss') |
||
108 | ->will($this->returnValue(true)); |
||
109 | $cacheItemMock |
||
110 | ->expects($this->once()) |
||
111 | ->method('set') |
||
112 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Group')) |
||
113 | ->will($this->returnValue($cacheItemMock)); |
||
114 | $cacheItemMock |
||
115 | ->expects($this->once()) |
||
116 | ->method('save') |
||
117 | ->with(); |
||
118 | |||
119 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
120 | |||
121 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
122 | $this->persistenceHandlerMock |
||
123 | ->expects($this->once()) |
||
124 | ->method('objectStateHandler') |
||
125 | ->will($this->returnValue($innerHandlerMock)); |
||
126 | |||
127 | $innerHandlerMock |
||
128 | ->expects($this->once()) |
||
129 | ->method('loadGroup') |
||
130 | ->with(1) |
||
131 | ->will($this->returnValue($expectedGroup)); |
||
132 | |||
133 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
134 | $group = $handler->loadGroup(1); |
||
135 | $this->assertEquals($group, $expectedGroup); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup |
||
140 | * @depends testLoadGroup |
||
141 | */ |
||
142 | View Code Duplication | public function testLoadGroupHasCache() |
|
167 | |||
168 | /** |
||
169 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroupByIdentifier |
||
170 | */ |
||
171 | public function testLoadGroupByIdentifier() |
||
199 | |||
200 | public function generateObjectGroupsArray() |
||
215 | |||
216 | /** |
||
217 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups |
||
218 | */ |
||
219 | public function testLoadAllGroups($offset = 0, $limit = -1) |
||
220 | { |
||
221 | $testGroups = $this->generateObjectGroupsArray(); |
||
222 | $testGroupIds = array_map( |
||
223 | function ($group) { |
||
224 | return $group->id; |
||
225 | }, |
||
226 | $testGroups |
||
227 | ); |
||
228 | |||
229 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
230 | $this->cacheMock |
||
231 | ->expects($this->at(0)) |
||
232 | ->method('getItem') |
||
233 | ->with('objectstategroup', 'all') |
||
234 | ->will($this->returnValue($cacheItemMock)); |
||
235 | $cacheItemMock |
||
236 | ->expects($this->once()) |
||
237 | ->method('get') |
||
238 | ->will($this->returnValue(null)); |
||
239 | $cacheItemMock |
||
240 | ->expects($this->once()) |
||
241 | ->method('isMiss') |
||
242 | ->will($this->returnValue(true)); |
||
243 | |||
244 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
245 | |||
246 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
247 | $this->persistenceHandlerMock |
||
248 | ->expects($this->once()) |
||
249 | ->method('objectStateHandler') |
||
250 | ->will($this->returnValue($innerHandlerMock)); |
||
251 | |||
252 | $innerHandlerMock |
||
253 | ->expects($this->once()) |
||
254 | ->method('loadAllGroups') |
||
255 | ->with(0, -1) |
||
256 | ->will($this->returnValue($testGroups)); |
||
257 | |||
258 | foreach ($testGroups as $group) { |
||
259 | $this->cacheMock |
||
260 | ->expects($this->at($group->id)) |
||
261 | ->method('getItem') |
||
262 | ->with('objectstategroup', $group->id) |
||
263 | ->will( |
||
264 | $this->returnCallback( |
||
265 | function ($cachekey, $i) use ($group) { |
||
266 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
267 | $cacheItemMock |
||
268 | ->expects($this->once()) |
||
269 | ->method('set') |
||
270 | ->with($group) |
||
271 | ->will($this->returnValue($cacheItemMock)); |
||
272 | |||
273 | $cacheItemMock |
||
274 | ->expects($this->once()) |
||
275 | ->method('save') |
||
276 | ->with(); |
||
277 | |||
278 | return $cacheItemMock; |
||
279 | } |
||
280 | ) |
||
281 | ); |
||
282 | } |
||
283 | |||
284 | $cacheItemMock |
||
285 | ->expects($this->once()) |
||
286 | ->method('set') |
||
287 | ->with($testGroupIds) |
||
288 | ->will($this->returnValue($cacheItemMock)); |
||
289 | |||
290 | $cacheItemMock |
||
291 | ->expects($this->once()) |
||
292 | ->method('save') |
||
293 | ->with(); |
||
294 | |||
295 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
296 | $groups = $handler->loadAllGroups($offset, $limit); |
||
297 | |||
298 | $expectedGroups = array_slice($testGroups, $offset, $limit > -1 ?: null); |
||
299 | $this->assertEquals($groups, $expectedGroups); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups |
||
304 | */ |
||
305 | public function testLoadAllGroupsCached($offset = 0, $limit = -1) |
||
357 | |||
358 | /** |
||
359 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups |
||
360 | */ |
||
361 | public function testLoadAllGroupsWithOffsetLimit() |
||
365 | |||
366 | /** |
||
367 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups |
||
368 | */ |
||
369 | public function testLoadAllGroupsCachedWithOffsetLimit() |
||
373 | |||
374 | /** |
||
375 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates |
||
376 | */ |
||
377 | public function testLoadObjectStates() |
||
378 | { |
||
379 | $testStates = array( |
||
380 | new SPIObjectState( |
||
381 | array( |
||
382 | 'id' => 1, |
||
383 | 'identifier' => 'test_state_1_group1', |
||
384 | 'groupId' => 1, |
||
385 | ) |
||
386 | ), |
||
387 | new SPIObjectState( |
||
388 | array( |
||
389 | 'id' => 2, |
||
390 | 'identifier' => 'test_state_2_group1', |
||
391 | 'groupId' => 1, |
||
392 | ) |
||
393 | ), |
||
394 | new SPIObjectState( |
||
395 | array( |
||
396 | 'id' => 3, |
||
397 | 'identifier' => 'test_state_3_group1', |
||
398 | 'groupId' => 1, |
||
399 | ) |
||
400 | ), |
||
401 | ); |
||
402 | $testStateIds = array_map( |
||
403 | function ($state) { |
||
404 | return $state->id; |
||
405 | }, |
||
406 | $testStates |
||
407 | ); |
||
408 | |||
409 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
410 | $this->cacheMock |
||
411 | ->expects($this->at(0)) |
||
412 | ->method('getItem') |
||
413 | ->with('objectstate', 'byGroup', 1) |
||
414 | ->will($this->returnValue($cacheItemMock)); |
||
415 | $cacheItemMock |
||
416 | ->expects($this->once()) |
||
417 | ->method('get') |
||
418 | ->will($this->returnValue(null)); |
||
419 | $cacheItemMock |
||
420 | ->expects($this->once()) |
||
421 | ->method('isMiss') |
||
422 | ->will($this->returnValue(true)); |
||
423 | |||
424 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
425 | |||
426 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
427 | $this->persistenceHandlerMock |
||
428 | ->expects($this->once()) |
||
429 | ->method('objectStateHandler') |
||
430 | ->will($this->returnValue($innerHandlerMock)); |
||
431 | |||
432 | $innerHandlerMock |
||
433 | ->expects($this->once()) |
||
434 | ->method('loadObjectStates') |
||
435 | ->with(1) |
||
436 | ->will($this->returnValue($testStates)); |
||
437 | |||
438 | $cacheItemMock |
||
439 | ->expects($this->once()) |
||
440 | ->method('set') |
||
441 | ->with($testStateIds) |
||
442 | ->will($this->returnValue($cacheItemMock)); |
||
443 | |||
444 | $cacheItemMock |
||
445 | ->expects($this->once()) |
||
446 | ->method('save') |
||
447 | ->with(); |
||
448 | |||
449 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
450 | $states = $handler->loadObjectStates(1); |
||
451 | $this->assertEquals($states, $testStates); |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates |
||
456 | */ |
||
457 | public function testLoadObjectStatesCached() |
||
529 | |||
530 | /** |
||
531 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup |
||
532 | */ |
||
533 | public function testUpdateGroup() |
||
570 | |||
571 | /** |
||
572 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup |
||
573 | */ |
||
574 | public function testDeleteGroup() |
||
606 | |||
607 | /** |
||
608 | * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create |
||
609 | */ |
||
610 | public function testCreate() |
||
648 | |||
649 | /** |
||
650 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load |
||
651 | */ |
||
652 | View Code Duplication | public function testLoad() |
|
653 | { |
||
654 | $expectedState = new SPIObjectState( |
||
655 | array( |
||
656 | 'id' => 1, |
||
657 | 'identifier' => 'test_state', |
||
658 | 'name' => 'Test State', |
||
659 | 'groupId' => 1, |
||
660 | ) |
||
661 | ); |
||
662 | |||
663 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
664 | $this->cacheMock |
||
665 | ->expects($this->once()) |
||
666 | ->method('getItem') |
||
667 | ->with('objectstate', 1) |
||
668 | ->will($this->returnValue($cacheItemMock)); |
||
669 | $cacheItemMock |
||
670 | ->expects($this->once()) |
||
671 | ->method('get') |
||
672 | ->will($this->returnValue(null)); |
||
673 | $cacheItemMock |
||
674 | ->expects($this->once()) |
||
675 | ->method('isMiss') |
||
676 | ->will($this->returnValue(true)); |
||
677 | $cacheItemMock |
||
678 | ->expects($this->once()) |
||
679 | ->method('set') |
||
680 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState')) |
||
681 | ->will($this->returnValue($cacheItemMock)); |
||
682 | $cacheItemMock |
||
683 | ->expects($this->once()) |
||
684 | ->method('save') |
||
685 | ->with(); |
||
686 | |||
687 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
688 | |||
689 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
690 | $this->persistenceHandlerMock |
||
691 | ->expects($this->once()) |
||
692 | ->method('objectStateHandler') |
||
693 | ->will($this->returnValue($innerHandlerMock)); |
||
694 | |||
695 | $innerHandlerMock |
||
696 | ->expects($this->once()) |
||
697 | ->method('load') |
||
698 | ->with(1) |
||
699 | ->will($this->returnValue($expectedState)); |
||
700 | |||
701 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
702 | $state = $handler->load(1); |
||
703 | $this->assertEquals($state, $expectedState); |
||
704 | } |
||
705 | |||
706 | /** |
||
707 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load |
||
708 | * @depends testLoad |
||
709 | */ |
||
710 | View Code Duplication | public function testLoadCached() |
|
736 | |||
737 | /** |
||
738 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier |
||
739 | */ |
||
740 | public function testLoadByIdentifier() |
||
769 | |||
770 | /** |
||
771 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update |
||
772 | */ |
||
773 | public function testUpdate() |
||
812 | |||
813 | /** |
||
814 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority |
||
815 | */ |
||
816 | public function testSetPriority() |
||
852 | |||
853 | /** |
||
854 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete |
||
855 | */ |
||
856 | public function testDelete() |
||
884 | |||
885 | /** |
||
886 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState |
||
887 | */ |
||
888 | public function testSetContentState() |
||
912 | |||
913 | /** |
||
914 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState |
||
915 | */ |
||
916 | public function testGetContentState() |
||
917 | { |
||
918 | $expectedState = new SPIObjectState( |
||
919 | array( |
||
920 | 'id' => 1, |
||
921 | 'identifier' => 'test_state', |
||
922 | 'name' => 'Test State', |
||
923 | 'groupId' => 1, |
||
924 | 'priority' => 1, |
||
925 | ) |
||
926 | ); |
||
927 | |||
928 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
929 | $this->cacheMock |
||
930 | ->expects($this->at(0)) |
||
931 | ->method('getItem') |
||
932 | ->with('objectstate', 'byContent', 10, 1) |
||
933 | ->will($this->returnValue($cacheItemMock)); |
||
934 | $cacheItemMock |
||
935 | ->expects($this->once()) |
||
936 | ->method('get') |
||
937 | ->will($this->returnValue(null)); |
||
938 | $cacheItemMock |
||
939 | ->expects($this->once()) |
||
940 | ->method('isMiss') |
||
941 | ->will($this->returnValue(true)); |
||
942 | $cacheItemMock |
||
943 | ->expects($this->once()) |
||
944 | ->method('set') |
||
945 | ->with(1) |
||
946 | ->will($this->returnValue($cacheItemMock)); |
||
947 | $cacheItemMock |
||
948 | ->expects($this->once()) |
||
949 | ->method('save') |
||
950 | ->with(); |
||
951 | |||
952 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
953 | |||
954 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler'); |
||
955 | $this->persistenceHandlerMock |
||
956 | ->expects($this->once()) |
||
957 | ->method('objectStateHandler') |
||
958 | ->will($this->returnValue($innerHandlerMock)); |
||
959 | |||
960 | $innerHandlerMock |
||
961 | ->expects($this->once()) |
||
962 | ->method('getContentState') |
||
963 | ->with(10, 1) |
||
964 | ->will($this->returnValue($expectedState)); |
||
965 | |||
966 | $handler = $this->persistenceCacheHandler->objectStateHandler(); |
||
967 | $state = $handler->getContentState(10, 1, 2); |
||
968 | $this->assertEquals($state, $expectedState); |
||
969 | } |
||
970 | |||
971 | /** |
||
972 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState |
||
973 | */ |
||
974 | public function testGetContentStateCached() |
||
1023 | |||
1024 | /** |
||
1025 | * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount |
||
1026 | */ |
||
1027 | public function testGetContentCount() |
||
1051 | } |
||
1052 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.