Completed
Push — sf_cache ( 8c890f...ff1daa )
by André
14:18
created

AbstractCacheHandlerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
getHandlerMethodName() 0 1 ?
getHandlerClassName() 0 1 ?
providerForUnCachedMethods() 0 1 ?
B testUnCachedMethods() 0 37 5
providerForCachedLoadMethods() 0 1 ?
A testLoadMethodsCacheHit() 0 20 1
B testLoadMethodsCacheMiss() 0 34 1
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
/**
12
 * Abstract test case for spi cache impl.
13
 */
14
abstract class AbstractCacheHandlerTest extends AbstractBaseHandlerTest
15
{
16
    abstract public function getHandlerMethodName() : string;
17
18
    abstract public function getHandlerClassName() : string;
19
20
    abstract public function providerForUnCachedMethods() : array;
21
22
    /**
23
     * @dataProvider providerForUnCachedMethods
24
     *
25
     * @param string $method
26
     * @param array $arguments
27
     * @param array|null $tags
28
     * @param string|null $key
29
     */
30
    final public function testUnCachedMethods(string $method, array $arguments, array $tags = null, string $key = null)
31
    {
32
        $handlerMethodName = $this->getHandlerMethodName();
33
34
        $this->loggerMock->expects($this->once())->method('logCall');
35
36
        $innerHandler = $this->getMock($this->getHandlerClassName());
37
        $this->persistenceHandlerMock
38
            ->expects($this->once())
39
            ->method($handlerMethodName)
40
            ->will($this->returnValue($innerHandler));
41
42
        $innerHandler
43
            ->expects($this->once())
44
            ->method($method)
45
            ->with(...$arguments)
46
            ->will($this->returnValue(null));
47
48
        if ($tags || $key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
            $this->cacheMock
50
                ->expects(!empty($tags) ? $this->once() : $this->never())
51
                ->method('invalidateTags')
52
                ->with($tags);
53
54
            $this->cacheMock
55
                ->expects(!empty($key) ? $this->once() : $this->never())
56
                ->method('deleteItem')
57
                ->with($key);
58
        } else {
59
            $this->cacheMock
60
                ->expects($this->never())
61
                ->method($this->anything());
62
        }
63
64
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
65
        call_user_func_array(array($handler, $method), $arguments);
66
    }
67
68
69
    abstract public function providerForCachedLoadMethods() : array;
70
71
    /**
72
     * @dataProvider providerForCachedLoadMethods
73
     *
74
     * @param string $method
75
     * @param array $arguments
76
     * @param string $key
77
     * @param mixed $data
78
     */
79
    final public function testLoadMethodsCacheHit(string $method, array $arguments, string $key, $data = null)
80
    {
81
        $cacheItem = $this->getCacheItem($key, $data);
82
        $handlerMethodName = $this->getHandlerMethodName();
83
84
        $this->loggerMock->expects($this->never())->method('logCall');
85
86
        $this->cacheMock
87
            ->expects($this->once())
88
            ->method('getItem')
89
            ->with($cacheItem->getKey())
90
            ->will($this->returnValue($cacheItem));
91
92
        $this->persistenceHandlerMock
93
            ->expects($this->never())
94
            ->method($handlerMethodName);
95
96
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
97
        call_user_func_array([$handler, $method], $arguments);
98
    }
99
100
    /**
101
     * @dataProvider providerForCachedLoadMethods
102
     *
103
     * @param string $method
104
     * @param array $arguments
105
     * @param string $key
106
     * @param object $data
107
     */
108
    final public function testLoadMethodsCacheMiss(string $method, array $arguments, string $key, $data = null)
109
    {
110
        $cacheItem = $this->getCacheItem($key, null);
111
        $handlerMethodName = $this->getHandlerMethodName();
112
113
        $this->loggerMock->expects($this->once())->method('logCall');
114
115
        $this->cacheMock
116
            ->expects($this->once())
117
            ->method('getItem')
118
            ->with($cacheItem->getKey())
119
            ->will($this->returnValue($cacheItem));
120
121
        $innerHandlerMock = $this->getMock($this->getHandlerClassName());
122
        $this->persistenceHandlerMock
123
            ->expects($this->once())
124
            ->method($handlerMethodName)
125
            ->will($this->returnValue($innerHandlerMock));
126
127
        $innerHandlerMock
128
            ->expects($this->once())
129
            ->method($method)
130
            ->with(...$arguments)
131
            ->will($this->returnValue($data));
132
133
        // @todo find way to also verify tags on the cache item
134
        $this->cacheMock
135
            ->expects($this->once())
136
            ->method('save')
137
            ->with($cacheItem);
138
139
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
140
        call_user_func_array([$handler, $method], $arguments);
141
    }
142
}
143