Completed
Push — master ( 955b04...f00653 )
by André
38:12 queued 16:07
created

testLoadMethodsCacheMiss()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 61

Duplication

Lines 12
Ratio 19.67 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 6
dl 12
loc 61
rs 8.8509
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
declare(strict_types=1);
10
11
namespace eZ\Publish\Core\Persistence\Cache\Tests;
12
13
/**
14
 * Abstract test case for spi cache impl, with in-memory handling.
15
 */
16
abstract class AbstractInMemoryCacheHandlerTest extends AbstractBaseHandlerTest
17
{
18
    abstract public function getHandlerMethodName(): string;
19
20
    abstract public function getHandlerClassName(): string;
21
22
    abstract public function providerForUnCachedMethods(): array;
23
24
    /**
25
     * @dataProvider providerForUnCachedMethods
26
     *
27
     * @param string $method
28
     * @param array $arguments
29
     * @param array|null $tags
30
     * @param array|null $key
31
     * @param mixed $returnValue
32
     */
33
    final public function testUnCachedMethods(string $method, array $arguments, array $tags = null, array $key = null, $returnValue = null)
34
    {
35
        $handlerMethodName = $this->getHandlerMethodName();
36
37
        $this->loggerMock->expects($this->once())->method('logCall');
38
        $this->loggerMock->expects($this->never())->method('logCacheHit');
39
        $this->loggerMock->expects($this->never())->method('logCacheMiss');
40
41
        $innerHandler = $this->createMock($this->getHandlerClassName());
42
        $this->persistenceHandlerMock
43
            ->expects($this->once())
44
            ->method($handlerMethodName)
45
            ->will($this->returnValue($innerHandler));
46
47
        $innerHandler
48
            ->expects($this->once())
49
            ->method($method)
50
            ->with(...$arguments)
51
            ->will($this->returnValue($returnValue));
52
53 View Code Duplication
        if ($tags || $key) {
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...
54
            $this->cacheMock
55
                ->expects(!empty($tags) ? $this->once() : $this->never())
56
                ->method('invalidateTags')
57
                ->with($tags);
58
59
            $this->cacheMock
60
                ->expects(!empty($key) ? $this->once() : $this->never())
61
                ->method('deleteItems')
62
                ->with($key);
63
        } else {
64
            $this->cacheMock
65
                ->expects($this->never())
66
                ->method($this->anything());
67
        }
68
69
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
70
        call_user_func_array(array($handler, $method), $arguments);
71
    }
72
73
    abstract public function providerForCachedLoadMethods(): array;
74
75
    /**
76
     * @dataProvider providerForCachedLoadMethods
77
     *
78
     * @param string $method
79
     * @param array $arguments
80
     * @param string $key
81
     * @param mixed $data
82
     * @param bool $multi Default false, set to true if method will lookup several cache items.
83
     * @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)
84
     */
85
    final public function testLoadMethodsCacheHit(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [])
86
    {
87
        $cacheItem = $this->getCacheItem($key, $multi ? reset($data) : $data);
88
        $handlerMethodName = $this->getHandlerMethodName();
89
90
        $this->loggerMock->expects($this->once())->method('logCacheHit');
91
        $this->loggerMock->expects($this->never())->method('logCall');
92
        $this->loggerMock->expects($this->never())->method('logCacheMiss');
93
94
        if ($multi) {
95
            $this->cacheMock
96
                ->expects($this->once())
97
                ->method('getItems')
98
                ->with([$cacheItem->getKey()])
99
                ->willReturn([$key => $cacheItem]);
100
        } else {
101
            $this->cacheMock
102
                ->expects($this->once())
103
                ->method('getItem')
104
                ->with($cacheItem->getKey())
105
                ->willReturn($cacheItem);
106
        }
107
108
        $this->persistenceHandlerMock
109
            ->expects($this->never())
110
            ->method($handlerMethodName);
111
112
        foreach ($additionalCalls as $additionalCall) {
113
            $this->persistenceHandlerMock
114
                ->expects($this->never())
115
                ->method($additionalCall[0]);
116
        }
117
118
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
119
        $return = call_user_func_array([$handler, $method], $arguments);
120
121
        $this->assertEquals($data, $return);
122
    }
123
124
    /**
125
     * @dataProvider providerForCachedLoadMethods
126
     *
127
     * @param string $method
128
     * @param array $arguments
129
     * @param string $key
130
     * @param object $data
131
     * @param bool $multi Default false, set to true if method will lookup several cache items.
132
     * @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)
133
     */
134
    final public function testLoadMethodsCacheMiss(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [])
135
    {
136
        $cacheItem = $this->getCacheItem($key, null);
137
        $handlerMethodName = $this->getHandlerMethodName();
138
139
        $this->loggerMock->expects($this->once())->method('logCacheMiss');
140
        $this->loggerMock->expects($this->never())->method('logCall');
141
        $this->loggerMock->expects($this->never())->method('logCacheHit');
142
143
        if ($multi) {
144
            $this->cacheMock
145
                ->expects($this->once())
146
                ->method('getItems')
147
                ->with([$cacheItem->getKey()])
148
                ->willReturn([$key => $cacheItem]);
149
        } else {
150
            $this->cacheMock
151
                ->expects($this->once())
152
                ->method('getItem')
153
                ->with($cacheItem->getKey())
154
                ->willReturn($cacheItem);
155
        }
156
157
        $innerHandlerMock = $this->createMock($this->getHandlerClassName());
158
        $this->persistenceHandlerMock
159
            ->expects($this->once())
160
            ->method($handlerMethodName)
161
            ->willReturn($innerHandlerMock);
162
163
        $innerHandlerMock
164
            ->expects($this->once())
165
            ->method($method)
166
            ->with(...$arguments)
167
            ->willReturn($data);
168
169 View Code Duplication
        foreach ($additionalCalls as $additionalCall) {
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...
170
            $innerHandlerMock = $this->createMock($additionalCall[1]);
171
            $this->persistenceHandlerMock
172
                ->expects($this->once())
173
                ->method($additionalCall[0])
174
                ->willReturn($innerHandlerMock);
175
176
            $innerHandlerMock
177
                ->expects($this->once())
178
                ->method($additionalCall[2])
179
                ->willReturn($additionalCall[3]);
180
        }
181
182
        $this->cacheMock
183
            ->expects($this->once())
184
            ->method('save')
185
            ->with($cacheItem);
186
187
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
188
        $return = call_user_func_array([$handler, $method], $arguments);
189
190
        $this->assertEquals($data, $return);
191
192
        // Assert use of tags would probably need custom logic as internal property is [$tag => $tag] value and we don't want to know that.
193
        //$this->assertAttributeEquals([], 'tags', $cacheItem);
194
    }
195
}
196