|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace RoaveTest\DoctrineSimpleCache; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
7
|
|
|
use Roave\DoctrineSimpleCache\Exception\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 invalidTTLs() : array |
|
21
|
|
|
{ |
|
22
|
|
|
return [ |
|
23
|
|
|
[''], |
|
24
|
|
|
[true], |
|
25
|
|
|
[false], |
|
26
|
|
|
['abc'], |
|
27
|
|
|
[2.5], |
|
28
|
|
|
[' 1'], // can be casted to a int |
|
29
|
|
|
['12foo'], // can be casted to a int |
|
30
|
|
|
['025'], // can be interpreted as hex |
|
31
|
|
|
[new \stdClass()], |
|
32
|
|
|
[['array']], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function validKeys() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
['AbC19_.'], |
|
40
|
|
|
['1234567890123456789012345678901234567890123456789012345678901234'], |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function invalidKeys() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
[''], |
|
48
|
|
|
[true], |
|
49
|
|
|
[false], |
|
50
|
|
|
[null], |
|
51
|
|
|
[2], |
|
52
|
|
|
[2.5], |
|
53
|
|
|
['{str'], |
|
54
|
|
|
['rand{'], |
|
55
|
|
|
['rand{str'], |
|
56
|
|
|
['rand}str'], |
|
57
|
|
|
['rand(str'], |
|
58
|
|
|
['rand)str'], |
|
59
|
|
|
['rand/str'], |
|
60
|
|
|
['rand\\str'], |
|
61
|
|
|
['rand@str'], |
|
62
|
|
|
['rand:str'], |
|
63
|
|
|
[new \stdClass()], |
|
64
|
|
|
[['array']], |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
71
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
|
72
|
|
|
|
|
73
|
|
|
$this->expectException(CacheException::class); |
|
74
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
80
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
|
81
|
|
|
|
|
82
|
|
|
$this->expectException(CacheException::class); |
|
83
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
|
87
|
|
|
{ |
|
88
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
89
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
|
90
|
|
|
|
|
91
|
|
|
$this->expectException(CacheException::class); |
|
92
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function testGetProxiesToDoctrineFetch() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$key = uniqid('key', true); |
|
98
|
|
|
$value = uniqid('value', true); |
|
99
|
|
|
|
|
100
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
101
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
102
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
105
|
|
|
self::assertSame($value, $psrCache->get($key)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testGetWithNotExistingKey() |
|
109
|
|
|
{ |
|
110
|
|
|
$key = uniqid('key', true); |
|
111
|
|
|
$value = uniqid('value', true); |
|
112
|
|
|
|
|
113
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
114
|
|
|
$psrCache->set($key, $value); |
|
115
|
|
|
|
|
116
|
|
|
$default = uniqid('default', true); |
|
117
|
|
|
self::assertSame($value, $psrCache->get($key, $default)); |
|
118
|
|
|
|
|
119
|
|
|
$anotherKey = uniqid('key', true); |
|
120
|
|
|
self::assertSame($default, $psrCache->get($anotherKey, $default)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testSetProxiesToDoctrineSave() |
|
124
|
|
|
{ |
|
125
|
|
|
$key = uniqid('key', true); |
|
126
|
|
|
$value = uniqid('value', true); |
|
127
|
|
|
$ttl = random_int(1000, 9999); |
|
128
|
|
|
|
|
129
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
130
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
131
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
134
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function testSetWithDateIntervalTTL() |
|
138
|
|
|
{ |
|
139
|
|
|
$key = uniqid('key', true); |
|
140
|
|
|
$value = uniqid('value', true); |
|
141
|
|
|
$ttl_date = \DateInterval::createFromDateString('1 day'); |
|
142
|
|
|
|
|
143
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
144
|
|
|
|
|
145
|
|
|
// This does not test if ttl is correctly set to 86400 sec. |
|
146
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl_date)); |
|
147
|
|
|
self::assertSame($psrCache->get($key), $value); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testSetWithNonPositiveTTL() |
|
151
|
|
|
{ |
|
152
|
|
|
$key = uniqid('key', true); |
|
153
|
|
|
$value = uniqid('value', true); |
|
154
|
|
|
$ttl = random_int(1000, 9999); |
|
155
|
|
|
|
|
156
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
157
|
|
|
|
|
158
|
|
|
$psrCache->set($key, $value, $ttl); |
|
159
|
|
|
self::assertSame($psrCache->get($key), $value); |
|
160
|
|
|
|
|
161
|
|
|
$psrCache->set($key, $value, -1); |
|
162
|
|
|
self::assertNull($psrCache->get($key), null); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param mixed $ttl |
|
167
|
|
|
* @dataProvider invalidTTLs |
|
168
|
|
|
*/ |
|
169
|
|
|
public function testSetWithInvalidTTL($ttl) |
|
170
|
|
|
{ |
|
171
|
|
|
$key = uniqid('key', true); |
|
172
|
|
|
$value = uniqid('value', true); |
|
173
|
|
|
|
|
174
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
175
|
|
|
|
|
176
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
177
|
|
|
$psrCache->set($key, $value, $ttl); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
|
|
181
|
|
|
{ |
|
182
|
|
|
$key = uniqid('key', true); |
|
183
|
|
|
|
|
184
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
185
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
186
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
189
|
|
|
self::assertTrue($psrCache->delete($key)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testClearProxiesToDeleteAll() |
|
193
|
|
|
{ |
|
194
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
195
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
196
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
199
|
|
|
self::assertTrue($psrCache->clear()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
|
203
|
|
|
{ |
|
204
|
|
|
$values = [ |
|
205
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
206
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
207
|
|
|
]; |
|
208
|
|
|
$keys = array_keys($values); |
|
209
|
|
|
|
|
210
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
211
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
212
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
215
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function testGetMultipleWithPartialKeys() |
|
219
|
|
|
{ |
|
220
|
|
|
$values = [ |
|
221
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
222
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
223
|
|
|
]; |
|
224
|
|
|
$keys = array_keys($values); |
|
225
|
|
|
|
|
226
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
227
|
|
|
$psrCache->setMultiple($values); |
|
228
|
|
|
|
|
229
|
|
|
$default = uniqid('default', true); |
|
230
|
|
|
$invalid_key = uniqid('key3', true); |
|
231
|
|
|
$keys[] = $invalid_key; |
|
232
|
|
|
$values[$invalid_key] = $default; |
|
233
|
|
|
|
|
234
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys, $default)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param mixed $key |
|
239
|
|
|
* @dataProvider invalidKeys |
|
240
|
|
|
*/ |
|
241
|
|
|
public function testGetMultipleThrowsExceptionWithInvalidKeys($key) |
|
242
|
|
|
{ |
|
243
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
244
|
|
|
|
|
245
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
246
|
|
|
$psrCache->getMultiple([$key]); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param mixed $key |
|
251
|
|
|
* @dataProvider validKeys |
|
252
|
|
|
*/ |
|
253
|
|
View Code Duplication |
public function testGetMultipleAcceptsTraversable($key) |
|
|
|
|
|
|
254
|
|
|
{ |
|
255
|
|
|
$values = [ |
|
256
|
|
|
$key => uniqid('value', true), |
|
257
|
|
|
]; |
|
258
|
|
|
|
|
259
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
260
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
261
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with(array_keys($values))->willReturn($values); |
|
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
264
|
|
|
$psrCache->getMultiple(new \ArrayObject(array_keys($values))); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
View Code Duplication |
public function testGetMultipleThrowsExceptionWhenNotArrayOrTraversable() |
|
|
|
|
|
|
268
|
|
|
{ |
|
269
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
270
|
|
|
|
|
271
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
272
|
|
|
$psrCache->getMultiple(uniqid('string', true)); |
|
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
|
276
|
|
|
{ |
|
277
|
|
|
$values = [ |
|
278
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
279
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
280
|
|
|
]; |
|
281
|
|
|
|
|
282
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
283
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
284
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
287
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function testSetMultipleWithDateIntervalTTL() |
|
291
|
|
|
{ |
|
292
|
|
|
$values = [ |
|
293
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
294
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
295
|
|
|
]; |
|
296
|
|
|
$keys = array_keys($values); |
|
297
|
|
|
$ttl_date = \DateInterval::createFromDateString('1 day'); |
|
298
|
|
|
|
|
299
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
300
|
|
|
|
|
301
|
|
|
// This does not test if ttl is correctly set to 86400 sec. |
|
302
|
|
|
self::assertTrue($psrCache->setMultiple($values, $ttl_date)); |
|
303
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
public function testSetMultipleWithNonPositiveTTL() |
|
307
|
|
|
{ |
|
308
|
|
|
$values = [ |
|
309
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
310
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
311
|
|
|
]; |
|
312
|
|
|
$keys = array_keys($values); |
|
313
|
|
|
|
|
314
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
315
|
|
|
$psrCache->setMultiple($values); |
|
316
|
|
|
|
|
317
|
|
|
$volatile = [$keys[0] => uniqid('value3', true)]; |
|
318
|
|
|
$psrCache->setMultiple($volatile, -1); |
|
319
|
|
|
|
|
320
|
|
|
self::assertNull($psrCache->get($keys[0])); |
|
321
|
|
|
self::assertNotNull($psrCache->get($keys[1])); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param mixed $ttl |
|
326
|
|
|
* @dataProvider invalidTTLs |
|
327
|
|
|
*/ |
|
328
|
|
|
public function testSetMultipleWithInvalidTTL($ttl) |
|
329
|
|
|
{ |
|
330
|
|
|
$values = [ |
|
331
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
|
332
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
|
333
|
|
|
]; |
|
334
|
|
|
|
|
335
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
336
|
|
|
|
|
337
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
338
|
|
|
$psrCache->setMultiple($values, $ttl); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
View Code Duplication |
public function testSetMultipleThrowsExceptionWhenNotArrayOrTraversable() |
|
|
|
|
|
|
342
|
|
|
{ |
|
343
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
344
|
|
|
|
|
345
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
|
346
|
|
|
$psrCache->setMultiple(uniqid('string', true)); |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
$keys = [ |
|
352
|
|
|
uniqid('key1', true), |
|
353
|
|
|
uniqid('key2', true), |
|
354
|
|
|
]; |
|
355
|
|
|
|
|
356
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
357
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
358
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
|
|
359
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
|
360
|
|
|
|
|
361
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
362
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
|
|
366
|
|
|
{ |
|
367
|
|
|
$keys = [ |
|
368
|
|
|
uniqid('key1', true), |
|
369
|
|
|
uniqid('key2', true), |
|
370
|
|
|
]; |
|
371
|
|
|
|
|
372
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
373
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
374
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
|
|
375
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
|
376
|
|
|
|
|
377
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
378
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
|
|
382
|
|
|
{ |
|
383
|
|
|
$key = uniqid('key', true); |
|
384
|
|
|
|
|
385
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
|
386
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
|
387
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
|
|
388
|
|
|
|
|
389
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
|
|
390
|
|
|
self::assertTrue($psrCache->has($key)); |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|
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.