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\Exception\InvalidArgumentException; |
9
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
10
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache; |
11
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache; |
12
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache; |
13
|
|
|
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter |
17
|
|
|
*/ |
18
|
|
|
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
public function validKeys() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
['AbC19_.'], |
24
|
|
|
['1234567890123456789012345678901234567890123456789012345678901234'], |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function invalidKeys() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
[''], |
32
|
|
|
[true], |
33
|
|
|
[false], |
34
|
|
|
[null], |
35
|
|
|
[2], |
36
|
|
|
[2.5], |
37
|
|
|
['{str'], |
38
|
|
|
['rand{'], |
39
|
|
|
['rand{str'], |
40
|
|
|
['rand}str'], |
41
|
|
|
['rand(str'], |
42
|
|
|
['rand)str'], |
43
|
|
|
['rand/str'], |
44
|
|
|
['rand\\str'], |
45
|
|
|
['rand@str'], |
46
|
|
|
['rand:str'], |
47
|
|
|
[new \stdClass()], |
48
|
|
|
[['array']], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
53
|
|
|
{ |
54
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
55
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
56
|
|
|
|
57
|
|
|
$this->expectException(CacheException::class); |
58
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
62
|
|
|
{ |
63
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
64
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
65
|
|
|
|
66
|
|
|
$this->expectException(CacheException::class); |
67
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
71
|
|
|
{ |
72
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
73
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
74
|
|
|
|
75
|
|
|
$this->expectException(CacheException::class); |
76
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testGetProxiesToDoctrineFetch() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$key = uniqid('key', true); |
82
|
|
|
$value = uniqid('value', true); |
83
|
|
|
|
84
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
85
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
86
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
89
|
|
|
self::assertSame($value, $psrCache->get($key)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetWithNotExistingKey() |
93
|
|
|
{ |
94
|
|
|
$key = uniqid('key', true); |
95
|
|
|
$value = uniqid('value', true); |
96
|
|
|
|
97
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
98
|
|
|
$psrCache->set($key, $value); |
99
|
|
|
|
100
|
|
|
$default = uniqid('default', true); |
101
|
|
|
self::assertSame($value, $psrCache->get($key, $default)); |
102
|
|
|
|
103
|
|
|
$anotherKey = uniqid('key', true); |
104
|
|
|
self::assertSame($default, $psrCache->get($anotherKey, $default)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testSetProxiesToDoctrineSave() |
108
|
|
|
{ |
109
|
|
|
$key = uniqid('key', true); |
110
|
|
|
$value = uniqid('value', true); |
111
|
|
|
$ttl = random_int(1000, 9999); |
112
|
|
|
|
113
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
114
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
115
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
118
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$key = uniqid('key', true); |
124
|
|
|
|
125
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
126
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
127
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
130
|
|
|
self::assertTrue($psrCache->delete($key)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testClearProxiesToDeleteAll() |
134
|
|
|
{ |
135
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
136
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
137
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
138
|
|
|
|
139
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
140
|
|
|
self::assertTrue($psrCache->clear()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
144
|
|
|
{ |
145
|
|
|
$values = [ |
146
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
147
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
148
|
|
|
]; |
149
|
|
|
$keys = array_keys($values); |
150
|
|
|
|
151
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
152
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
153
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
156
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testGetMultipleWithPartialKeys() |
160
|
|
|
{ |
161
|
|
|
$values = [ |
162
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
163
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
164
|
|
|
]; |
165
|
|
|
$keys = array_keys($values); |
166
|
|
|
|
167
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
168
|
|
|
$psrCache->setMultiple($values); |
169
|
|
|
|
170
|
|
|
$default = uniqid('default', true); |
171
|
|
|
$invalid_key = uniqid('key3', true); |
172
|
|
|
$keys[] = $invalid_key; |
173
|
|
|
$values[$invalid_key] = $default; |
174
|
|
|
|
175
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys, $default)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param mixed $key |
180
|
|
|
* @dataProvider invalidKeys |
181
|
|
|
*/ |
182
|
|
|
public function testGetMultipleThrowsExceptionWithInvalidKeys($key) |
183
|
|
|
{ |
184
|
|
|
$this->expectException(InvalidArgumentException::class); |
185
|
|
|
|
186
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
187
|
|
|
$psrCache->getMultiple([$key]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param mixed $key |
192
|
|
|
* @dataProvider validKeys |
193
|
|
|
*/ |
194
|
|
View Code Duplication |
public function testGetMultipleAcceptsTraversable($key) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
$values = [ |
197
|
|
|
$key => uniqid('value', true), |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
201
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
202
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with(array_keys($values))->willReturn($values); |
|
|
|
|
203
|
|
|
|
204
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
205
|
|
|
$psrCache->getMultiple(new \ArrayObject(array_keys($values))); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function testGetMultipleThrowsExceptionWhenNotArrayOrTraversable() |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$this->expectException(InvalidArgumentException::class); |
211
|
|
|
|
212
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
213
|
|
|
$psrCache->getMultiple(uniqid('string', true)); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
217
|
|
|
{ |
218
|
|
|
$values = [ |
219
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
220
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
221
|
|
|
]; |
222
|
|
|
|
223
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
224
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
225
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
226
|
|
|
|
227
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
228
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function testSetMultipleThrowsExceptionWhenNotArrayOrTraversable() |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$this->expectException(InvalidArgumentException::class); |
234
|
|
|
|
235
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
236
|
|
|
$psrCache->setMultiple(uniqid('string', true)); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
$keys = [ |
242
|
|
|
uniqid('key1', true), |
243
|
|
|
uniqid('key2', true), |
244
|
|
|
]; |
245
|
|
|
|
246
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
247
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
248
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
249
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
250
|
|
|
|
251
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
252
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$keys = [ |
258
|
|
|
uniqid('key1', true), |
259
|
|
|
uniqid('key2', true), |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
263
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
264
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
265
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
266
|
|
|
|
267
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
268
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$key = uniqid('key', true); |
274
|
|
|
|
275
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
276
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
277
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
278
|
|
|
|
279
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
280
|
|
|
self::assertTrue($psrCache->has($key)); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
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.