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