1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Cache; |
4
|
|
|
|
5
|
|
|
use Ds\Cache\Cache; |
6
|
|
|
use Ds\Cache\CacheStorageInterface; |
7
|
|
|
use Ds\Cache\NullStorage; |
8
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
9
|
|
|
use Tests\Cache\Mock\IteratorMock; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CacheTest |
13
|
|
|
* @package Tests\Cache |
14
|
|
|
*/ |
15
|
|
|
class CacheTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Cache |
19
|
|
|
*/ |
20
|
|
|
public $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CacheStorageInterface |
24
|
|
|
*/ |
25
|
|
|
public $storageMock; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->storageMock = $this->getMockBuilder(CacheStorageInterface::class)->getMock(); |
33
|
|
|
$this->cache = new Cache($this->storageMock); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Test that CacheStorageInterface::set() invalid key exception |
38
|
|
|
*/ |
39
|
|
|
public function testSetInvalidKey() |
40
|
|
|
{ |
41
|
|
|
$key = 21021000; |
42
|
|
|
$this->expectException(InvalidArgumentException::class); |
43
|
|
|
$this->cache->set($key,'value'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test that CacheStorageInterface::set() is called |
48
|
|
|
*/ |
49
|
|
|
public function testSet() |
50
|
|
|
{ |
51
|
|
|
$key = 'key'; |
52
|
|
|
$value = 'value'; |
53
|
|
|
$expires = 10; |
54
|
|
|
|
55
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
56
|
|
|
->method('set') |
57
|
|
|
->with( |
58
|
|
|
$this->equalTo($key), |
59
|
|
|
$this->equalTo($value), |
60
|
|
|
$this->equalTo($expires) |
61
|
|
|
); |
62
|
|
|
$this->cache->set($key, $value, $expires); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test that CacheStorageInterface::set() is called |
67
|
|
|
*/ |
68
|
|
|
public function testSetDateInterval() |
69
|
|
|
{ |
70
|
|
|
$key = 'key'; |
71
|
|
|
$value = 'value'; |
72
|
|
|
$expires = new \DateInterval('PT1H'); |
73
|
|
|
$oneHour = 60 * 60 * 1; |
74
|
|
|
|
75
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
76
|
|
|
->method('set') |
77
|
|
|
->with( |
78
|
|
|
$this->equalTo($key), |
79
|
|
|
$this->equalTo($value), |
80
|
|
|
$this->equalTo($oneHour) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->cache->set($key, $value, $expires); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test that CacheStorageInterface::has() invalid key exception |
88
|
|
|
*/ |
89
|
|
|
public function testHasInvalidKey() |
90
|
|
|
{ |
91
|
|
|
$key = 21021000; |
92
|
|
|
$this->expectException(InvalidArgumentException::class); |
93
|
|
|
$this->cache->has($key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test that CacheStorageInterface::has() is called and returns true on finding key |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function testHasFoundWithKey() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$key = 'key'; |
103
|
|
|
$expected = true; |
104
|
|
|
|
105
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
106
|
|
|
->method('has') |
107
|
|
|
->with( |
108
|
|
|
$this->equalTo($key) |
109
|
|
|
) |
110
|
|
|
->willReturn($expected); |
111
|
|
|
|
112
|
|
|
$actual = $this->cache->has($key); |
113
|
|
|
$this->assertEquals($expected, $actual); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test that CacheStorageInterface::has() is called and returns false on failing to find key |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testHasFoundNoKey() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$key = 'key'; |
122
|
|
|
$expected = false; |
123
|
|
|
|
124
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
125
|
|
|
->method('has') |
126
|
|
|
->with( |
127
|
|
|
$this->equalTo($key) |
128
|
|
|
) |
129
|
|
|
->willReturn($expected); |
130
|
|
|
|
131
|
|
|
$actual = $this->cache->has($key); |
132
|
|
|
$this->assertEquals($expected, $actual); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test that CacheStorageInterface::has() is called and returns false on failing to find key |
138
|
|
|
*/ |
139
|
|
|
public function testGetInvalidKey() |
140
|
|
|
{ |
141
|
|
|
$key = 21021000; |
142
|
|
|
$this->expectException(InvalidArgumentException::class); |
143
|
|
|
$this->cache->get($key); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Test that CacheStorageInterface::get() returns values when key is present. |
148
|
|
|
*/ |
149
|
|
|
public function testGetFoundValue() |
150
|
|
|
{ |
151
|
|
|
$key = 'key'; |
152
|
|
|
$expected = 'cacheValue'; |
153
|
|
|
$default = 'default value'; |
154
|
|
|
|
155
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
156
|
|
|
->method('has') |
157
|
|
|
->with( |
158
|
|
|
$this->equalTo($key) |
159
|
|
|
) |
160
|
|
|
->willReturn(true); |
161
|
|
|
|
162
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
163
|
|
|
->method('get') |
164
|
|
|
->with( |
165
|
|
|
$this->equalTo($key) |
166
|
|
|
) |
167
|
|
|
->willReturn($expected); |
168
|
|
|
|
169
|
|
|
$actual = $this->cache->get($key, $default); |
170
|
|
|
$this->assertEquals($expected, $actual); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Test that CacheStorageInterface::get() doesn't return values and default is returned. |
175
|
|
|
*/ |
176
|
|
|
public function testGetDefaultValue() |
177
|
|
|
{ |
178
|
|
|
$key = 'key'; |
179
|
|
|
$default = 'default value'; |
180
|
|
|
|
181
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
182
|
|
|
->method('has') |
183
|
|
|
->with( |
184
|
|
|
$this->equalTo($key) |
185
|
|
|
) |
186
|
|
|
->willReturn(false); |
187
|
|
|
|
188
|
|
|
$actual = $this->cache->get($key, $default); |
189
|
|
|
$this->assertEquals($default, $actual); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Test that CacheStorageInterface::delete() is called |
194
|
|
|
*/ |
195
|
|
|
public function testDelete() |
196
|
|
|
{ |
197
|
|
|
$key = 'key'; |
198
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
199
|
|
|
->method('delete') |
200
|
|
|
->with( |
201
|
|
|
$this->equalTo($key) |
202
|
|
|
); |
203
|
|
|
$this->cache->delete($key); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* |
208
|
|
|
*/ |
209
|
|
|
public function testGetMultiple(){ |
210
|
|
|
|
211
|
|
|
$expected = [ |
212
|
|
|
'foo' => 'fooValue', |
213
|
|
|
'bar' => 'barValue', |
214
|
|
|
'baz' => 'bazValue' |
215
|
|
|
]; |
216
|
|
|
|
217
|
|
|
$i = 0; |
218
|
|
|
|
219
|
|
|
foreach ($expected as $key => $value){ |
220
|
|
|
|
221
|
|
|
$this->storageMock->expects($this->at($i)) |
|
|
|
|
222
|
|
|
->method('get') |
223
|
|
|
->with( |
224
|
|
|
$this->equalTo($key) |
225
|
|
|
) |
226
|
|
|
->willReturn($value); |
227
|
|
|
$i++; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$actual = $this->cache->getMultiple(\array_keys($expected)); |
|
|
|
|
231
|
|
|
|
232
|
|
|
$this->assertEquals($expected, $actual); |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* |
238
|
|
|
*/ |
239
|
|
|
public function testSetMultiple(){ |
240
|
|
|
|
241
|
|
|
$keys = [ |
242
|
|
|
'foo' => 'fooValue', |
243
|
|
|
'bar' => 'barValue', |
244
|
|
|
'baz' => 'bazValue' |
245
|
|
|
]; |
246
|
|
|
|
247
|
|
|
$addStatus = true; |
248
|
|
|
$expected = true; |
249
|
|
|
|
250
|
|
|
$i = 0; |
251
|
|
|
$expires = 60 * 60; |
252
|
|
|
|
253
|
|
|
foreach ($keys as $key => $value){ |
254
|
|
|
$this->storageMock->expects($this->at($i)) |
|
|
|
|
255
|
|
|
->method('set') |
256
|
|
|
->with( |
257
|
|
|
$this->equalTo($key), |
258
|
|
|
$this->equalTo($value), |
259
|
|
|
$this->equalTo($expires) |
260
|
|
|
) |
261
|
|
|
->willReturn($addStatus); |
262
|
|
|
$i++; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$actual = $this->cache->setMultiple($keys,$expires); |
|
|
|
|
266
|
|
|
$this->assertEquals($expected, $actual); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* |
271
|
|
|
*/ |
272
|
|
|
public function testSetMultipleWithFailure(){ |
273
|
|
|
|
274
|
|
|
$keys = [ |
275
|
|
|
'foo' => 'fooValue', |
276
|
|
|
'bar' => 'barValue', |
277
|
|
|
'baz' => 'bazValue' |
278
|
|
|
]; |
279
|
|
|
|
280
|
|
|
$addStatus = true; |
281
|
|
|
$expected = false; |
282
|
|
|
|
283
|
|
|
$i = 0; |
284
|
|
|
$expires = 60 * 60; |
285
|
|
|
|
286
|
|
|
foreach ($keys as $key => $value){ |
287
|
|
|
|
288
|
|
|
$status = $addStatus; |
289
|
|
|
|
290
|
|
|
if ($i === 1){ |
291
|
|
|
$status = false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$this->storageMock->expects($this->at($i)) |
|
|
|
|
295
|
|
|
->method('set') |
296
|
|
|
->with( |
297
|
|
|
$this->equalTo($key), |
298
|
|
|
$this->equalTo($value), |
299
|
|
|
$this->equalTo($expires) |
300
|
|
|
) |
301
|
|
|
->willReturn($status); |
302
|
|
|
$i++; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$actual = $this->cache->setMultiple($keys,$expires); |
|
|
|
|
306
|
|
|
$this->assertEquals($expected, $actual); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* |
311
|
|
|
*/ |
312
|
|
|
public function testDeleteMultiple(){ |
313
|
|
|
|
314
|
|
|
$keys = ['foo','bar','baz']; |
315
|
|
|
$deleteStatus = true; |
316
|
|
|
$expected = true; |
317
|
|
|
|
318
|
|
|
foreach ($keys as $i => $key){ |
319
|
|
|
$this->storageMock->expects($this->at($i)) |
|
|
|
|
320
|
|
|
->method('delete') |
321
|
|
|
->with( |
322
|
|
|
$this->equalTo($key) |
323
|
|
|
) |
324
|
|
|
->willReturn($deleteStatus); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$actual = $this->cache->deleteMultiple($keys); |
|
|
|
|
328
|
|
|
$this->assertEquals($expected, $actual); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* |
333
|
|
|
*/ |
334
|
|
|
public function testDeleteMultipleFailure(){ |
335
|
|
|
|
336
|
|
|
$keys = ['foo','bar','baz']; |
337
|
|
|
$expected = false; |
338
|
|
|
$deleteStatus = true; |
339
|
|
|
|
340
|
|
|
foreach ($keys as $i => $key){ |
341
|
|
|
|
342
|
|
|
if ($i === 1){ |
343
|
|
|
$deleteStatus = false; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$this->storageMock->expects($this->at($i)) |
|
|
|
|
347
|
|
|
->method('delete') |
348
|
|
|
->with( |
349
|
|
|
$this->equalTo($key) |
350
|
|
|
) |
351
|
|
|
->willReturn($deleteStatus); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$actual = $this->cache->deleteMultiple($keys); |
|
|
|
|
355
|
|
|
$this->assertEquals($expected, $actual); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Test that storage clear is called. |
360
|
|
|
*/ |
361
|
|
|
public function testclear(){ |
362
|
|
|
$this->storageMock->expects($this->once()) |
|
|
|
|
363
|
|
|
->method('clear'); |
364
|
|
|
$this->cache->clear(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Test that array has keys. |
369
|
|
|
*/ |
370
|
|
View Code Duplication |
public function testArrayHasKeys(){ |
|
|
|
|
371
|
|
|
$data = ['a' => 'foo', 'b' => 'bar']; |
372
|
|
|
|
373
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
374
|
|
|
$method = $reflector->getMethod('_hasKeys'); |
375
|
|
|
$method->setAccessible(true); |
376
|
|
|
$actual = $method->invokeArgs($this->cache, [$data]); |
377
|
|
|
|
378
|
|
|
$this->assertEquals(true, $actual); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Test exception thrown when array is missing keys. |
383
|
|
|
*/ |
384
|
|
View Code Duplication |
public function testArrayHasNoKeys(){ |
|
|
|
|
385
|
|
|
$this->expectException(InvalidArgumentException::class); |
386
|
|
|
$data = ['foo','bar']; |
387
|
|
|
$reflector = new \ReflectionClass(Cache::class); |
388
|
|
|
$method = $reflector->getMethod('_hasKeys'); |
389
|
|
|
$method->setAccessible(true); |
390
|
|
|
$method->invokeArgs($this->cache, [$data]); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Test array contains a failure. |
395
|
|
|
*/ |
396
|
|
View Code Duplication |
public function testHasFailure(){ |
|
|
|
|
397
|
|
|
$results = [true,true,true,false,true]; |
398
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
399
|
|
|
$method = $reflector->getMethod('_hasFailure'); |
400
|
|
|
$method->setAccessible(true); |
401
|
|
|
$actual = $method->invokeArgs($this->cache, [$results]); |
402
|
|
|
$expected = true; |
403
|
|
|
$this->assertEquals($expected, $actual); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* |
408
|
|
|
*/ |
409
|
|
View Code Duplication |
public function testHasNoFailure(){ |
|
|
|
|
410
|
|
|
$results = [true,true,true,true,true]; |
411
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
412
|
|
|
$method = $reflector->getMethod('_hasFailure'); |
413
|
|
|
$method->setAccessible(true); |
414
|
|
|
$actual = $method->invokeArgs($this->cache, [$results]); |
415
|
|
|
$expected = false; |
416
|
|
|
$this->assertEquals($expected, $actual); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Test that array is Traversable |
421
|
|
|
*/ |
422
|
|
View Code Duplication |
public function testIsTraversable(){ |
|
|
|
|
423
|
|
|
$data = ['a','b','c','d']; |
424
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
425
|
|
|
$method = $reflector->getMethod('_isTraversable'); |
426
|
|
|
$method->setAccessible(true); |
427
|
|
|
$actual = $method->invokeArgs($this->cache, [$data]); |
428
|
|
|
$expected = true; |
429
|
|
|
$this->assertEquals($expected, $actual); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Test that string is not Traversable |
434
|
|
|
*/ |
435
|
|
View Code Duplication |
public function testIsNotTraversable(){ |
|
|
|
|
436
|
|
|
$this->expectException(InvalidArgumentException::class); |
437
|
|
|
$data = 'some-random-string'; |
438
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
439
|
|
|
$method = $reflector->getMethod('_isTraversable'); |
440
|
|
|
$method->setAccessible(true); |
441
|
|
|
$method->invokeArgs($this->cache, [$data]); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Test that instance of Iterator is traversable |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function testIsTraversableIterator(){ |
|
|
|
|
448
|
|
|
$iterator = new IteratorMock(); |
449
|
|
|
$reflector = new \ReflectionClass('\Ds\Cache\Cache'); |
450
|
|
|
$method = $reflector->getMethod('_isTraversable'); |
451
|
|
|
$method->setAccessible(true); |
452
|
|
|
$actual = $method->invokeArgs($this->cache, [$iterator]); |
453
|
|
|
$expected = true; |
454
|
|
|
$this->assertEquals($expected, $actual); |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.