Completed
Push — inmemory-abstract-trait ( 74ab21...7860f3 )
by André
83:26 queued 52:33
created

AbstractCacheHandlerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 188
Duplicated Lines 27.66 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 52
loc 188
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
getHandlerMethodName() 0 1 ?
getHandlerClassName() 0 1 ?
providerForUnCachedMethods() 0 1 ?
providerForCachedLoadMethods() 0 1 ?
A testUnCachedMethods() 0 37 5
B testLoadMethodsCacheHit() 20 42 5
B testLoadMethodsCacheMiss() 32 65 4

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