Completed
Push — sf_cache ( ff1daa...dd5469 )
by André
15:49
created

testLoadMethodsCacheHit()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 22

Duplication

Lines 13
Ratio 40.63 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 2
nop 5
dl 13
loc 32
rs 8.8571
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->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
     * @param bool $multi Default false, set to true if method will lookup several cache items.
79
     */
80
    final public function testLoadMethodsCacheHit(string $method, array $arguments, string $key, $data = null, bool $multi = false)
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
106
107
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
108
        $return = call_user_func_array([$handler, $method], $arguments);
109
110
        $this->assertEquals($data, $return);
111
    }
112
113
    /**
114
     * @dataProvider providerForCachedLoadMethods
115
     *
116
     * @param string $method
117
     * @param array $arguments
118
     * @param string $key
119
     * @param object $data
120
     * @param bool $multi Default false, set to true if method will lookup several cache items.
121
     */
122
    final public function testLoadMethodsCacheMiss(string $method, array $arguments, string $key, $data = null, bool $multi = false)
123
    {
124
        $cacheItem = $this->getCacheItem($key, null);
125
        $handlerMethodName = $this->getHandlerMethodName();
126
127
        $this->loggerMock->expects($this->once())->method('logCall');
128
129 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...
130
            $this->cacheMock
131
                ->expects($this->once())
132
                ->method('getItems')
133
                ->with([$cacheItem->getKey()])
134
                ->willReturn([$key => $cacheItem]);
135
        } else {
136
            $this->cacheMock
137
                ->expects($this->once())
138
                ->method('getItem')
139
                ->with($cacheItem->getKey())
140
                ->willReturn($cacheItem);
141
        }
142
143
        $innerHandlerMock = $this->getMock($this->getHandlerClassName());
144
        $this->persistenceHandlerMock
145
            ->expects($this->once())
146
            ->method($handlerMethodName)
147
            ->willReturn($innerHandlerMock);
148
149
        $innerHandlerMock
150
            ->expects($this->once())
151
            ->method($method)
152
            ->with(...$arguments)
153
            ->willReturn($data);
154
155
        $this->cacheMock
156
            ->expects($this->once())
157
            ->method('save')
158
            ->with($cacheItem);
159
160
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
161
        $return = call_user_func_array([$handler, $method], $arguments);
162
163
        $this->assertEquals($data, $return);
164
165
        // Assert use of tags would probably need custom logic as internal property is [$tag => $tag] value and we don't want to know that.
166
        //$this->assertAttributeEquals([], 'tags', $cacheItem);
167
    }
168
}
169