-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathCacheTest.php
More file actions
477 lines (415 loc) · 13.9 KB
/
CacheTest.php
File metadata and controls
477 lines (415 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
namespace DetectionTests;
use DateInterval;
use Detection\Cache\Cache;
use Detection\Cache\CacheInvalidArgumentException;
use PHPUnit\Framework\TestCase;
final class CacheTest extends TestCase
{
protected Cache $cache;
protected function setUp(): void
{
$this->cache = new Cache();
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetInvalidCacheKeyThrowsException()
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->get('');
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetInvalidCacheKeyThrowsException()
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->set('', 'a', 100);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetNonExistentReturnsNull()
{
$this->assertNull($this->cache->get('random'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetNonExistentReturnsCustomDefault(): void
{
$this->assertEquals('customDefault', $this->cache->get('random', 'customDefault'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetExpiredItemReturnsDefault(): void
{
$this->cache->set('expiring', 'value', 1);
sleep(2);
$this->assertNull($this->cache->get('expiring'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetExpiredItemReturnsCustomDefault(): void
{
$this->cache->set('expiring', 'value', 1);
sleep(2);
$this->assertEquals('fallback', $this->cache->get('expiring', 'fallback'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetGetBooleanValues()
{
$this->cache->set('isMobile', true, 100);
$this->assertTrue($this->cache->get('isMobile'));
$this->cache->set('isTablet', false, 100);
$this->assertFalse($this->cache->get('isTablet'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetGetZeroTTL()
{
$this->cache->set('isMobile', true, 0);
$this->assertNull($this->cache->get('isMobile'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetGetNegativeTTL()
{
$this->cache->set('isMobile', true, -999);
$this->assertNull($this->cache->get('isMobile'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetZeroTTLWithInvalidKeyThrowsException()
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->set('', true, 0);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetNegativeTTLWithInvalidKeyThrowsException()
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->set('', true, -999);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetValidTtlAsAnIntegerReturnsTheSetValue()
{
$this->cache->set('isMobile', 'someValue', 1000);
$this->assertEquals('someValue', $this->cache->get('isMobile'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetNullTtlReturnsTheSetValue()
{
$this->cache->set('isMobile', 'abc');
$this->assertEquals('abc', $this->cache->get('isMobile'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetWithDateIntervalTtl(): void
{
$this->cache->set('withInterval', 'intervalValue', new DateInterval('PT1H'));
$this->assertEquals('intervalValue', $this->cache->get('withInterval'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testDeletionOfValidRecord()
{
$this->cache->set('isMobile', 'a b c', 100);
$this->assertEquals('a b c', $this->cache->get('isMobile'));
$this->cache->delete('isMobile');
$this->assertNull($this->cache->get('isMobile'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testDeleteInvalidKeyThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->delete('invalid key');
}
/**
* @throws CacheInvalidArgumentException
*/
public function testDeleteNonExistentKeyReturnsTrue(): void
{
$this->assertTrue($this->cache->delete('non_existent'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testClear()
{
$this->cache->set('isMobile', true);
$this->cache->set('isTablet', true);
$this->assertCount(2, $this->cache->getKeys());
$this->cache->clear();
$this->assertCount(0, $this->cache->getKeys());
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetMultiple(): void
{
$this->cache->set('isMobile', true, 100);
$this->cache->set('isTablet', false, 200);
$this->assertEquals(
[
'isMobile' => true,
'isTablet' => false,
'isUnknown' => null,
],
$this->cache->getMultiple(['isMobile', 'isTablet', 'isUnknown'])
);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetMultiple(): void
{
$this->cache->setMultiple(['isA' => true, 'isB' => false], 200);
$this->assertEquals([
'isA' => true,
'isB' => false
], $this->cache->getMultiple(['isA', 'isB']));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testDeleteMultiple(): void
{
$this->cache->setMultiple(['isA' => true, 'isB' => false, 'isC' => true], 300);
$this->cache->deleteMultiple(['isA', 'isB']);
$this->assertEquals([
'isA' => null,
'isB' => null,
'isC' => true
], $this->cache->getMultiple(['isA', 'isB', 'isC']));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testHasReturnsTrueForValidCacheRecord(): void
{
$this->cache->set('isA', 'some value1');
$this->assertTrue($this->cache->has('isA'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testHasReturnsTrueForValidNonNullTtl(): void
{
$this->cache->set('isB', 'some value', 3600);
$this->assertTrue($this->cache->has('isB'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testHasReturnsFalseForExpiredCacheRecord(): void
{
$this->cache->set('isA', 'some value2', 1);
sleep(2);
$this->assertFalse($this->cache->has('isA'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testHasReturnsFalseForNonExistentCacheRecord(): void
{
$this->assertFalse($this->cache->has('non_existent'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testHasThrowsExceptionForInvalidKey(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->has('invalid key');
}
/**
* @throws CacheInvalidArgumentException
*/
public function testGetMultipleWithCustomDefault(): void
{
$this->cache->set('exists', 'value', 100);
$this->assertEquals(
[
'exists' => 'value',
'missing' => 'customDefault',
],
$this->cache->getMultiple(['exists', 'missing'], 'customDefault')
);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testSetMultipleReturnsFalseWhenOneFails(): void
{
// Setting with zero TTL causes set() to return false
$result = $this->cache->setMultiple(['isA' => true, 'isB' => false], 0);
$this->assertFalse($result);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testCheckKeyWithInvalidCharactersThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->get('invalid@key');
}
/**
* @throws CacheInvalidArgumentException
*/
public function testCheckKeyWithSpecialCharsThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->get('key{}[]');
}
/**
* @throws CacheInvalidArgumentException
*/
public function testCheckKeyExceeding64CharsThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$longKey = str_repeat('a', 65);
$this->cache->get($longKey);
}
/**
* @throws CacheInvalidArgumentException
*/
public function testCheckKeyWith64CharsIsValid(): void
{
$validKey = str_repeat('a', 64);
$this->cache->set($validKey, 'value');
$this->assertEquals('value', $this->cache->get($validKey));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testEvictExpiredRemovesExpiredItems(): void
{
$this->cache->set('expiring1', 'value1', 1);
$this->cache->set('expiring2', 'value2', 1);
$this->cache->set('persistent', 'value3', 3600);
$this->assertCount(3, $this->cache->getKeys());
sleep(2);
$evicted = $this->cache->evictExpired();
$this->assertEquals(2, $evicted);
$this->assertCount(1, $this->cache->getKeys());
$this->assertEquals('value3', $this->cache->get('persistent'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testEvictExpiredKeepsNullTtlItems(): void
{
$this->cache->set('noTtl', 'value1');
$this->cache->set('expiring', 'value2', 1);
sleep(2);
$evicted = $this->cache->evictExpired();
$this->assertEquals(1, $evicted);
$this->assertCount(1, $this->cache->getKeys());
$this->assertEquals('value1', $this->cache->get('noTtl'));
}
/**
* @throws CacheInvalidArgumentException
*/
public function testEvictExpiredReturnsZeroWhenNothingToEvict(): void
{
$this->cache->set('valid1', 'value1', 3600);
$this->cache->set('valid2', 'value2');
$evicted = $this->cache->evictExpired();
$this->assertEquals(0, $evicted);
$this->assertCount(2, $this->cache->getKeys());
}
public function testEvictExpiredOnEmptyCache(): void
{
$evicted = $this->cache->evictExpired();
$this->assertEquals(0, $evicted);
}
/**
* Regression test for https://github.com/serbanghita/Mobile-Detect/issues/989
*
* Cache must remain LSP-compatible with PSR-16 v1 (untyped parameters) so it
* loads cleanly in hosts where another plugin has registered an older
* Psr\SimpleCache\CacheInterface. Re-adding scalar type declarations on the
* listed parameters will break that at class load with a fatal error.
*/
public function testPublicMethodParametersAreUntypedForPsr16V1Compatibility(): void
{
// [method => parameter index] pairs that MUST have no type declaration.
$untypedSlots = [
['get', 0], // $key
['set', 0], // $key
['set', 2], // $ttl
['delete', 0], // $key
['has', 0], // $key
['getMultiple', 0], // $keys
['setMultiple', 0], // $values
['setMultiple', 1], // $ttl
['deleteMultiple', 0], // $keys
];
foreach ($untypedSlots as [$method, $index]) {
$param = (new \ReflectionMethod(Cache::class, $method))->getParameters()[$index];
self::assertFalse(
$param->hasType(),
sprintf(
'Cache::%s() parameter $%s must have no type declaration for PSR-16 v1 LSP compatibility, got: %s',
$method,
$param->getName(),
(string) $param->getType()
)
);
}
}
public function testSetWithInvalidTtlTypeThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
$this->cache->set('isMobile', true, 'not-a-valid-ttl');
}
public function testSetMultipleWithNonIterableThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
/** @phpstan-ignore-next-line Intentional: asserts runtime validation of the widened signature. */
$this->cache->setMultiple('not-iterable');
}
public function testGetMultipleWithNonIterableThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
/** @phpstan-ignore-next-line Intentional: asserts runtime validation of the widened signature. */
$this->cache->getMultiple('not-iterable');
}
public function testDeleteMultipleWithNonIterableThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
/** @phpstan-ignore-next-line Intentional: asserts runtime validation of the widened signature. */
$this->cache->deleteMultiple('not-iterable');
}
public function testGetWithNonStringKeyThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
/** @phpstan-ignore-next-line Intentional: asserts runtime validation of the widened signature. */
$this->cache->get(123);
}
public function testSetWithNonStringKeyThrowsException(): void
{
$this->expectException(CacheInvalidArgumentException::class);
/** @phpstan-ignore-next-line Intentional: asserts runtime validation of the widened signature. */
$this->cache->set(['notAKey'], 'value');
}
}