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 | |||
652 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
653 | $this->persistenceHandlerMock |
||
654 | ->expects($this->once()) |
||
655 | ->method('urlAliasHandler') |
||
656 | ->will($this->returnValue($innerHandler)); |
||
657 | |||
658 | $innerHandler |
||
659 | ->expects($this->once()) |
||
660 | ->method('removeURLAliases'); |
||
661 | |||
662 | $this->cacheMock |
||
663 | ->expects($this->at(0)) |
||
664 | ->method('clear') |
||
665 | ->with('urlAlias', 'url'); |
||
666 | |||
667 | $this->cacheMock |
||
668 | ->expects($this->at(1)) |
||
669 | ->method('clear') |
||
670 | ->with('urlAlias', 21); |
||
671 | |||
672 | $this->cacheMock |
||
673 | ->expects($this->at(2)) |
||
674 | ->method('clear') |
||
675 | ->with('urlAlias', 32); |
||
676 | |||
677 | $this->cacheMock |
||
678 | ->expects($this->at(3)) |
||
679 | ->method('clear') |
||
680 | ->with('urlAlias', 'location', 44); |
||
681 | |||
682 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
683 | $handler->removeURLAliases( |
||
684 | array( |
||
685 | new UrlAlias(array('id' => 21)), |
||
686 | new UrlAlias(array('id' => 32, 'type' => UrlAlias::LOCATION, 'destination' => 44)), |
||
687 | ) |
||
688 | ); |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
693 | * @group justme |
||
694 | */ |
||
695 | public function testLookupIsMiss() |
||
696 | { |
||
697 | $urlAlias = new UrlAlias(array('id' => 55)); |
||
698 | |||
699 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
700 | |||
701 | $missedUrlAliasIdCacheItem = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
702 | $missedUrlAliasIdCacheItem |
||
703 | ->expects($this->once()) |
||
704 | ->method('get') |
||
705 | ->will($this->returnValue(null)); |
||
706 | |||
707 | $missedUrlAliasIdCacheItem |
||
708 | ->expects($this->once()) |
||
709 | ->method('isMiss') |
||
710 | ->will($this->returnValue(true)); |
||
711 | |||
712 | $missedUrlAliasIdCacheItem |
||
713 | ->expects($this->once()) |
||
714 | ->method('set') |
||
715 | ->with(55) |
||
716 | ->will($this->returnValue($missedUrlAliasIdCacheItem)); |
||
717 | |||
718 | $missedUrlAliasIdCacheItem |
||
719 | ->expects($this->once()) |
||
720 | ->method('save') |
||
721 | ->with(); |
||
722 | |||
723 | $newUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
724 | $newUrlAliasCacheItem |
||
725 | ->expects($this->once()) |
||
726 | ->method('set') |
||
727 | ->with($urlAlias) |
||
728 | ->will($this->returnValue($newUrlAliasCacheItem)); |
||
729 | |||
730 | $newUrlAliasCacheItem |
||
731 | ->expects($this->once()) |
||
732 | ->method('save') |
||
733 | ->with(); |
||
734 | |||
735 | $this->cacheMock |
||
736 | ->expects($this->at(0)) |
||
737 | ->method('getItem') |
||
738 | ->with('urlAlias', 'url', '/url') |
||
739 | ->will($this->returnValue($missedUrlAliasIdCacheItem)); |
||
740 | $this->cacheMock |
||
741 | ->expects($this->at(1)) |
||
742 | ->method('getItem') |
||
743 | ->with('urlAlias', 55) |
||
744 | ->will($this->returnValue($newUrlAliasCacheItem)); |
||
745 | |||
746 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
747 | $this->persistenceHandlerMock |
||
748 | ->expects($this->once()) |
||
749 | ->method('urlAliasHandler') |
||
750 | ->will($this->returnValue($innerHandler)); |
||
751 | |||
752 | $innerHandler |
||
753 | ->expects($this->once()) |
||
754 | ->method('lookup') |
||
755 | ->with('/url') |
||
756 | ->will($this->returnValue($urlAlias)); |
||
757 | |||
758 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
759 | $handler->lookup('/url'); |
||
760 | } |
||
761 | |||
762 | /** |
||
763 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup |
||
764 | */ |
||
765 | View Code Duplication | public function testLookupHasCache() |
|
766 | { |
||
767 | $this->loggerMock->expects($this->never())->method('logCall'); |
||
768 | |||
769 | $this->persistenceHandlerMock |
||
770 | ->expects($this->never()) |
||
771 | ->method($this->anything()); |
||
772 | |||
773 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
774 | $this->cacheMock |
||
775 | ->expects($this->at(0)) |
||
776 | ->method('getItem') |
||
777 | ->with('urlAlias', 'url', '/url') |
||
778 | ->will($this->returnValue($cacheItemMock)); |
||
779 | |||
780 | $cacheItemMock |
||
781 | ->expects($this->once()) |
||
782 | ->method('get') |
||
783 | ->will($this->returnValue(55)); |
||
784 | |||
785 | $cacheItemMock |
||
786 | ->expects($this->once()) |
||
787 | ->method('isMiss') |
||
788 | ->will($this->returnValue(false)); |
||
789 | |||
790 | $cacheItemMock |
||
791 | ->expects($this->never()) |
||
792 | ->method('set'); |
||
793 | |||
794 | $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
795 | $this->cacheMock |
||
796 | ->expects($this->at(1)) |
||
797 | ->method('getItem') |
||
798 | ->with('urlAlias', 55) |
||
799 | ->will($this->returnValue($cacheItemMock2)); |
||
800 | |||
801 | $cacheItemMock2 |
||
802 | ->expects($this->once()) |
||
803 | ->method('get') |
||
804 | ->will($this->returnValue(new UrlAlias(array('id' => 55)))); |
||
805 | |||
806 | $cacheItemMock2 |
||
807 | ->expects($this->once()) |
||
808 | ->method('isMiss') |
||
809 | ->will($this->returnValue(false)); |
||
810 | |||
811 | $cacheItemMock2 |
||
812 | ->expects($this->never()) |
||
813 | ->method('set'); |
||
814 | |||
815 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
816 | $handler->lookup('/url'); |
||
817 | } |
||
818 | |||
819 | /** |
||
820 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias |
||
821 | */ |
||
822 | View Code Duplication | public function testLoadUrlAliasIsMiss() |
|
823 | { |
||
824 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
825 | |||
826 | $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
827 | $this->cacheMock |
||
828 | ->expects($this->once()) |
||
829 | ->method('getItem') |
||
830 | ->with('urlAlias', 55) |
||
831 | ->will($this->returnValue($cacheItemMock)); |
||
832 | |||
833 | $cacheItemMock |
||
834 | ->expects($this->once()) |
||
835 | ->method('get') |
||
836 | ->will($this->returnValue(null)); |
||
837 | |||
838 | $cacheItemMock |
||
839 | ->expects($this->once()) |
||
840 | ->method('isMiss') |
||
841 | ->will($this->returnValue(true)); |
||
842 | |||
843 | $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler'); |
||
844 | $this->persistenceHandlerMock |
||
845 | ->expects($this->once()) |
||
846 | ->method('urlAliasHandler') |
||
847 | ->will($this->returnValue($innerHandler)); |
||
848 | |||
849 | $innerHandler |
||
850 | ->expects($this->once()) |
||
851 | ->method('loadUrlAlias') |
||
852 | ->with(55) |
||
853 | ->will($this->returnValue(new UrlAlias(array('id' => 55)))); |
||
854 | |||
855 | $cacheItemMock |
||
856 | ->expects($this->once()) |
||
857 | ->method('set') |
||
858 | ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias')) |
||
859 | ->will($this->returnValue($cacheItemMock)); |
||
860 | |||
861 | $cacheItemMock |
||
862 | ->expects($this->once()) |
||
863 | ->method('save') |
||
864 | ->with(); |
||
865 | |||
866 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
867 | $handler->loadUrlAlias(55); |
||
868 | } |
||
869 | |||
870 | /** |
||
871 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias |
||
872 | */ |
||
873 | View Code Duplication | public function testLoadUrlAliasHasCache() |
|
905 | |||
906 | /** |
||
907 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationMoved |
||
908 | */ |
||
909 | View Code Duplication | public function testLocationMoved() |
|
933 | |||
934 | /** |
||
935 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted |
||
936 | */ |
||
937 | public function testLocationDeletedWithoutCachedLocation() |
||
938 | { |
||
939 | $locationNotCached = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
940 | $locationNotCached |
||
941 | ->expects($this->once()) |
||
942 | ->method('isMiss') |
||
943 | ->willReturn(true); |
||
944 | $locationNotCached |
||
945 | ->expects($this->never()) |
||
946 | ->method('clear'); |
||
947 | |||
948 | $this->prepareDeleteMocks($locationNotCached); |
||
949 | |||
950 | $this->cacheMock |
||
951 | ->expects($this->once()) |
||
952 | ->method('clear') |
||
953 | ->with('urlAlias'); |
||
954 | |||
955 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
956 | $handler->locationDeleted(44); |
||
957 | } |
||
958 | |||
959 | /** |
||
960 | * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted |
||
961 | */ |
||
962 | public function testLocationDeletedWithCachedLocation() |
||
963 | { |
||
964 | $locationCacheItem = $this->getMock('Stash\Interfaces\ItemInterface'); |
||
965 | $locationCacheItem |
||
966 | ->expects($this->once()) |
||
967 | ->method('isMiss') |
||
968 | ->willReturn(false); |
||
969 | $locationCacheItem |
||
970 | ->expects($this->once()) |
||
971 | ->method('get') |
||
972 | ->willReturn(['44']) |
||
973 | ; |
||
974 | $locationCacheItem |
||
975 | ->expects($this->once()) |
||
976 | ->method('clear') |
||
977 | ->will($this->returnValue(null)); |
||
978 | |||
979 | $this->prepareDeleteMocks($locationCacheItem); |
||
980 | |||
981 | $this->cacheMock |
||
982 | ->expects($this->at(1)) |
||
983 | ->method('clear') |
||
984 | ->with('urlAlias', 44); |
||
985 | $this->cacheMock |
||
986 | ->expects($this->at(2)) |
||
987 | ->method('clear') |
||
988 | ->with('urlAlias', 'url'); |
||
989 | |||
990 | $handler = $this->persistenceCacheHandler->urlAliasHandler(); |
||
991 | $handler->locationDeleted(44); |
||
992 | } |
||
993 | |||
994 | /** |
||
995 | * @param $locationCacheMissed |
||
996 | */ |
||
997 | View Code Duplication | protected function prepareDeleteMocks($locationCacheMissed) |
|
1014 | } |
||
1015 |