Completed
Push — travis_php71 ( 735d1d...231818 )
by André
13:40
created

UrlAliasHandlerTest::testLookupIsMiss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 53
nc 1
nop 0
dl 0
loc 66
rs 9.3191
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File contains Test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache\Tests;
10
11
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
12
13
/**
14
 * Test case for Persistence\Cache\UrlAliasHandler.
15
 */
16
class UrlAliasHandlerTest extends HandlerTest
17
{
18
    /**
19
     * @return array
20
     */
21
    public function providerForUnCachedMethods()
22
    {
23
        return array(
24
            //array( 'publishUrlAliasForLocation', array( 44, 2, 'name', 'eng-GB', true ) ),
25
            //array( 'createCustomUrlAlias', array( 44, '/path', true, 'eng-GB', true ) ),
26
            //array( 'createGlobalUrlAlias', array( '/old', '/path', true, 'eng-GB', true ) ),
27
            array('listGlobalURLAliases', array('eng-GB', 10, 5)),
28
            //array( 'listURLAliasesForLocation', array( 44, true ) ),
29
            //array( 'removeURLAliases', array( array( 1, 2 ) ) ),
30
            //array( 'lookup', array( '/url' ) ),
31
            //array( 'loadUrlAlias', array( 88 ) ),
32
            //array( 'locationMoved', array( 44, 2, 45 ) ),
33
            //array( 'locationCopied', array( 44, 2, 45 ) ),
34
            //array( 'locationDeleted', array( 44 ) ),
35
        );
36
    }
37
38
    /**
39
     * @dataProvider providerForUnCachedMethods
40
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler
41
     */
42
    public function testUnCachedMethods($method, array $arguments)
43
    {
44
        $this->loggerMock->expects($this->once())->method('logCall');
45
        $this->cacheMock
46
            ->expects($this->never())
47
            ->method($this->anything());
48
49
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
50
        $this->persistenceHandlerMock
51
            ->expects($this->once())
52
            ->method('urlAliasHandler')
53
            ->will($this->returnValue($innerHandler));
54
55
        $expects = $innerHandler
56
            ->expects($this->once())
57
            ->method($method);
58
59
        if (isset($arguments[4])) {
60
            $expects->with($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
61
        } elseif (isset($arguments[3])) {
62
            $expects->with($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
63
        } elseif (isset($arguments[2])) {
64
            $expects->with($arguments[0], $arguments[1], $arguments[2]);
65
        } elseif (isset($arguments[1])) {
66
            $expects->with($arguments[0], $arguments[1]);
67
        } elseif (isset($arguments[0])) {
68
            $expects->with($arguments[0]);
69
        }
70
71
        $expects->will($this->returnValue(null));
72
73
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
74
        call_user_func_array(array($handler, $method), $arguments);
75
    }
76
77
    /**
78
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::publishUrlAliasForLocation
79
     */
80
    public function testPublishUrlAliasForLocationWithoutCachedLocation()
81
    {
82
        $this->loggerMock->expects($this->once())->method('logCall');
83
84
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
85
        $cacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
86
87
        $this->persistenceHandlerMock
88
            ->expects($this->once())
89
            ->method('urlAliasHandler')
90
            ->will($this->returnValue($innerHandler));
91
92
        $innerHandler
93
            ->expects($this->once())
94
            ->method('publishUrlAliasForLocation')
95
            ->with(44, 2, 'name', 'eng-GB', true)
96
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
97
98
        $cacheItem
99
            ->expects($this->once())
100
            ->method('isMiss')
101
            ->willReturn(true);
102
        $cacheItem
103
            ->expects($this->never())
104
            ->method('clear');
105
106
        $this->cacheMock
107
            ->expects($this->once())
108
            ->method('clear')
109
            ->with('urlAlias')
110
            ->will($this->returnValue(null));
111
        $this->cacheMock
112
            ->expects($this->once())
113
            ->method('getItem')
114
            ->with('urlAlias', 'location', 44)
115
            ->willReturn($cacheItem);
116
117
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
118
        $handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true);
119
    }
120
121
    /**
122
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::publishUrlAliasForLocation
123
     */
124
    public function testPublishUrlAliasForLocationWithCachedLocation()
125
    {
126
        $this->loggerMock->expects($this->once())->method('logCall');
127
128
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
129
        $cacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
130
131
        $this->persistenceHandlerMock
132
            ->expects($this->once())
133
            ->method('urlAliasHandler')
134
            ->will($this->returnValue($innerHandler));
135
136
        $innerHandler
137
            ->expects($this->once())
138
            ->method('publishUrlAliasForLocation')
139
            ->with(44, 2, 'name', 'eng-GB', true)
140
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
141
142
        $cacheItem
143
            ->expects($this->once())
144
            ->method('isMiss')
145
            ->willReturn(false);
146
        $cacheItem
147
            ->expects($this->once())
148
            ->method('clear');
149
        $cacheItem
150
            ->expects($this->once())
151
            ->method('get')
152
            ->willReturn([44]);
153
154
        $this->cacheMock
155
            ->expects($this->once())
156
            ->method('getItem')
157
            ->with('urlAlias', 'location', 44)
158
            ->willReturn($cacheItem);
159
        $this->cacheMock
160
            ->expects($this->at(1))
161
            ->method('clear')
162
            ->with('urlAlias', 44)
163
            ->will($this->returnValue(null));
164
        $this->cacheMock
165
            ->expects($this->at(2))
166
            ->method('clear')
167
            ->with('urlAlias', 'url')
168
            ->will($this->returnValue(null));
169
170
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
171
        $handler->publishUrlAliasForLocation(44, 2, 'name', 'eng-GB', true);
172
    }
173
174
    /**
175
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createCustomUrlAlias
176
     */
177
    public function testCreateCustomUrlAliasHasCache()
178
    {
179
        $this->loggerMock->expects($this->once())->method('logCall');
180
181
        $urlAlias = new UrlAlias(array('id' => 55, 'destination' => 44));
182
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
183
        $this->persistenceHandlerMock
184
            ->expects($this->once())
185
            ->method('urlAliasHandler')
186
            ->will($this->returnValue($innerHandler));
187
188
        $innerHandler
189
            ->expects($this->once())
190
            ->method('createCustomUrlAlias')
191
            ->with(44, '/path', true, 'eng-GB', true)
192
            ->will($this->returnValue($urlAlias));
193
194
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
195
        $this->cacheMock
196
            ->expects($this->at(0))
197
            ->method('getItem')
198
            ->with('urlAlias', 55)
199
            ->will($this->returnValue($cacheItemMock));
200
201
        $cacheItemMock
202
            ->expects($this->never())
203
            ->method('get');
204
        $cacheItemMock
205
            ->expects($this->once())
206
            ->method('set')
207
            ->with($urlAlias)
208
            ->will($this->returnValue($cacheItemMock));
209
210
        $cacheItemMock
211
            ->expects($this->once())
212
            ->method('save')
213
            ->with();
214
215
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
216
        $this->cacheMock
217
            ->expects($this->at(1))
218
            ->method('getItem')
219
            ->with('urlAlias', 'location', 44, 'custom')
220
            ->will($this->returnValue($cacheItemMock2));
221
222
        $cacheItemMock2
223
            ->expects($this->once())
224
            ->method('get')
225
            ->will($this->returnValue(array(42)));
226
227
        $cacheItemMock2
228
            ->expects($this->once())
229
            ->method('isMiss')
230
            ->will($this->returnValue(false));
231
232
        $cacheItemMock2
233
            ->expects($this->once())
234
            ->method('set')
235
            ->with(array(42, 55))
236
            ->will($this->returnValue($cacheItemMock2));
237
238
        $cacheItemMock2
239
            ->expects($this->once())
240
            ->method('save')
241
            ->with();
242
243
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
244
        $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true);
245
    }
246
247
    /**
248
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createCustomUrlAlias
249
     */
250
    public function testCreateCustomUrlAliasIsMiss()
251
    {
252
        $this->loggerMock->expects($this->once())->method('logCall');
253
254
        $urlAlias = new UrlAlias(array('id' => 55, 'destination' => 44));
255
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
256
        $this->persistenceHandlerMock
257
            ->expects($this->once())
258
            ->method('urlAliasHandler')
259
            ->will($this->returnValue($innerHandler));
260
261
        $innerHandler
262
            ->expects($this->once())
263
            ->method('createCustomUrlAlias')
264
            ->with(44, '/path', true, 'eng-GB', true)
265
            ->will($this->returnValue($urlAlias));
266
267
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
268
        $this->cacheMock
269
            ->expects($this->at(0))
270
            ->method('getItem')
271
            ->with('urlAlias', 55)
272
            ->will($this->returnValue($cacheItemMock));
273
274
        $cacheItemMock
275
            ->expects($this->never())
276
            ->method('get');
277
        $cacheItemMock
278
            ->expects($this->once())
279
            ->method('set')
280
            ->with($urlAlias)
281
            ->will($this->returnValue($cacheItemMock));
282
283
        $cacheItemMock
284
            ->expects($this->once())
285
            ->method('save')
286
            ->with();
287
288
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
289
        $this->cacheMock
290
            ->expects($this->at(1))
291
            ->method('getItem')
292
            ->with('urlAlias', 'location', 44, 'custom')
293
            ->will($this->returnValue($cacheItemMock2));
294
295
        $cacheItemMock2
296
            ->expects($this->once())
297
            ->method('get')
298
            ->will($this->returnValue(null));
299
300
        $cacheItemMock2
301
            ->expects($this->once())
302
            ->method('isMiss')
303
            ->will($this->returnValue(true));
304
305
        $cacheItemMock2
306
            ->expects($this->once())
307
            ->method('set')
308
            ->with(array(55))
309
            ->will($this->returnValue($cacheItemMock2));
310
311
        $cacheItemMock2
312
            ->expects($this->once())
313
            ->method('save')
314
            ->with();
315
316
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
317
        $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true);
318
    }
319
320
    /**
321
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createGlobalUrlAlias
322
     */
323
    public function testCreateGlobalUrlAlias()
324
    {
325
        $this->loggerMock->expects($this->once())->method('logCall');
326
327
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
328
        $this->persistenceHandlerMock
329
            ->expects($this->once())
330
            ->method('urlAliasHandler')
331
            ->will($this->returnValue($innerHandler));
332
333
        $innerHandler
334
            ->expects($this->once())
335
            ->method('createGlobalUrlAlias')
336
            ->with('/old', '/path', true, 'eng-GB', true)
337
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
338
339
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
340
        $this->cacheMock
341
            ->expects($this->once())
342
            ->method('getItem')
343
            ->with('urlAlias', 55)
344
            ->will($this->returnValue($cacheItemMock));
345
346
        $cacheItemMock
347
            ->expects($this->once())
348
            ->method('set')
349
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias'))
350
            ->will($this->returnValue($cacheItemMock));
351
352
        $cacheItemMock
353
            ->expects($this->once())
354
            ->method('save')
355
            ->with();
356
357
        $cacheItemMock
358
            ->expects($this->never())
359
            ->method('get');
360
361
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
362
        $handler->createGlobalUrlAlias('/old', '/path', true, 'eng-GB', true);
363
    }
364
365
    /**
366
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
367
     */
368 View Code Duplication
    public function testListURLAliasesForLocationIsMiss()
369
    {
370
        $this->loggerMock->expects($this->once())->method('logCall');
371
372
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
373
        $this->cacheMock
374
            ->expects($this->once())
375
            ->method('getItem')
376
            ->with('urlAlias', 'location', 44)
377
            ->will($this->returnValue($cacheItemMock));
378
379
        $cacheItemMock
380
            ->expects($this->once())
381
            ->method('get')
382
            ->will($this->returnValue(null));
383
384
        $cacheItemMock
385
            ->expects($this->once())
386
            ->method('isMiss')
387
            ->will($this->returnValue(true));
388
389
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
390
        $this->persistenceHandlerMock
391
            ->expects($this->once())
392
            ->method('urlAliasHandler')
393
            ->will($this->returnValue($innerHandler));
394
395
        $innerHandler
396
            ->expects($this->once())
397
            ->method('listURLAliasesForLocation')
398
            ->with(44, false)
399
            ->will(
400
                $this->returnValue(
401
                    array(
402
                        new UrlAlias(array('id' => 55)),
403
                        new UrlAlias(array('id' => 58)),
404
                        new UrlAlias(array('id' => 91)),
405
                    )
406
                )
407
            );
408
409
        $cacheItemMock
410
            ->expects($this->once())
411
            ->method('set')
412
            ->with(array(55, 58, 91))
413
            ->will($this->returnValue($cacheItemMock));
414
415
        $cacheItemMock
416
            ->expects($this->once())
417
            ->method('save')
418
            ->with();
419
420
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
421
        $handler->listURLAliasesForLocation(44, false);
422
    }
423
424
    /**
425
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
426
     */
427 View Code Duplication
    public function testListURLAliasesForLocationCustomIsMiss()
428
    {
429
        $this->loggerMock->expects($this->once())->method('logCall');
430
431
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
432
        $this->cacheMock
433
            ->expects($this->once())
434
            ->method('getItem')
435
            ->with('urlAlias', 'location', '44', 'custom')
436
            ->will($this->returnValue($cacheItemMock));
437
438
        $cacheItemMock
439
            ->expects($this->once())
440
            ->method('get')
441
            ->will($this->returnValue(null));
442
443
        $cacheItemMock
444
            ->expects($this->once())
445
            ->method('isMiss')
446
            ->will($this->returnValue(true));
447
448
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
449
        $this->persistenceHandlerMock
450
            ->expects($this->once())
451
            ->method('urlAliasHandler')
452
            ->will($this->returnValue($innerHandler));
453
454
        $innerHandler
455
            ->expects($this->once())
456
            ->method('listURLAliasesForLocation')
457
            ->with(44, true)
458
            ->will(
459
                $this->returnValue(
460
                    array(
461
                        new UrlAlias(array('id' => 55)),
462
                        new UrlAlias(array('id' => 58)),
463
                        new UrlAlias(array('id' => 91)),
464
                    )
465
                )
466
            );
467
468
        $cacheItemMock
469
            ->expects($this->once())
470
            ->method('set')
471
            ->with(array(55, 58, 91))
472
            ->will($this->returnValue($cacheItemMock));
473
474
        $cacheItemMock
475
            ->expects($this->once())
476
            ->method('save')
477
            ->with();
478
479
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
480
        $handler->listURLAliasesForLocation(44, true);
481
    }
482
483
    /**
484
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
485
     */
486 View Code Duplication
    public function testListURLAliasesForLocationHasCache()
487
    {
488
        $this->loggerMock->expects($this->never())->method('logCall');
489
490
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
491
        $this->cacheMock
492
            ->expects($this->at(0))
493
            ->method('getItem')
494
            ->with('urlAlias', 'location', 44)
495
            ->will($this->returnValue($cacheItemMock));
496
497
        $cacheItemMock
498
            ->expects($this->once())
499
            ->method('get')
500
            ->will($this->returnValue(array(55, 58, 91)));
501
502
        $cacheItemMock
503
            ->expects($this->once())
504
            ->method('isMiss')
505
            ->will($this->returnValue(false));
506
507
        $this->persistenceHandlerMock
508
            ->expects($this->never())
509
            ->method($this->anything());
510
511
        $cacheItemMock
512
            ->expects($this->never())
513
            ->method('set');
514
515
        // inline calls to loadUrlAlias() using the cache
516
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
517
        $this->cacheMock
518
            ->expects($this->at(1))
519
            ->method('getItem')
520
            ->with('urlAlias', 55)
521
            ->will($this->returnValue($cacheItemMock2));
522
523
        $cacheItemMock2
524
            ->expects($this->at(0))
525
            ->method('get')
526
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
527
528
        $this->cacheMock
529
            ->expects($this->at(2))
530
            ->method('getItem')
531
            ->with('urlAlias', 58)
532
            ->will($this->returnValue($cacheItemMock2));
533
534
        $cacheItemMock2
535
            ->expects($this->at(1))
536
            ->method('get')
537
            ->will($this->returnValue(new UrlAlias(array('id' => 58))));
538
539
        $this->cacheMock
540
            ->expects($this->at(3))
541
            ->method('getItem')
542
            ->with('urlAlias', 91)
543
            ->will($this->returnValue($cacheItemMock2));
544
545
        $cacheItemMock2
546
            ->expects($this->at(2))
547
            ->method('get')
548
            ->will($this->returnValue(new UrlAlias(array('id' => 91))));
549
550
        $cacheItemMock2
551
            ->expects($this->exactly(3))
552
            ->method('isMiss')
553
            ->will($this->returnValue(false));
554
555
        $cacheItemMock2
556
            ->expects($this->never())
557
            ->method('set');
558
559
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
560
        $handler->listURLAliasesForLocation(44, false);
561
    }
562
563
    /**
564
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
565
     */
566 View Code Duplication
    public function testListURLAliasesForLocationCustomHasCache()
567
    {
568
        $this->loggerMock->expects($this->never())->method('logCall');
569
570
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
571
        $this->cacheMock
572
            ->expects($this->at(0))
573
            ->method('getItem')
574
            ->with('urlAlias', 'location', '44', 'custom')
575
            ->will($this->returnValue($cacheItemMock));
576
577
        $cacheItemMock
578
            ->expects($this->once())
579
            ->method('get')
580
            ->will($this->returnValue(array(55, 58, 91)));
581
582
        $cacheItemMock
583
            ->expects($this->once())
584
            ->method('isMiss')
585
            ->will($this->returnValue(false));
586
587
        $this->persistenceHandlerMock
588
            ->expects($this->never())
589
            ->method($this->anything());
590
591
        $cacheItemMock
592
            ->expects($this->never())
593
            ->method('set');
594
595
                // inline calls to loadUrlAlias() using the cache
596
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
597
        $this->cacheMock
598
            ->expects($this->at(1))
599
            ->method('getItem')
600
            ->with('urlAlias', 55)
601
            ->will($this->returnValue($cacheItemMock2));
602
603
        $cacheItemMock2
604
            ->expects($this->at(0))
605
            ->method('get')
606
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
607
608
        $this->cacheMock
609
            ->expects($this->at(2))
610
            ->method('getItem')
611
            ->with('urlAlias', 58)
612
            ->will($this->returnValue($cacheItemMock2));
613
614
        $cacheItemMock2
615
            ->expects($this->at(1))
616
            ->method('get')
617
            ->will($this->returnValue(new UrlAlias(array('id' => 58))));
618
619
        $this->cacheMock
620
            ->expects($this->at(3))
621
            ->method('getItem')
622
            ->with('urlAlias', 91)
623
            ->will($this->returnValue($cacheItemMock2));
624
625
        $cacheItemMock2
626
            ->expects($this->at(2))
627
            ->method('get')
628
            ->will($this->returnValue(new UrlAlias(array('id' => 91))));
629
630
        $cacheItemMock2
631
            ->expects($this->exactly(3))
632
            ->method('isMiss')
633
            ->will($this->returnValue(false));
634
635
        $cacheItemMock2
636
            ->expects($this->never())
637
            ->method('set');
638
639
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
640
        $handler->listURLAliasesForLocation(44, true);
641
    }
642
643
    /**
644
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::removeURLAliases
645
     */
646
    public function testRemoveURLAliases()
647
    {
648
        $this->loggerMock->expects($this->once())->method('logCall');
649
650
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
651
        $this->persistenceHandlerMock
652
            ->expects($this->once())
653
            ->method('urlAliasHandler')
654
            ->will($this->returnValue($innerHandler));
655
656
        $innerHandler
657
            ->expects($this->once())
658
            ->method('removeURLAliases');
659
660
        $this->cacheMock
661
            ->expects($this->at(0))
662
            ->method('clear')
663
            ->with('urlAlias', 'url');
664
665
        $this->cacheMock
666
            ->expects($this->at(1))
667
            ->method('clear')
668
            ->with('urlAlias', 21);
669
670
        $this->cacheMock
671
            ->expects($this->at(2))
672
            ->method('clear')
673
            ->with('urlAlias', 32);
674
675
        $this->cacheMock
676
            ->expects($this->at(3))
677
            ->method('clear')
678
            ->with('urlAlias', 'location', 44);
679
680
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
681
        $handler->removeURLAliases(
682
            array(
683
                new UrlAlias(array('id' => 21)),
684
                new UrlAlias(array('id' => 32, 'type' => UrlAlias::LOCATION, 'destination' => 44)),
685
            )
686
        );
687
    }
688
689
    /**
690
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
691
     */
692
    public function testLookupIsMissActive()
693
    {
694
        $urlAlias = new UrlAlias(
695
            [
696
                'id' => 55,
697
                'isHistory' => false,
698
            ]
699
        );
700
701
        $this->loggerMock->expects($this->once())->method('logCall');
702
703
        $missedUrlAliasIdCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
704
        $missedUrlAliasIdCacheItem
705
            ->expects($this->once())
706
            ->method('get')
707
            ->will($this->returnValue(null));
708
709
        $missedUrlAliasIdCacheItem
710
            ->expects($this->once())
711
            ->method('isMiss')
712
            ->will($this->returnValue(true));
713
714
        $missedUrlAliasIdCacheItem
715
            ->expects($this->once())
716
            ->method('set')
717
            ->with(55)
718
            ->will($this->returnValue($missedUrlAliasIdCacheItem));
719
720
        $missedUrlAliasIdCacheItem
721
            ->expects($this->once())
722
            ->method('save')
723
            ->with();
724
725
        $newUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
726
        $newUrlAliasCacheItem
727
            ->expects($this->once())
728
            ->method('set')
729
            ->with($urlAlias)
730
            ->will($this->returnValue($newUrlAliasCacheItem));
731
732
        $newUrlAliasCacheItem
733
            ->expects($this->once())
734
            ->method('save')
735
            ->with();
736
737
        $historyUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
738
        $historyUrlAliasCacheItem
739
            ->expects($this->once())
740
            ->method('get')
741
            ->will($this->returnValue(null));
742
743
        $historyUrlAliasCacheItem
744
            ->expects($this->once())
745
            ->method('isMiss')
746
            ->will($this->returnValue(true));
747
748
        $this->cacheMock
749
            ->expects($this->at(0))
750
            ->method('getItem')
751
            ->with('urlAlias', 'url', '/url')
752
            ->will($this->returnValue($missedUrlAliasIdCacheItem));
753
        $this->cacheMock
754
            ->expects($this->at(1))
755
            ->method('getItem')
756
            ->with('urlAlias', 'url', 'history', '/url')
757
            ->will($this->returnValue($historyUrlAliasCacheItem));
758
        $this->cacheMock
759
            ->expects($this->at(2))
760
            ->method('getItem')
761
            ->with('urlAlias', 55)
762
            ->will($this->returnValue($newUrlAliasCacheItem));
763
764
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
765
        $this->persistenceHandlerMock
766
            ->expects($this->once())
767
            ->method('urlAliasHandler')
768
            ->will($this->returnValue($innerHandler));
769
770
        $innerHandler
771
            ->expects($this->once())
772
            ->method('lookup')
773
            ->with('/url')
774
            ->will($this->returnValue($urlAlias));
775
776
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
777
        $handler->lookup('/url');
778
    }
779
780
    /**
781
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
782
     */
783
    public function testLookupIsMissHistory()
784
    {
785
        $urlAlias = new UrlAlias(
786
            [
787
                'id' => 55,
788
                'isHistory' => true,
789
            ]
790
        );
791
792
        $this->loggerMock->expects($this->once())->method('logCall');
793
794
        $missedUrlAliasIdCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
795
        $missedUrlAliasIdCacheItem
796
            ->expects($this->once())
797
            ->method('get')
798
            ->will($this->returnValue(null));
799
800
        $missedUrlAliasIdCacheItem
801
            ->expects($this->once())
802
            ->method('isMiss')
803
            ->will($this->returnValue(true));
804
805
        $historyUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
806
        $historyUrlAliasCacheItem
807
            ->expects($this->once())
808
            ->method('get')
809
            ->will($this->returnValue(null));
810
811
        $historyUrlAliasCacheItem
812
            ->expects($this->once())
813
            ->method('isMiss')
814
            ->will($this->returnValue(true));
815
816
        $historyUrlAliasCacheItem
817
            ->expects($this->once())
818
            ->method('set')
819
            ->with($urlAlias)
820
            ->will($this->returnValue($historyUrlAliasCacheItem));
821
        $historyUrlAliasCacheItem
822
            ->expects($this->once())
823
            ->method('save')
824
            ->with();
825
826
        $this->cacheMock
827
            ->expects($this->at(0))
828
            ->method('getItem')
829
            ->with('urlAlias', 'url', '/url')
830
            ->will($this->returnValue($missedUrlAliasIdCacheItem));
831
        $this->cacheMock
832
            ->expects($this->at(1))
833
            ->method('getItem')
834
            ->with('urlAlias', 'url', 'history', '/url')
835
            ->will($this->returnValue($historyUrlAliasCacheItem));
836
837
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
838
        $this->persistenceHandlerMock
839
            ->expects($this->once())
840
            ->method('urlAliasHandler')
841
            ->will($this->returnValue($innerHandler));
842
843
        $innerHandler
844
            ->expects($this->once())
845
            ->method('lookup')
846
            ->with('/url')
847
            ->will($this->returnValue($urlAlias));
848
849
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
850
        $handler->lookup('/url');
851
    }
852
853
    /**
854
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
855
     */
856 View Code Duplication
    public function testLookupHasCache()
857
    {
858
        $this->loggerMock->expects($this->never())->method('logCall');
859
860
        $this->persistenceHandlerMock
861
            ->expects($this->never())
862
            ->method($this->anything());
863
864
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
865
        $this->cacheMock
866
            ->expects($this->at(0))
867
            ->method('getItem')
868
            ->with('urlAlias', 'url', '/url')
869
            ->will($this->returnValue($cacheItemMock));
870
871
        $cacheItemMock
872
            ->expects($this->once())
873
            ->method('get')
874
            ->will($this->returnValue(55));
875
876
        $cacheItemMock
877
            ->expects($this->once())
878
            ->method('isMiss')
879
            ->will($this->returnValue(false));
880
881
        $cacheItemMock
882
            ->expects($this->never())
883
            ->method('set');
884
885
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
886
        $this->cacheMock
887
            ->expects($this->at(1))
888
            ->method('getItem')
889
            ->with('urlAlias', 55)
890
            ->will($this->returnValue($cacheItemMock2));
891
892
        $cacheItemMock2
893
            ->expects($this->once())
894
            ->method('get')
895
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
896
897
        $cacheItemMock2
898
            ->expects($this->once())
899
            ->method('isMiss')
900
            ->will($this->returnValue(false));
901
902
        $cacheItemMock2
903
            ->expects($this->never())
904
            ->method('set');
905
906
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
907
        $handler->lookup('/url');
908
    }
909
910
    /**
911
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
912
     */
913
    public function testLookupHasHistoryCache()
914
    {
915
        $urlAlias = new UrlAlias(array('id' => 55));
916
917
        $this->loggerMock
918
            ->expects($this->never())
919
            ->method('logCall');
920
921
        $missedUrlAliasIdCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
922
        $missedUrlAliasIdCacheItem
923
            ->expects($this->once())
924
            ->method('get')
925
            ->will($this->returnValue(null));
926
927
        $missedUrlAliasIdCacheItem
928
            ->expects($this->once())
929
            ->method('isMiss')
930
            ->will($this->returnValue(true));
931
932
        $historyUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
933
        $historyUrlAliasCacheItem
934
            ->expects($this->once())
935
            ->method('get')
936
            ->will($this->returnValue($urlAlias));
937
938
        $historyUrlAliasCacheItem
939
            ->expects($this->once())
940
            ->method('isMiss')
941
            ->will($this->returnValue(false));
942
943
        $this->cacheMock
944
            ->expects($this->at(0))
945
            ->method('getItem')
946
            ->with('urlAlias', 'url', '/url')
947
            ->will($this->returnValue($missedUrlAliasIdCacheItem));
948
        $this->cacheMock
949
            ->expects($this->at(1))
950
            ->method('getItem')
951
            ->with('urlAlias', 'url', 'history', '/url')
952
            ->will($this->returnValue($historyUrlAliasCacheItem));
953
954
        $this->persistenceHandlerMock
955
            ->expects($this->never())
956
            ->method('urlAliasHandler');
957
958
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
959
        $handler->lookup('/url');
960
    }
961
962
    /**
963
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias
964
     */
965 View Code Duplication
    public function testLoadUrlAliasIsMiss()
966
    {
967
        $this->loggerMock->expects($this->once())->method('logCall');
968
969
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
970
        $this->cacheMock
971
            ->expects($this->once())
972
            ->method('getItem')
973
            ->with('urlAlias', 55)
974
            ->will($this->returnValue($cacheItemMock));
975
976
        $cacheItemMock
977
            ->expects($this->once())
978
            ->method('get')
979
            ->will($this->returnValue(null));
980
981
        $cacheItemMock
982
            ->expects($this->once())
983
            ->method('isMiss')
984
            ->will($this->returnValue(true));
985
986
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
987
        $this->persistenceHandlerMock
988
            ->expects($this->once())
989
            ->method('urlAliasHandler')
990
            ->will($this->returnValue($innerHandler));
991
992
        $innerHandler
993
            ->expects($this->once())
994
            ->method('loadUrlAlias')
995
            ->with(55)
996
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
997
998
        $cacheItemMock
999
            ->expects($this->once())
1000
            ->method('set')
1001
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias'))
1002
            ->will($this->returnValue($cacheItemMock));
1003
1004
        $cacheItemMock
1005
            ->expects($this->once())
1006
            ->method('save')
1007
            ->with();
1008
1009
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
1010
        $handler->loadUrlAlias(55);
1011
    }
1012
1013
    /**
1014
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias
1015
     */
1016 View Code Duplication
    public function testLoadUrlAliasHasCache()
1017
    {
1018
        $this->loggerMock->expects($this->never())->method('logCall');
1019
1020
        $this->persistenceHandlerMock
1021
            ->expects($this->never())
1022
            ->method($this->anything());
1023
1024
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
1025
        $this->cacheMock
1026
            ->expects($this->once())
1027
            ->method('getItem')
1028
            ->with('urlAlias', 55)
1029
            ->will($this->returnValue($cacheItemMock));
1030
1031
        $cacheItemMock
1032
            ->expects($this->once())
1033
            ->method('get')
1034
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
1035
1036
        $cacheItemMock
1037
            ->expects($this->once())
1038
            ->method('isMiss')
1039
            ->will($this->returnValue(false));
1040
1041
        $cacheItemMock
1042
            ->expects($this->never())
1043
            ->method('set');
1044
1045
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
1046
        $handler->loadUrlAlias(55);
1047
    }
1048
1049
    /**
1050
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationMoved
1051
     */
1052 View Code Duplication
    public function testLocationMoved()
1053
    {
1054
        $this->loggerMock->expects($this->once())->method('logCall');
1055
1056
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
1057
        $this->persistenceHandlerMock
1058
            ->expects($this->once())
1059
            ->method('urlAliasHandler')
1060
            ->will($this->returnValue($innerHandler));
1061
1062
        $innerHandler
1063
            ->expects($this->once())
1064
            ->method('locationMoved')
1065
            ->with(44, 2, 45);
1066
1067
        $this->cacheMock
1068
            ->expects($this->once())
1069
            ->method('clear')
1070
            ->with('urlAlias')
1071
            ->will($this->returnValue(null));
1072
1073
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
1074
        $handler->locationMoved(44, 2, 45);
1075
    }
1076
1077
    /**
1078
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted
1079
     */
1080
    public function testLocationDeletedWithoutCachedLocation()
1081
    {
1082
        $locationNotCached = $this->getMock('Stash\Interfaces\ItemInterface');
1083
        $locationNotCached
1084
            ->expects($this->once())
1085
            ->method('isMiss')
1086
            ->willReturn(true);
1087
        $locationNotCached
1088
            ->expects($this->never())
1089
            ->method('clear');
1090
1091
        $this->prepareDeleteMocks($locationNotCached);
1092
1093
        $this->cacheMock
1094
            ->expects($this->once())
1095
            ->method('clear')
1096
            ->with('urlAlias');
1097
1098
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
1099
        $handler->locationDeleted(44);
1100
    }
1101
1102
    /**
1103
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted
1104
     */
1105
    public function testLocationDeletedWithCachedLocation()
1106
    {
1107
        $locationCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
1108
        $locationCacheItem
1109
            ->expects($this->once())
1110
            ->method('isMiss')
1111
            ->willReturn(false);
1112
        $locationCacheItem
1113
            ->expects($this->once())
1114
            ->method('get')
1115
            ->willReturn(['44'])
1116
        ;
1117
        $locationCacheItem
1118
            ->expects($this->once())
1119
            ->method('clear')
1120
            ->will($this->returnValue(null));
1121
1122
        $this->prepareDeleteMocks($locationCacheItem);
1123
1124
        $this->cacheMock
1125
            ->expects($this->at(1))
1126
            ->method('clear')
1127
            ->with('urlAlias', 44);
1128
        $this->cacheMock
1129
            ->expects($this->at(2))
1130
            ->method('clear')
1131
            ->with('urlAlias', 'url');
1132
1133
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
1134
        $handler->locationDeleted(44);
1135
    }
1136
1137
    /**
1138
     * @param $locationCacheMissed
1139
     */
1140 View Code Duplication
    protected function prepareDeleteMocks($locationCacheMissed)
1141
    {
1142
        $this->loggerMock->expects($this->once())->method('logCall');
1143
1144
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
1145
        $this->persistenceHandlerMock
1146
            ->expects($this->once())
1147
            ->method('urlAliasHandler')
1148
            ->will($this->returnValue($innerHandler));
1149
1150
        $innerHandler->expects($this->once())->method('locationDeleted')->with(44);
1151
1152
        $this->cacheMock
1153
            ->expects($this->once())
1154
            ->method('getItem')
1155
            ->willReturn($locationCacheMissed);
1156
    }
1157
}
1158