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 |
||
27 | class ContentHandlerTest extends HandlerTest |
||
28 | { |
||
29 | /** |
||
30 | * @return array |
||
31 | */ |
||
32 | public function providerForUnCachedMethods() |
||
33 | { |
||
34 | return array( |
||
35 | array('create', array(new CreateStruct())), |
||
36 | array('createDraftFromVersion', array(2, 1, 14)), |
||
37 | array('copy', array(2, 1)), |
||
38 | //array( 'load', array( 2, 1, array( 'eng-GB' ) ) ), |
||
39 | //array( 'load', array( 2, 1 ) ), |
||
40 | //array( 'loadContentInfo', array( 2 ) ), |
||
41 | //array('loadVersionInfo', array(2, 1)), |
||
42 | array('loadDraftsForUser', array(14)), |
||
43 | //array( 'setStatus', array( 2, 0, 1 ) ), |
||
44 | //array( 'updateMetadata', array( 2, new MetadataUpdateStruct ) ), |
||
45 | //array( 'updateContent', array( 2, 1, new UpdateStruct ) ), |
||
46 | //array( 'deleteContent', array( 2 ) ), |
||
47 | //array( 'deleteVersion', array( 2, 1 ) ), |
||
48 | array('listVersions', array(2)), |
||
49 | array('addRelation', array(new RelationCreateStruct())), |
||
50 | array('removeRelation', array(66, APIRelation::COMMON)), |
||
51 | array('loadRelations', array(2, 1, 3)), |
||
52 | array('loadReverseRelations', array(2, 3)), |
||
53 | //array( 'publish', array( 2, 3, new MetadataUpdateStruct ) ), |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @dataProvider providerForUnCachedMethods |
||
59 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler |
||
60 | */ |
||
61 | View Code Duplication | public function testUnCachedMethods($method, array $arguments) |
|
91 | |||
92 | /** |
||
93 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::load |
||
94 | */ |
||
95 | public function testLoadCacheIsMiss() |
||
96 | { |
||
97 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
98 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
99 | $this->cacheMock |
||
100 | ->expects($this->once()) |
||
101 | ->method('getItem') |
||
102 | ->with('content', 2, 1, 'eng-GB|eng-US') |
||
103 | ->will($this->returnValue($cacheItemMock)); |
||
104 | |||
105 | $cacheItemMock |
||
106 | ->expects($this->once()) |
||
107 | ->method('get') |
||
108 | ->will($this->returnValue(null)); |
||
109 | |||
110 | $cacheItemMock |
||
111 | ->expects($this->once()) |
||
112 | ->method('isMiss') |
||
113 | ->will($this->returnValue(true)); |
||
114 | |||
115 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Handler'); |
||
116 | $this->persistenceHandlerMock |
||
117 | ->expects($this->once()) |
||
118 | ->method('contentHandler') |
||
119 | ->will($this->returnValue($innerHandlerMock)); |
||
120 | |||
121 | $innerHandlerMock |
||
122 | ->expects($this->once()) |
||
123 | ->method('load') |
||
124 | ->with(2, 1, array('eng-GB', 'eng-US')) |
||
125 | ->will( |
||
126 | $this->returnValue( |
||
127 | new Content( |
||
128 | array( |
||
129 | 'fields' => array(), |
||
130 | 'versionInfo' => new VersionInfo( |
||
131 | array( |
||
132 | 'versionNo' => 1, |
||
133 | 'contentInfo' => new ContentInfo(array('id' => 2)), |
||
134 | ) |
||
135 | ), |
||
136 | ) |
||
137 | ) |
||
138 | ) |
||
139 | ); |
||
140 | |||
141 | $cacheItemMock |
||
142 | ->expects($this->once()) |
||
143 | ->method('set') |
||
144 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content')) |
||
145 | ->will($this->returnValue($cacheItemMock)); |
||
146 | |||
147 | $cacheItemMock |
||
148 | ->expects($this->once()) |
||
149 | ->method('save') |
||
150 | ->with(); |
||
151 | |||
152 | $handler = $this->persistenceCacheHandler->contentHandler(); |
||
153 | $handler->load(2, 1, array('eng-GB', 'eng-US')); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::load |
||
158 | */ |
||
159 | public function testLoadHasCache() |
||
204 | |||
205 | /** |
||
206 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadContentInfo |
||
207 | */ |
||
208 | public function testLoadContentInfoCacheIsMiss() |
||
209 | { |
||
210 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
211 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
212 | $this->cacheMock |
||
213 | ->expects($this->once()) |
||
214 | ->method('getItem') |
||
215 | ->with('content', 'info', 2) |
||
216 | ->will($this->returnValue($cacheItemMock)); |
||
217 | |||
218 | $cacheItemMock |
||
219 | ->expects($this->once()) |
||
220 | ->method('get') |
||
221 | ->will($this->returnValue(null)); |
||
222 | |||
223 | $cacheItemMock |
||
224 | ->expects($this->once()) |
||
225 | ->method('isMiss') |
||
226 | ->will($this->returnValue(true)); |
||
227 | |||
228 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Handler'); |
||
229 | $this->persistenceHandlerMock |
||
230 | ->expects($this->once()) |
||
231 | ->method('contentHandler') |
||
232 | ->will($this->returnValue($innerHandlerMock)); |
||
233 | |||
234 | $innerHandlerMock |
||
235 | ->expects($this->once()) |
||
236 | ->method('loadContentInfo') |
||
237 | ->with(2) |
||
238 | ->will( |
||
239 | $this->returnValue( |
||
240 | new ContentInfo(array('id' => 2)) |
||
241 | ) |
||
242 | ); |
||
243 | |||
244 | $cacheItemMock |
||
245 | ->expects($this->once()) |
||
246 | ->method('set') |
||
247 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ContentInfo')) |
||
248 | ->will($this->returnValue($cacheItemMock)); |
||
249 | |||
250 | $cacheItemMock |
||
251 | ->expects($this->once()) |
||
252 | ->method('save') |
||
253 | ->with(); |
||
254 | |||
255 | $handler = $this->persistenceCacheHandler->contentHandler(); |
||
256 | $handler->loadContentInfo(2, 1); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadContentInfo |
||
261 | */ |
||
262 | public function testLoadContentInfoHasCache() |
||
297 | |||
298 | /** |
||
299 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadVersionInfo |
||
300 | */ |
||
301 | public function testLoadVersionInfoCacheIsMiss() |
||
351 | |||
352 | /** |
||
353 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::loadVersionInfo |
||
354 | */ |
||
355 | public function testLoadVersionInfoHasCache() |
||
356 | { |
||
357 | $this->loggerMock->expects($this->never())->method($this->anything()); |
||
358 | $cacheItemMock = $this->getMock(ItemInterface::class); |
||
359 | $this->cacheMock |
||
360 | ->expects($this->once()) |
||
361 | ->method('getItem') |
||
362 | ->with('content', 'info', 2, 'versioninfo', 1) |
||
363 | ->will($this->returnValue($cacheItemMock)); |
||
364 | |||
365 | $cacheItemMock |
||
366 | ->expects($this->once()) |
||
367 | ->method('isMiss') |
||
368 | ->will($this->returnValue(false)); |
||
369 | |||
370 | $this->persistenceHandlerMock |
||
371 | ->expects($this->never()) |
||
372 | ->method('contentHandler'); |
||
373 | |||
374 | $cacheItemMock |
||
375 | ->expects($this->once()) |
||
376 | ->method('get') |
||
377 | ->will( |
||
378 | $this->returnValue( |
||
379 | new VersionInfo(['contentInfo' => new ContentInfo(['id' => 2]), 'versionNo' => 1]) |
||
380 | ) |
||
381 | ); |
||
382 | |||
383 | $cacheItemMock |
||
384 | ->expects($this->never()) |
||
385 | ->method('set'); |
||
386 | |||
387 | $handler = $this->persistenceCacheHandler->contentHandler(); |
||
388 | $handler->loadVersionInfo(2, 1); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::setStatus |
||
393 | */ |
||
394 | public function testSetStatus() |
||
395 | { |
||
396 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
397 | |||
398 | $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Handler'); |
||
399 | $this->persistenceHandlerMock |
||
400 | ->expects($this->once()) |
||
401 | ->method('contentHandler') |
||
402 | ->will($this->returnValue($innerHandlerMock)); |
||
403 | |||
404 | $innerHandlerMock |
||
405 | ->expects($this->once()) |
||
406 | ->method('setStatus') |
||
407 | ->with(2, VersionInfo::STATUS_ARCHIVED, 1) |
||
408 | ->will($this->returnValue(true)); |
||
409 | |||
410 | $this->cacheMock |
||
411 | ->expects($this->at(0)) |
||
412 | ->method('clear') |
||
413 | ->with('content', 2, 1) |
||
414 | ->will($this->returnValue(null)); |
||
415 | |||
416 | $this->cacheMock |
||
417 | ->expects($this->at(1)) |
||
418 | ->method('clear') |
||
419 | ->with('content', 'info', 2, 'versioninfo', 1) |
||
420 | ->will($this->returnValue(null)); |
||
421 | |||
422 | $handler = $this->persistenceCacheHandler->contentHandler(); |
||
423 | $handler->setStatus(2, VersionInfo::STATUS_ARCHIVED, 1); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::setStatus |
||
428 | */ |
||
429 | View Code Duplication | public function testSetStatusPublished() |
|
460 | |||
461 | /** |
||
462 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::updateMetadata |
||
463 | */ |
||
464 | public function testUpdateMetadata() |
||
501 | |||
502 | /** |
||
503 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::updateContent |
||
504 | */ |
||
505 | public function testUpdateContent() |
||
550 | |||
551 | /** |
||
552 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::deleteContent |
||
553 | */ |
||
554 | public function testDeleteContent() |
||
633 | |||
634 | /** |
||
635 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::deleteVersion |
||
636 | */ |
||
637 | public function testDeleteVersion() |
||
680 | |||
681 | /** |
||
682 | * @covers \eZ\Publish\Core\Persistence\Cache\ContentHandler::publish |
||
683 | */ |
||
684 | public function testPublish() |
||
785 | } |
||
786 |