Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

providerForCachedLoadMethods()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
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->createMock($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
    abstract public function providerForCachedLoadMethods(): array;
69
70
    /**
71
     * @dataProvider providerForCachedLoadMethods
72
     *
73
     * @param string $method
74
     * @param array $arguments
75
     * @param string $key
76
     * @param mixed $data
77
     * @param bool $multi Default false, set to true if method will lookup several cache items.
78
     * @param array $additionalCalls Sets of additional calls being made to handlers, with 4 values (0: handler name, 1: handler class, 2: method, 3: return data)
79
     */
80
    final public function testLoadMethodsCacheHit(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [])
81
    {
82
        $cacheItem = $this->getCacheItem($key, $multi ? reset($data) : $data);
83
        $handlerMethodName = $this->getHandlerMethodName();
84
85
        $this->loggerMock->expects($this->never())->method('logCall');
86
87 View Code Duplication
        if ($multi) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            $this->cacheMock
89
                ->expects($this->once())
90
                ->method('getItems')
91
                ->with([$cacheItem->getKey()])
92
                ->willReturn([$key => $cacheItem]);
93
        } else {
94
            $this->cacheMock
95
                ->expects($this->once())
96
                ->method('getItem')
97
                ->with($cacheItem->getKey())
98
                ->willReturn($cacheItem);
99
        }
100
101
        $this->persistenceHandlerMock
102
            ->expects($this->never())
103
            ->method($handlerMethodName);
104
105
        foreach ($additionalCalls as $additionalCall) {
106
            $this->persistenceHandlerMock
107
                ->expects($this->never())
108
                ->method($additionalCall[0]);
109
        }
110
111
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
112
        $return = call_user_func_array([$handler, $method], $arguments);
113
114
        $this->assertEquals($data, $return);
115
    }
116
117
    /**
118
     * @dataProvider providerForCachedLoadMethods
119
     *
120
     * @param string $method
121
     * @param array $arguments
122
     * @param string $key
123
     * @param object $data
124
     * @param bool $multi Default false, set to true if method will lookup several cache items.
125
     * @param array $additionalCalls Sets of additional calls being made to handlers, with 4 values (0: handler name, 1: handler class, 2: method, 3: return data)
126
     */
127
    final public function testLoadMethodsCacheMiss(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [])
128
    {
129
        $cacheItem = $this->getCacheItem($key, null);
130
        $handlerMethodName = $this->getHandlerMethodName();
131
132
        $this->loggerMock->expects($this->once())->method('logCall');
133
134 View Code Duplication
        if ($multi) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
            $this->cacheMock
136
                ->expects($this->once())
137
                ->method('getItems')
138
                ->with([$cacheItem->getKey()])
139
                ->willReturn([$key => $cacheItem]);
140
        } else {
141
            $this->cacheMock
142
                ->expects($this->once())
143
                ->method('getItem')
144
                ->with($cacheItem->getKey())
145
                ->willReturn($cacheItem);
146
        }
147
148
        $innerHandlerMock = $this->createMock($this->getHandlerClassName());
149
        $this->persistenceHandlerMock
150
            ->expects($this->once())
151
            ->method($handlerMethodName)
152
            ->willReturn($innerHandlerMock);
153
154
        $innerHandlerMock
155
            ->expects($this->once())
156
            ->method($method)
157
            ->with(...$arguments)
158
            ->willReturn($data);
159
160
        foreach ($additionalCalls as $additionalCall) {
161
            $innerHandlerMock = $this->createMock($additionalCall[1]);
162
            $this->persistenceHandlerMock
163
                ->expects($this->once())
164
                ->method($additionalCall[0])
165
                ->willReturn($innerHandlerMock);
166
167
            $innerHandlerMock
168
                ->expects($this->once())
169
                ->method($additionalCall[2])
170
                ->willReturn($additionalCall[3]);
171
        }
172
173
        $this->cacheMock
174
            ->expects($this->once())
175
            ->method('save')
176
            ->with($cacheItem);
177
178
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
179
        $return = call_user_func_array([$handler, $method], $arguments);
180
181
        $this->assertEquals($data, $return);
182
183
        // Assert use of tags would probably need custom logic as internal property is [$tag => $tag] value and we don't want to know that.
184
        //$this->assertAttributeEquals([], 'tags', $cacheItem);
185
    }
186
}
187