Completed
Push — non_purge_indexer ( 31b501...720488 )
by André
12:42
created

testCreateCustomUrlAliasHasCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

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