Completed
Push — signal_search_issues ( 5556b2...f328ba )
by André
63:06 queued 07:22
created

UrlAliasHandlerTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 937
Duplicated Lines 44.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 417
loc 937
rs 9.7391
wmc 25
lcom 1
cbo 4

20 Methods

Rating   Name   Duplication   Size   Complexity  
B testUnCachedMethods() 0 34 6
A providerForUnCachedMethods() 0 16 1
B testPublishUrlAliasForLocationWithoutCachedLocation() 0 40 1
A testPublishUrlAliasForLocationWithCachedLocation() 0 49 1
A testCreateCustomUrlAliasHasCache() 0 57 1
A testCreateCustomUrlAliasIsMiss() 0 57 1
B testCreateGlobalUrlAlias() 0 35 1
A testListURLAliasesForLocationIsMiss() 49 49 1
A testListURLAliasesForLocationCustomIsMiss() 49 49 1
B testListURLAliasesForLocationHasCache() 76 76 1
B testListURLAliasesForLocationCustomHasCache() 76 76 1
B testRemoveURLAliases() 0 42 1
A testLookupIsMiss() 0 54 1
A testLookupHasCache() 53 53 1
B testLoadUrlAliasIsMiss() 41 41 1
B testLoadUrlAliasHasCache() 32 32 1
B testLocationMoved() 24 24 1
A testLocationDeletedWithoutCachedLocation() 0 21 1
B testLocationDeletedWithCachedLocation() 0 31 1
A prepareDeleteMocks() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

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
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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Cache\Tests;
12
13
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
14
15
/**
16
 * Test case for Persistence\Cache\UrlAliasHandler.
17
 */
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)
45
    {
46
        $this->loggerMock->expects($this->once())->method('logCall');
47
        $this->cacheMock
48
            ->expects($this->never())
49
            ->method($this->anything());
50
51
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
52
        $this->persistenceHandlerMock
53
            ->expects($this->once())
54
            ->method('urlAliasHandler')
55
            ->will($this->returnValue($innerHandler));
56
57
        $expects = $innerHandler
58
            ->expects($this->once())
59
            ->method($method);
60
61
        if (isset($arguments[4])) {
62
            $expects->with($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
63
        } elseif (isset($arguments[3])) {
64
            $expects->with($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
65
        } elseif (isset($arguments[2])) {
66
            $expects->with($arguments[0], $arguments[1], $arguments[2]);
67
        } elseif (isset($arguments[1])) {
68
            $expects->with($arguments[0], $arguments[1]);
69
        } elseif (isset($arguments[0])) {
70
            $expects->with($arguments[0]);
71
        }
72
73
        $expects->will($this->returnValue(null));
74
75
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
76
        call_user_func_array(array($handler, $method), $arguments);
77
    }
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
211
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
212
        $this->cacheMock
213
            ->expects($this->at(1))
214
            ->method('getItem')
215
            ->with('urlAlias', 'location', 44, 'custom')
216
            ->will($this->returnValue($cacheItemMock2));
217
218
        $cacheItemMock2
219
            ->expects($this->once())
220
            ->method('get')
221
            ->will($this->returnValue(array(42)));
222
223
        $cacheItemMock2
224
            ->expects($this->once())
225
            ->method('isMiss')
226
            ->will($this->returnValue(false));
227
228
        $cacheItemMock2
229
            ->expects($this->once())
230
            ->method('set')
231
            ->with(array(42, 55));
232
233
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
234
        $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true);
235
    }
236
237
    /**
238
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createCustomUrlAlias
239
     */
240
    public function testCreateCustomUrlAliasIsMiss()
241
    {
242
        $this->loggerMock->expects($this->once())->method('logCall');
243
244
        $urlAlias = new UrlAlias(array('id' => 55, 'destination' => 44));
245
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
246
        $this->persistenceHandlerMock
247
            ->expects($this->once())
248
            ->method('urlAliasHandler')
249
            ->will($this->returnValue($innerHandler));
250
251
        $innerHandler
252
            ->expects($this->once())
253
            ->method('createCustomUrlAlias')
254
            ->with(44, '/path', true, 'eng-GB', true)
255
            ->will($this->returnValue($urlAlias));
256
257
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
258
        $this->cacheMock
259
            ->expects($this->at(0))
260
            ->method('getItem')
261
            ->with('urlAlias', 55)
262
            ->will($this->returnValue($cacheItemMock));
263
264
        $cacheItemMock
265
            ->expects($this->never())
266
            ->method('get');
267
        $cacheItemMock
268
            ->expects($this->once())
269
            ->method('set')
270
            ->with($urlAlias);
271
272
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
273
        $this->cacheMock
274
            ->expects($this->at(1))
275
            ->method('getItem')
276
            ->with('urlAlias', 'location', 44, 'custom')
277
            ->will($this->returnValue($cacheItemMock2));
278
279
        $cacheItemMock2
280
            ->expects($this->once())
281
            ->method('get')
282
            ->will($this->returnValue(null));
283
284
        $cacheItemMock2
285
            ->expects($this->once())
286
            ->method('isMiss')
287
            ->will($this->returnValue(true));
288
289
        $cacheItemMock2
290
            ->expects($this->once())
291
            ->method('set')
292
            ->with(array(55));
293
294
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
295
        $handler->createCustomUrlAlias(44, '/path', true, 'eng-GB', true);
296
    }
297
298
    /**
299
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::createGlobalUrlAlias
300
     */
301
    public function testCreateGlobalUrlAlias()
302
    {
303
        $this->loggerMock->expects($this->once())->method('logCall');
304
305
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
306
        $this->persistenceHandlerMock
307
            ->expects($this->once())
308
            ->method('urlAliasHandler')
309
            ->will($this->returnValue($innerHandler));
310
311
        $innerHandler
312
            ->expects($this->once())
313
            ->method('createGlobalUrlAlias')
314
            ->with('/old', '/path', true, 'eng-GB', true)
315
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
316
317
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
318
        $this->cacheMock
319
            ->expects($this->once())
320
            ->method('getItem')
321
            ->with('urlAlias', 55)
322
            ->will($this->returnValue($cacheItemMock));
323
324
        $cacheItemMock
325
            ->expects($this->once())
326
            ->method('set')
327
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias'));
328
329
        $cacheItemMock
330
            ->expects($this->never())
331
            ->method('get');
332
333
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
334
        $handler->createGlobalUrlAlias('/old', '/path', true, 'eng-GB', true);
335
    }
336
337
    /**
338
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
339
     */
340 View Code Duplication
    public function testListURLAliasesForLocationIsMiss()
341
    {
342
        $this->loggerMock->expects($this->once())->method('logCall');
343
344
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
345
        $this->cacheMock
346
            ->expects($this->once())
347
            ->method('getItem')
348
            ->with('urlAlias', 'location', 44)
349
            ->will($this->returnValue($cacheItemMock));
350
351
        $cacheItemMock
352
            ->expects($this->once())
353
            ->method('get')
354
            ->will($this->returnValue(null));
355
356
        $cacheItemMock
357
            ->expects($this->once())
358
            ->method('isMiss')
359
            ->will($this->returnValue(true));
360
361
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
362
        $this->persistenceHandlerMock
363
            ->expects($this->once())
364
            ->method('urlAliasHandler')
365
            ->will($this->returnValue($innerHandler));
366
367
        $innerHandler
368
            ->expects($this->once())
369
            ->method('listURLAliasesForLocation')
370
            ->with(44, false)
371
            ->will(
372
                $this->returnValue(
373
                    array(
374
                        new UrlAlias(array('id' => 55)),
375
                        new UrlAlias(array('id' => 58)),
376
                        new UrlAlias(array('id' => 91)),
377
                    )
378
                )
379
            );
380
381
        $cacheItemMock
382
            ->expects($this->once())
383
            ->method('set')
384
            ->with(array(55, 58, 91));
385
386
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
387
        $handler->listURLAliasesForLocation(44, false);
388
    }
389
390
    /**
391
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
392
     */
393 View Code Duplication
    public function testListURLAliasesForLocationCustomIsMiss()
394
    {
395
        $this->loggerMock->expects($this->once())->method('logCall');
396
397
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
398
        $this->cacheMock
399
            ->expects($this->once())
400
            ->method('getItem')
401
            ->with('urlAlias', 'location', '44', 'custom')
402
            ->will($this->returnValue($cacheItemMock));
403
404
        $cacheItemMock
405
            ->expects($this->once())
406
            ->method('get')
407
            ->will($this->returnValue(null));
408
409
        $cacheItemMock
410
            ->expects($this->once())
411
            ->method('isMiss')
412
            ->will($this->returnValue(true));
413
414
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
415
        $this->persistenceHandlerMock
416
            ->expects($this->once())
417
            ->method('urlAliasHandler')
418
            ->will($this->returnValue($innerHandler));
419
420
        $innerHandler
421
            ->expects($this->once())
422
            ->method('listURLAliasesForLocation')
423
            ->with(44, true)
424
            ->will(
425
                $this->returnValue(
426
                    array(
427
                        new UrlAlias(array('id' => 55)),
428
                        new UrlAlias(array('id' => 58)),
429
                        new UrlAlias(array('id' => 91)),
430
                    )
431
                )
432
            );
433
434
        $cacheItemMock
435
            ->expects($this->once())
436
            ->method('set')
437
            ->with(array(55, 58, 91));
438
439
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
440
        $handler->listURLAliasesForLocation(44, true);
441
    }
442
443
    /**
444
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
445
     */
446 View Code Duplication
    public function testListURLAliasesForLocationHasCache()
447
    {
448
        $this->loggerMock->expects($this->never())->method('logCall');
449
450
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
451
        $this->cacheMock
452
            ->expects($this->at(0))
453
            ->method('getItem')
454
            ->with('urlAlias', 'location', 44)
455
            ->will($this->returnValue($cacheItemMock));
456
457
        $cacheItemMock
458
            ->expects($this->once())
459
            ->method('get')
460
            ->will($this->returnValue(array(55, 58, 91)));
461
462
        $cacheItemMock
463
            ->expects($this->once())
464
            ->method('isMiss')
465
            ->will($this->returnValue(false));
466
467
        $this->persistenceHandlerMock
468
            ->expects($this->never())
469
            ->method($this->anything());
470
471
        $cacheItemMock
472
            ->expects($this->never())
473
            ->method('set');
474
475
        // inline calls to loadUrlAlias() using the cache
476
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
477
        $this->cacheMock
478
            ->expects($this->at(1))
479
            ->method('getItem')
480
            ->with('urlAlias', 55)
481
            ->will($this->returnValue($cacheItemMock2));
482
483
        $cacheItemMock2
484
            ->expects($this->at(0))
485
            ->method('get')
486
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
487
488
        $this->cacheMock
489
            ->expects($this->at(2))
490
            ->method('getItem')
491
            ->with('urlAlias', 58)
492
            ->will($this->returnValue($cacheItemMock2));
493
494
        $cacheItemMock2
495
            ->expects($this->at(1))
496
            ->method('get')
497
            ->will($this->returnValue(new UrlAlias(array('id' => 58))));
498
499
        $this->cacheMock
500
            ->expects($this->at(3))
501
            ->method('getItem')
502
            ->with('urlAlias', 91)
503
            ->will($this->returnValue($cacheItemMock2));
504
505
        $cacheItemMock2
506
            ->expects($this->at(2))
507
            ->method('get')
508
            ->will($this->returnValue(new UrlAlias(array('id' => 91))));
509
510
        $cacheItemMock2
511
            ->expects($this->exactly(3))
512
            ->method('isMiss')
513
            ->will($this->returnValue(false));
514
515
        $cacheItemMock2
516
            ->expects($this->never())
517
            ->method('set');
518
519
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
520
        $handler->listURLAliasesForLocation(44, false);
521
    }
522
523
    /**
524
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::listURLAliasesForLocation
525
     */
526 View Code Duplication
    public function testListURLAliasesForLocationCustomHasCache()
527
    {
528
        $this->loggerMock->expects($this->never())->method('logCall');
529
530
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
531
        $this->cacheMock
532
            ->expects($this->at(0))
533
            ->method('getItem')
534
            ->with('urlAlias', 'location', '44', 'custom')
535
            ->will($this->returnValue($cacheItemMock));
536
537
        $cacheItemMock
538
            ->expects($this->once())
539
            ->method('get')
540
            ->will($this->returnValue(array(55, 58, 91)));
541
542
        $cacheItemMock
543
            ->expects($this->once())
544
            ->method('isMiss')
545
            ->will($this->returnValue(false));
546
547
        $this->persistenceHandlerMock
548
            ->expects($this->never())
549
            ->method($this->anything());
550
551
        $cacheItemMock
552
            ->expects($this->never())
553
            ->method('set');
554
555
                // inline calls to loadUrlAlias() using the cache
556
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
557
        $this->cacheMock
558
            ->expects($this->at(1))
559
            ->method('getItem')
560
            ->with('urlAlias', 55)
561
            ->will($this->returnValue($cacheItemMock2));
562
563
        $cacheItemMock2
564
            ->expects($this->at(0))
565
            ->method('get')
566
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
567
568
        $this->cacheMock
569
            ->expects($this->at(2))
570
            ->method('getItem')
571
            ->with('urlAlias', 58)
572
            ->will($this->returnValue($cacheItemMock2));
573
574
        $cacheItemMock2
575
            ->expects($this->at(1))
576
            ->method('get')
577
            ->will($this->returnValue(new UrlAlias(array('id' => 58))));
578
579
        $this->cacheMock
580
            ->expects($this->at(3))
581
            ->method('getItem')
582
            ->with('urlAlias', 91)
583
            ->will($this->returnValue($cacheItemMock2));
584
585
        $cacheItemMock2
586
            ->expects($this->at(2))
587
            ->method('get')
588
            ->will($this->returnValue(new UrlAlias(array('id' => 91))));
589
590
        $cacheItemMock2
591
            ->expects($this->exactly(3))
592
            ->method('isMiss')
593
            ->will($this->returnValue(false));
594
595
        $cacheItemMock2
596
            ->expects($this->never())
597
            ->method('set');
598
599
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
600
        $handler->listURLAliasesForLocation(44, true);
601
    }
602
603
    /**
604
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::removeURLAliases
605
     */
606
    public function testRemoveURLAliases()
607
    {
608
        $this->loggerMock->expects($this->once())->method('logCall');
609
610
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
611
        $this->persistenceHandlerMock
612
            ->expects($this->once())
613
            ->method('urlAliasHandler')
614
            ->will($this->returnValue($innerHandler));
615
616
        $innerHandler
617
            ->expects($this->once())
618
            ->method('removeURLAliases');
619
620
        $this->cacheMock
621
            ->expects($this->at(0))
622
            ->method('clear')
623
            ->with('urlAlias', 'url');
624
625
        $this->cacheMock
626
            ->expects($this->at(1))
627
            ->method('clear')
628
            ->with('urlAlias', 21);
629
630
        $this->cacheMock
631
            ->expects($this->at(2))
632
            ->method('clear')
633
            ->with('urlAlias', 32);
634
635
        $this->cacheMock
636
            ->expects($this->at(3))
637
            ->method('clear')
638
            ->with('urlAlias', 'location', 44);
639
640
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
641
        $handler->removeURLAliases(
642
            array(
643
                new UrlAlias(array('id' => 21)),
644
                new UrlAlias(array('id' => 32, 'type' => UrlAlias::LOCATION, 'destination' => 44)),
645
            )
646
        );
647
    }
648
649
    /**
650
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
651
     * @group justme
652
     */
653
    public function testLookupIsMiss()
654
    {
655
        $urlAlias = new UrlAlias(array('id' => 55));
656
657
        $this->loggerMock->expects($this->once())->method('logCall');
658
659
        $missedUrlAliasIdCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
660
        $missedUrlAliasIdCacheItem
661
            ->expects($this->once())
662
            ->method('get')
663
            ->will($this->returnValue(null));
664
665
        $missedUrlAliasIdCacheItem
666
            ->expects($this->once())
667
            ->method('isMiss')
668
            ->will($this->returnValue(true));
669
670
        $missedUrlAliasIdCacheItem
671
            ->expects($this->once())
672
            ->method('set')
673
            ->with(55);
674
675
        $newUrlAliasCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
676
        $newUrlAliasCacheItem
677
            ->expects($this->once())
678
             ->method('set')
679
            ->with($urlAlias);
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', 55)
690
                ->will($this->returnValue($newUrlAliasCacheItem));
691
692
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
693
        $this->persistenceHandlerMock
694
            ->expects($this->once())
695
            ->method('urlAliasHandler')
696
            ->will($this->returnValue($innerHandler));
697
698
        $innerHandler
699
            ->expects($this->once())
700
            ->method('lookup')
701
            ->with('/url')
702
            ->will($this->returnValue($urlAlias));
703
704
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
705
        $handler->lookup('/url');
706
    }
707
708
    /**
709
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::lookup
710
     */
711 View Code Duplication
    public function testLookupHasCache()
712
    {
713
        $this->loggerMock->expects($this->never())->method('logCall');
714
715
        $this->persistenceHandlerMock
716
            ->expects($this->never())
717
            ->method($this->anything());
718
719
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
720
        $this->cacheMock
721
            ->expects($this->at(0))
722
            ->method('getItem')
723
            ->with('urlAlias', 'url', '/url')
724
            ->will($this->returnValue($cacheItemMock));
725
726
        $cacheItemMock
727
            ->expects($this->once())
728
            ->method('get')
729
            ->will($this->returnValue(55));
730
731
        $cacheItemMock
732
            ->expects($this->once())
733
            ->method('isMiss')
734
            ->will($this->returnValue(false));
735
736
        $cacheItemMock
737
            ->expects($this->never())
738
            ->method('set');
739
740
        $cacheItemMock2 = $this->getMock('Stash\Interfaces\ItemInterface');
741
        $this->cacheMock
742
            ->expects($this->at(1))
743
            ->method('getItem')
744
            ->with('urlAlias', 55)
745
            ->will($this->returnValue($cacheItemMock2));
746
747
        $cacheItemMock2
748
            ->expects($this->once())
749
            ->method('get')
750
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
751
752
        $cacheItemMock2
753
            ->expects($this->once())
754
            ->method('isMiss')
755
            ->will($this->returnValue(false));
756
757
        $cacheItemMock2
758
            ->expects($this->never())
759
            ->method('set');
760
761
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
762
        $handler->lookup('/url');
763
    }
764
765
    /**
766
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias
767
     */
768 View Code Duplication
    public function testLoadUrlAliasIsMiss()
769
    {
770
        $this->loggerMock->expects($this->once())->method('logCall');
771
772
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
773
        $this->cacheMock
774
            ->expects($this->once())
775
            ->method('getItem')
776
            ->with('urlAlias', 55)
777
            ->will($this->returnValue($cacheItemMock));
778
779
        $cacheItemMock
780
            ->expects($this->once())
781
            ->method('get')
782
            ->will($this->returnValue(null));
783
784
        $cacheItemMock
785
            ->expects($this->once())
786
            ->method('isMiss')
787
            ->will($this->returnValue(true));
788
789
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
790
        $this->persistenceHandlerMock
791
            ->expects($this->once())
792
            ->method('urlAliasHandler')
793
            ->will($this->returnValue($innerHandler));
794
795
        $innerHandler
796
            ->expects($this->once())
797
            ->method('loadUrlAlias')
798
            ->with(55)
799
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
800
801
        $cacheItemMock
802
            ->expects($this->once())
803
            ->method('set')
804
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias'));
805
806
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
807
        $handler->loadUrlAlias(55);
808
    }
809
810
    /**
811
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::loadUrlAlias
812
     */
813 View Code Duplication
    public function testLoadUrlAliasHasCache()
814
    {
815
        $this->loggerMock->expects($this->never())->method('logCall');
816
817
        $this->persistenceHandlerMock
818
            ->expects($this->never())
819
            ->method($this->anything());
820
821
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
822
        $this->cacheMock
823
            ->expects($this->once())
824
            ->method('getItem')
825
            ->with('urlAlias', 55)
826
            ->will($this->returnValue($cacheItemMock));
827
828
        $cacheItemMock
829
            ->expects($this->once())
830
            ->method('get')
831
            ->will($this->returnValue(new UrlAlias(array('id' => 55))));
832
833
        $cacheItemMock
834
            ->expects($this->once())
835
            ->method('isMiss')
836
            ->will($this->returnValue(false));
837
838
        $cacheItemMock
839
            ->expects($this->never())
840
            ->method('set');
841
842
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
843
        $handler->loadUrlAlias(55);
844
    }
845
846
    /**
847
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationMoved
848
     */
849 View Code Duplication
    public function testLocationMoved()
850
    {
851
        $this->loggerMock->expects($this->once())->method('logCall');
852
853
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
854
        $this->persistenceHandlerMock
855
            ->expects($this->once())
856
            ->method('urlAliasHandler')
857
            ->will($this->returnValue($innerHandler));
858
859
        $innerHandler
860
            ->expects($this->once())
861
            ->method('locationMoved')
862
            ->with(44, 2, 45);
863
864
        $this->cacheMock
865
            ->expects($this->once())
866
            ->method('clear')
867
            ->with('urlAlias')
868
            ->will($this->returnValue(null));
869
870
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
871
        $handler->locationMoved(44, 2, 45);
872
    }
873
874
    /**
875
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted
876
     */
877
    public function testLocationDeletedWithoutCachedLocation()
878
    {
879
        $locationNotCached = $this->getMock('Stash\Interfaces\ItemInterface');
880
        $locationNotCached
881
            ->expects($this->once())
882
            ->method('isMiss')
883
            ->willReturn(true);
884
        $locationNotCached
885
            ->expects($this->never())
886
            ->method('clear');
887
888
        $this->prepareDeleteMocks($locationNotCached);
889
890
        $this->cacheMock
891
            ->expects($this->once())
892
            ->method('clear')
893
            ->with('urlAlias');
894
895
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
896
        $handler->locationDeleted(44);
897
    }
898
899
    /**
900
     * @covers eZ\Publish\Core\Persistence\Cache\UrlAliasHandler::locationDeleted
901
     */
902
    public function testLocationDeletedWithCachedLocation()
903
    {
904
        $locationCacheItem = $this->getMock('Stash\Interfaces\ItemInterface');
905
        $locationCacheItem
906
            ->expects($this->once())
907
            ->method('isMiss')
908
            ->willReturn(false);
909
        $locationCacheItem
910
            ->expects($this->once())
911
            ->method('get')
912
            ->willReturn(['44'])
913
        ;
914
        $locationCacheItem
915
            ->expects($this->once())
916
            ->method('clear')
917
            ->will($this->returnValue(null));
918
919
        $this->prepareDeleteMocks($locationCacheItem);
920
921
        $this->cacheMock
922
            ->expects($this->at(1))
923
            ->method('clear')
924
            ->with('urlAlias', 44);
925
        $this->cacheMock
926
            ->expects($this->at(2))
927
            ->method('clear')
928
            ->with('urlAlias', 'url');
929
930
        $handler = $this->persistenceCacheHandler->urlAliasHandler();
931
        $handler->locationDeleted(44);
932
    }
933
934
    /**
935
     * @param $locationCacheMissed
936
     */
937 View Code Duplication
    protected function prepareDeleteMocks($locationCacheMissed)
938
    {
939
        $this->loggerMock->expects($this->once())->method('logCall');
940
941
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\UrlAlias\\Handler');
942
        $this->persistenceHandlerMock
943
            ->expects($this->once())
944
            ->method('urlAliasHandler')
945
            ->will($this->returnValue($innerHandler));
946
947
        $innerHandler->expects($this->once())->method('locationDeleted')->with(44);
948
949
        $this->cacheMock
950
            ->expects($this->once())
951
            ->method('getItem')
952
            ->willReturn($locationCacheMissed);
953
    }
954
}
955