1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
7
|
|
|
use Roave\DoctrineSimpleCache\CacheException; |
8
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
9
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; |
10
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache; |
11
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache; |
12
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter |
16
|
|
|
*/ |
17
|
|
|
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
20
|
|
|
{ |
21
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
22
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
23
|
|
|
|
24
|
|
|
$this->expectException(CacheException::class); |
25
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
29
|
|
|
{ |
30
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
31
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
32
|
|
|
|
33
|
|
|
$this->expectException(CacheException::class); |
34
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
38
|
|
|
{ |
39
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
40
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
41
|
|
|
|
42
|
|
|
$this->expectException(CacheException::class); |
43
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetProxiesToDoctrineFetch() |
47
|
|
|
{ |
48
|
|
|
$key = uniqid('key', true); |
49
|
|
|
$value = uniqid('value', true); |
50
|
|
|
|
51
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
52
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
53
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
56
|
|
|
self::assertSame($value, $psrCache->get($key)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetWithNotExistingKey() |
60
|
|
|
{ |
61
|
|
|
$key = uniqid('key', true); |
62
|
|
|
$value = uniqid('value', true); |
63
|
|
|
|
64
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
65
|
|
|
$psrCache->set($key, $value); |
66
|
|
|
|
67
|
|
|
$default = uniqid('default', true); |
68
|
|
|
self::assertSame($value, $psrCache->get($key, $default)); |
69
|
|
|
|
70
|
|
|
$anotherKey = uniqid('key', true); |
71
|
|
|
self::assertSame($default, $psrCache->get($anotherKey, $default)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testSetProxiesToDoctrineSave() |
75
|
|
|
{ |
76
|
|
|
$key = uniqid('key', true); |
77
|
|
|
$value = uniqid('value', true); |
78
|
|
|
$ttl = random_int(1000, 9999); |
79
|
|
|
|
80
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
81
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
82
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
85
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$key = uniqid('key', true); |
91
|
|
|
|
92
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
93
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
94
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
97
|
|
|
self::assertTrue($psrCache->delete($key)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testClearProxiesToDeleteAll() |
101
|
|
|
{ |
102
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
103
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
104
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
107
|
|
|
self::assertTrue($psrCache->clear()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
111
|
|
|
{ |
112
|
|
|
$values = [ |
113
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
114
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
115
|
|
|
]; |
116
|
|
|
$keys = array_keys($values); |
117
|
|
|
|
118
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
119
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
120
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
123
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetMultipleWithPartialKeys() |
127
|
|
|
{ |
128
|
|
|
$values = [ |
129
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
130
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
131
|
|
|
]; |
132
|
|
|
$keys = array_keys($values); |
133
|
|
|
|
134
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
135
|
|
|
$psrCache->setMultiple($values); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$default = uniqid('default', true); |
138
|
|
|
$invalid_key = uniqid('key3', true); |
139
|
|
|
$keys[] = $invalid_key; |
140
|
|
|
$values[$invalid_key] = $default; |
141
|
|
|
|
142
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys, $default)); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
146
|
|
|
{ |
147
|
|
|
$values = [ |
148
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
149
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
153
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
154
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
157
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$keys = [ |
163
|
|
|
uniqid('key1', true), |
164
|
|
|
uniqid('key2', true), |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
168
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
169
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
170
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
171
|
|
|
|
172
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
173
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$keys = [ |
179
|
|
|
uniqid('key1', true), |
180
|
|
|
uniqid('key2', true), |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
184
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
185
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
186
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
187
|
|
|
|
188
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
189
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$key = uniqid('key', true); |
195
|
|
|
|
196
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
197
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
198
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
201
|
|
|
self::assertTrue($psrCache->has($key)); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.