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 |
||
18 | class UrlAliasHandlerTest extends HandlerTest |
||
19 | { |
||
20 | /** |
||
21 | * @return array |
||
22 | */ |
||
23 | public function providerForUnCachedMethods() |
||
24 | { |
||
25 | return array( |
||
26 | //array( 'publishUrlAliasForLocation', array( 44, 2, 'name', 'eng-GB', true ) ), |
||
27 | //array( 'createCustomUrlAlias', array( 44, '/path', true, 'eng-GB', true ) ), |
||
28 | //array( 'createGlobalUrlAlias', array( '/old', '/path', true, 'eng-GB', true ) ), |
||
29 | array('listGlobalURLAliases', array('eng-GB', 10, 5)), |
||
30 | //array( 'listURLAliasesForLocation', array( 44, true ) ), |
||
31 | //array( 'removeURLAliases', array( array( 1, 2 ) ) ), |
||
32 | //array( 'lookup', array( '/url' ) ), |
||
33 | //array( 'loadUrlAlias', array( 88 ) ), |
||
34 | //array( 'locationMoved', array( 44, 2, 45 ) ), |
||
35 | //array( 'locationCopied', array( 44, 2, 45 ) ), |
||
36 | //array( 'locationDeleted', array( 44 ) ), |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @dataProvider providerForUnCachedMethods |
||
42 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler |
||
43 | */ |
||
44 | public function testUnCachedMethods($method, array $arguments) |
||
78 | |||
79 | /** |
||
80 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::publishUrlAliasForLocation |
||
81 | */ |
||
82 | public function testPublishUrlAliasForLocationWithoutCachedLocation() |
||
83 | { |
||
84 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
85 | |||
86 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
87 | $cacheItem = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
88 | |||
89 | $this->persistenceHandlerMock |
||
90 | ->expects($this->once()) |
||
91 | ->method('urlAliasHandler') |
||
92 | ->will($this->returnValue($innerHandler)); |
||
93 | |||
94 | $innerHandler |
||
95 | ->expects($this->once()) |
||
96 | ->method('publishUrlAliasForLocation') |
||
97 | ->with(44, 2, 'name', 'eng-GB', true) |
||
98 | ->will($this->returnValue(new UrlAlias(array('id' => 55)))); |
||
99 | |||
100 | $cacheItem |
||
101 | ->expects($this->once()) |
||
102 | ->method('isMiss') |
||
103 | ->willReturn(true); |
||
104 | $cacheItem |
||
105 | ->expects($this->never()) |
||
106 | ->method('clear'); |
||
107 | |||
108 | $this->cacheMock |
||
109 | ->expects($this->once()) |
||
110 | ->method('clear') |
||
111 | ->with('urlAlias') |
||
112 | ->will($this->returnValue(null)); |
||
113 | $this->cacheMock |
||
114 | ->expects($this->once()) |
||
115 | ->method('getItem') |
||
116 | ->with('urlAlias', 'location', 44) |
||
117 | ->willReturn($cacheItem); |
||
118 | |||
119 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
120 | $handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::publishUrlAliasForLocation |
||
125 | */ |
||
126 | public function testPublishUrlAliasForLocationWithCachedLocation() |
||
127 | { |
||
128 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
129 | |||
130 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
131 | $cacheItem = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
132 | |||
133 | $this->persistenceHandlerMock |
||
134 | ->expects($this->once()) |
||
135 | ->method('urlAliasHandler') |
||
136 | ->will($this->returnValue($innerHandler)); |
||
137 | |||
138 | $innerHandler |
||
139 | ->expects($this->once()) |
||
140 | ->method('publishUrlAliasForLocation') |
||
141 | ->with(44, 2, 'name', 'eng-GB', true) |
||
142 | ->will($this->returnValue(new UrlAlias(array('id' => 55)))); |
||
143 | |||
144 | $cacheItem |
||
145 | ->expects($this->once()) |
||
146 | ->method('isMiss') |
||
147 | ->willReturn(false); |
||
148 | $cacheItem |
||
149 | ->expects($this->once()) |
||
150 | ->method('clear'); |
||
151 | $cacheItem |
||
152 | ->expects($this->once()) |
||
153 | ->method('get') |
||
154 | ->willReturn([44]); |
||
155 | |||
156 | $this->cacheMock |
||
157 | ->expects($this->once()) |
||
158 | ->method('getItem') |
||
159 | ->with('urlAlias', 'location', 44) |
||
160 | ->willReturn($cacheItem); |
||
161 | $this->cacheMock |
||
162 | ->expects($this->at(1)) |
||
163 | ->method('clear') |
||
164 | ->with('urlAlias', 44) |
||
165 | ->will($this->returnValue(null)); |
||
166 | $this->cacheMock |
||
167 | ->expects($this->at(2)) |
||
168 | ->method('clear') |
||
169 | ->with('urlAlias', 'url') |
||
170 | ->will($this->returnValue(null)); |
||
171 | |||
172 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
173 | $handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createCustomUrlAlias |
||
178 | */ |
||
179 | public function testCreateCustomUrlAliasHasCache() |
||
180 | { |
||
181 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
182 | |||
183 | $urlAlias = new UrlAlias(array('id' => 55, 'destination' => 44)); |
||
184 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
185 | $this->persistenceHandlerMock |
||
186 | ->expects($this->once()) |
||
187 | ->method('urlAliasHandler') |
||
188 | ->will($this->returnValue($innerHandler)); |
||
189 | |||
190 | $innerHandler |
||
191 | ->expects($this->once()) |
||
192 | ->method('createCustomUrlAlias') |
||
193 | ->with(44, '/path', true, 'eng-GB', true) |
||
194 | ->will($this->returnValue($urlAlias)); |
||
195 | |||
196 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
197 | $this->cacheMock |
||
198 | ->expects($this->at(0)) |
||
199 | ->method('getItem') |
||
200 | ->with('urlAlias', 55) |
||
201 | ->will($this->returnValue($cacheItemMock)); |
||
202 | |||
203 | $cacheItemMock |
||
204 | ->expects($this->never()) |
||
205 | ->method('get'); |
||
206 | $cacheItemMock |
||
207 | ->expects($this->once()) |
||
208 | ->method('set') |
||
209 | ->with($urlAlias) |
||
210 | ->will($this->returnValue($cacheItemMock)); |
||
211 | |||
212 | $cacheItemMock |
||
213 | ->expects($this->once()) |
||
214 | ->method('save') |
||
215 | ->with(); |
||
216 | |||
217 | $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
218 | $this->cacheMock |
||
219 | ->expects($this->at(1)) |
||
220 | ->method('getItem') |
||
221 | ->with('urlAlias', 'location', 44, 'custom') |
||
222 | ->will($this->returnValue($cacheItemMock2)); |
||
223 | |||
224 | $cacheItemMock2 |
||
225 | ->expects($this->once()) |
||
226 | ->method('get') |
||
227 | ->will($this->returnValue(array(42))); |
||
228 | |||
229 | $cacheItemMock2 |
||
230 | ->expects($this->once()) |
||
231 | ->method('isMiss') |
||
232 | ->will($this->returnValue(false)); |
||
233 | |||
234 | $cacheItemMock2 |
||
235 | ->expects($this->once()) |
||
236 | ->method('set') |
||
237 | ->with(array(42, 55)) |
||
238 | ->will($this->returnValue($cacheItemMock2)); |
||
239 | |||
240 | $cacheItemMock2 |
||
241 | ->expects($this->once()) |
||
242 | ->method('save') |
||
243 | ->with(); |
||
244 | |||
245 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
246 | $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createCustomUrlAlias |
||
251 | */ |
||
252 | public function testCreateCustomUrlAliasIsMiss() |
||
253 | { |
||
254 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
255 | |||
256 | $urlAlias = new UrlAlias(array('id' => 55, 'destination' => 44)); |
||
257 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
258 | $this->persistenceHandlerMock |
||
259 | ->expects($this->once()) |
||
260 | ->method('urlAliasHandler') |
||
261 | ->will($this->returnValue($innerHandler)); |
||
262 | |||
263 | $innerHandler |
||
264 | ->expects($this->once()) |
||
265 | ->method('createCustomUrlAlias') |
||
266 | ->with(44, '/path', true, 'eng-GB', true) |
||
267 | ->will($this->returnValue($urlAlias)); |
||
268 | |||
269 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
270 | $this->cacheMock |
||
271 | ->expects($this->at(0)) |
||
272 | ->method('getItem') |
||
273 | ->with('urlAlias', 55) |
||
274 | ->will($this->returnValue($cacheItemMock)); |
||
275 | |||
276 | $cacheItemMock |
||
277 | ->expects($this->never()) |
||
278 | ->method('get'); |
||
279 | $cacheItemMock |
||
280 | ->expects($this->once()) |
||
281 | ->method('set') |
||
282 | ->with($urlAlias) |
||
283 | ->will($this->returnValue($cacheItemMock)); |
||
284 | |||
285 | $cacheItemMock |
||
286 | ->expects($this->once()) |
||
287 | ->method('save') |
||
288 | ->with(); |
||
289 | |||
290 | $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
291 | $this->cacheMock |
||
292 | ->expects($this->at(1)) |
||
293 | ->method('getItem') |
||
294 | ->with('urlAlias', 'location', 44, 'custom') |
||
295 | ->will($this->returnValue($cacheItemMock2)); |
||
296 | |||
297 | $cacheItemMock2 |
||
298 | ->expects($this->once()) |
||
299 | ->method('get') |
||
300 | ->will($this->returnValue(null)); |
||
301 | |||
302 | $cacheItemMock2 |
||
303 | ->expects($this->once()) |
||
304 | ->method('isMiss') |
||
305 | ->will($this->returnValue(true)); |
||
306 | |||
307 | $cacheItemMock2 |
||
308 | ->expects($this->once()) |
||
309 | ->method('set') |
||
310 | ->with(array(55)) |
||
311 | ->will($this->returnValue($cacheItemMock2)); |
||
312 | |||
313 | $cacheItemMock2 |
||
314 | ->expects($this->once()) |
||
315 | ->method('save') |
||
316 | ->with(); |
||
317 | |||
318 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
319 | $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createGlobalUrlAlias |
||
324 | */ |
||
325 | public function testCreateGlobalUrlAlias() |
||
326 | { |
||
327 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
328 | |||
329 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
330 | $this->persistenceHandlerMock |
||
331 | ->expects($this->once()) |
||
332 | ->method('urlAliasHandler') |
||
333 | ->will($this->returnValue($innerHandler)); |
||
334 | |||
335 | $innerHandler |
||
336 | ->expects($this->once()) |
||
337 | ->method('createGlobalUrlAlias') |
||
338 | ->with('/old', '/path', true, 'eng-GB', true) |
||
339 | ->will($this->returnValue(new UrlAlias(array('id' => 55)))); |
||
340 | |||
341 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
342 | $this->cacheMock |
||
343 | ->expects($this->once()) |
||
344 | ->method('getItem') |
||
345 | ->with('urlAlias', 55) |
||
346 | ->will($this->returnValue($cacheItemMock)); |
||
347 | |||
348 | $cacheItemMock |
||
349 | ->expects($this->once()) |
||
350 | ->method('set') |
||
351 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias')) |
||
352 | ->will($this->returnValue($cacheItemMock)); |
||
353 | |||
354 | $cacheItemMock |
||
355 | ->expects($this->once()) |
||
356 | ->method('save') |
||
357 | ->with(); |
||
358 | |||
359 | $cacheItemMock |
||
360 | ->expects($this->never()) |
||
361 | ->method('get'); |
||
362 | |||
363 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
364 | $handler->createGlobalUrlAlias('/old', '/path', true, 'eng-GB', true); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation |
||
369 | */ |
||
370 | View Code Duplication | public function testListURLAliasesForLocationIsMiss() |
|
371 | { |
||
372 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
373 | |||
374 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
375 | $this->cacheMock |
||
376 | ->expects($this->once()) |
||
377 | ->method('getItem') |
||
378 | ->with('urlAlias', 'location', 44) |
||
379 | ->will($this->returnValue($cacheItemMock)); |
||
380 | |||
381 | $cacheItemMock |
||
382 | ->expects($this->once()) |
||
383 | ->method('get') |
||
384 | ->will($this->returnValue(null)); |
||
385 | |||
386 | $cacheItemMock |
||
387 | ->expects($this->once()) |
||
388 | ->method('isMiss') |
||
389 | ->will($this->returnValue(true)); |
||
390 | |||
391 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
392 | $this->persistenceHandlerMock |
||
393 | ->expects($this->once()) |
||
394 | ->method('urlAliasHandler') |
||
395 | ->will($this->returnValue($innerHandler)); |
||
396 | |||
397 | $innerHandler |
||
398 | ->expects($this->once()) |
||
399 | ->method('listURLAliasesForLocation') |
||
400 | ->with(44, false) |
||
401 | ->will( |
||
402 | $this->returnValue( |
||
403 | array( |
||
404 | new UrlAlias(array('id' => 55)), |
||
405 | new UrlAlias(array('id' => 58)), |
||
406 | new UrlAlias(array('id' => 91)), |
||
407 | ) |
||
408 | ) |
||
409 | ); |
||
410 | |||
411 | $cacheItemMock |
||
412 | ->expects($this->once()) |
||
413 | ->method('set') |
||
414 | ->with(array(55, 58, 91)) |
||
415 | ->will($this->returnValue($cacheItemMock)); |
||
416 | |||
417 | $cacheItemMock |
||
418 | ->expects($this->once()) |
||
419 | ->method('save') |
||
420 | ->with(); |
||
421 | |||
422 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
423 | $handler->listURLAliasesForLocation(44, false); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation |
||
428 | */ |
||
429 | View Code Duplication | public function testListURLAliasesForLocationCustomIsMiss() |
|
430 | { |
||
431 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
432 | |||
433 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
434 | $this->cacheMock |
||
435 | ->expects($this->once()) |
||
436 | ->method('getItem') |
||
437 | ->with('urlAlias', 'location', '44', 'custom') |
||
438 | ->will($this->returnValue($cacheItemMock)); |
||
439 | |||
440 | $cacheItemMock |
||
441 | ->expects($this->once()) |
||
442 | ->method('get') |
||
443 | ->will($this->returnValue(null)); |
||
444 | |||
445 | $cacheItemMock |
||
446 | ->expects($this->once()) |
||
447 | ->method('isMiss') |
||
448 | ->will($this->returnValue(true)); |
||
449 | |||
450 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
451 | $this->persistenceHandlerMock |
||
452 | ->expects($this->once()) |
||
453 | ->method('urlAliasHandler') |
||
454 | ->will($this->returnValue($innerHandler)); |
||
455 | |||
456 | $innerHandler |
||
457 | ->expects($this->once()) |
||
458 | ->method('listURLAliasesForLocation') |
||
459 | ->with(44, true) |
||
460 | ->will( |
||
461 | $this->returnValue( |
||
462 | array( |
||
463 | new UrlAlias(array('id' => 55)), |
||
464 | new UrlAlias(array('id' => 58)), |
||
465 | new UrlAlias(array('id' => 91)), |
||
466 | ) |
||
467 | ) |
||
468 | ); |
||
469 | |||
470 | $cacheItemMock |
||
471 | ->expects($this->once()) |
||
472 | ->method('set') |
||
473 | ->with(array(55, 58, 91)) |
||
474 | ->will($this->returnValue($cacheItemMock)); |
||
475 | |||
476 | $cacheItemMock |
||
477 | ->expects($this->once()) |
||
478 | ->method('save') |
||
479 | ->with(); |
||
480 | |||
481 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
482 | $handler->listURLAliasesForLocation(44, true); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation |
||
487 | */ |
||
488 | View Code Duplication | public function testListURLAliasesForLocationHasCache() |
|
564 | |||
565 | /** |
||
566 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation |
||
567 | */ |
||
568 | View Code Duplication | public function testListURLAliasesForLocationCustomHasCache() |
|
644 | |||
645 | /** |
||
646 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::removeURLAliases |
||
647 | */ |
||
648 | public function testRemoveURLAliases() |
||
649 | { |
||
650 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
651 | |||
690 | |||
691 | /** |
||
692 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
693 | */ |
||
694 | public function testLookupIsMissActive() |
||
781 | |||
782 | /** |
||
783 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
784 | */ |
||
785 | public function testLookupIsMissHistory() |
||
854 | |||
855 | /** |
||
856 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
857 | */ |
||
858 | View Code Duplication | public function testLookupHasCache() |
|
911 | |||
912 | /** |
||
913 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
914 | */ |
||
915 | public function testLookupHasHistoryCache() |
||
963 | |||
964 | /** |
||
965 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias |
||
966 | */ |
||
967 | View Code Duplication | public function testLoadUrlAliasIsMiss() |
|
1014 | |||
1015 | /** |
||
1016 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias |
||
1017 | */ |
||
1018 | View Code Duplication | public function testLoadUrlAliasHasCache() |
|
1050 | |||
1051 | /** |
||
1052 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationMoved |
||
1053 | */ |
||
1054 | View Code Duplication | public function testLocationMoved() |
|
1078 | |||
1079 | /** |
||
1080 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted |
||
1081 | */ |
||
1082 | public function testLocationDeletedWithoutCachedLocation() |
||
1103 | |||
1104 | /** |
||
1105 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted |
||
1106 | */ |
||
1107 | public function testLocationDeletedWithCachedLocation() |
||
1138 | |||
1139 | /** |
||
1140 | * @param $locationCacheMissed |
||
1141 | */ |
||
1142 | View Code Duplication | protected function prepareDeleteMocks($locationCacheMissed) |
|
1159 | } |
||
1160 |