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 testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed() |
37
|
|
|
{ |
38
|
|
|
/** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
39
|
|
|
$doctrineCache = $this->createMock(NotMultiPuttableCache::class); |
40
|
|
|
|
41
|
|
|
$this->expectException(CacheException::class); |
42
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() |
46
|
|
|
{ |
47
|
|
|
/** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
48
|
|
|
$doctrineCache = $this->createMock(NotClearableCache::class); |
49
|
|
|
|
50
|
|
|
$this->expectException(CacheException::class); |
51
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed() |
55
|
|
|
{ |
56
|
|
|
/** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
57
|
|
|
$doctrineCache = $this->createMock(NotMultiGettableCache::class); |
58
|
|
|
|
59
|
|
|
$this->expectException(CacheException::class); |
60
|
|
|
new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGetProxiesToDoctrineFetch() |
64
|
|
|
{ |
65
|
|
|
$key = uniqid('key', true); |
66
|
|
|
$value = uniqid('value', true); |
67
|
|
|
|
68
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
69
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
70
|
|
|
$doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
73
|
|
|
self::assertSame($value, $psrCache->get($key)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetWithNotExistingKey() |
77
|
|
|
{ |
78
|
|
|
$key = uniqid('key', true); |
79
|
|
|
$value = uniqid('value', true); |
80
|
|
|
|
81
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
82
|
|
|
$psrCache->set($key, $value); |
83
|
|
|
|
84
|
|
|
$default = uniqid('default', true); |
85
|
|
|
self::assertSame($value, $psrCache->get($key, $default)); |
86
|
|
|
|
87
|
|
|
$anotherKey = uniqid('key', true); |
88
|
|
|
self::assertSame($default, $psrCache->get($anotherKey, $default)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function testGetWithFalseValueStoredInCache() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$key = uniqid('key', true); |
94
|
|
|
|
95
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
96
|
|
|
$psrCache->set($key, false); |
97
|
|
|
|
98
|
|
|
self::assertFalse($psrCache->get($key, uniqid('default', true))); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testSetProxiesToDoctrineSave() |
102
|
|
|
{ |
103
|
|
|
$key = uniqid('key', true); |
104
|
|
|
$value = uniqid('value', true); |
105
|
|
|
$ttl = random_int(1000, 9999); |
106
|
|
|
|
107
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
108
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
109
|
|
|
$doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
112
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testSetWithDateIntervalTTL() |
116
|
|
|
{ |
117
|
|
|
$key = uniqid('key', true); |
118
|
|
|
$value = uniqid('value', true); |
119
|
|
|
$ttl_date = \DateInterval::createFromDateString('1 day'); |
120
|
|
|
|
121
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
122
|
|
|
|
123
|
|
|
// This does not test if ttl is correctly set to 86400 sec. |
124
|
|
|
self::assertTrue($psrCache->set($key, $value, $ttl_date)); |
125
|
|
|
self::assertSame($psrCache->get($key), $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testSetWithNonPositiveTTL() |
129
|
|
|
{ |
130
|
|
|
$key = uniqid('key', true); |
131
|
|
|
$value = uniqid('value', true); |
132
|
|
|
$ttl = random_int(1000, 9999); |
133
|
|
|
|
134
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
135
|
|
|
|
136
|
|
|
$psrCache->set($key, $value, $ttl); |
137
|
|
|
self::assertSame($psrCache->get($key), $value); |
138
|
|
|
|
139
|
|
|
$psrCache->set($key, $value, -1); |
140
|
|
|
self::assertNull($psrCache->get($key), null); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param mixed $ttl |
145
|
|
|
* @dataProvider invalidTTLs |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function testSetWithInvalidTTL($ttl) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$key = uniqid('key', true); |
150
|
|
|
$value = uniqid('value', true); |
151
|
|
|
|
152
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
153
|
|
|
|
154
|
|
|
$this->expectException(InvalidArgumentException::class); |
155
|
|
|
$psrCache->set($key, $value, $ttl); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
View Code Duplication |
public function testDeleteProxiesToDoctrineDelete() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$key = uniqid('key', true); |
161
|
|
|
|
162
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
163
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
164
|
|
|
$doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
167
|
|
|
self::assertTrue($psrCache->delete($key)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testClearProxiesToDeleteAll() |
171
|
|
|
{ |
172
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
173
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
174
|
|
|
$doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true); |
|
|
|
|
175
|
|
|
|
176
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
177
|
|
|
self::assertTrue($psrCache->clear()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testGetMultipleProxiesToFetchMultiple() |
181
|
|
|
{ |
182
|
|
|
$values = [ |
183
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
184
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
185
|
|
|
]; |
186
|
|
|
$keys = array_keys($values); |
187
|
|
|
|
188
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
189
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
190
|
|
|
$doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
193
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetMultipleWithPartialKeys() |
197
|
|
|
{ |
198
|
|
|
$values = [ |
199
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
200
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
201
|
|
|
]; |
202
|
|
|
$keys = array_keys($values); |
203
|
|
|
|
204
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
205
|
|
|
$psrCache->setMultiple($values); |
|
|
|
|
206
|
|
|
|
207
|
|
|
$default = uniqid('default', true); |
208
|
|
|
$invalid_key = uniqid('key3', true); |
209
|
|
|
$keys[] = $invalid_key; |
210
|
|
|
$values[$invalid_key] = $default; |
211
|
|
|
|
212
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys, $default)); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testSetMultipleProxiesToSaveMultiple() |
216
|
|
|
{ |
217
|
|
|
$values = [ |
218
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
219
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
223
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
224
|
|
|
$doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true); |
|
|
|
|
225
|
|
|
|
226
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
227
|
|
|
self::assertTrue($psrCache->setMultiple($values)); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testSetMultipleWithDateIntervalTTL() |
231
|
|
|
{ |
232
|
|
|
$values = [ |
233
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
234
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
235
|
|
|
]; |
236
|
|
|
$keys = array_keys($values); |
237
|
|
|
$ttl_date = \DateInterval::createFromDateString('1 day'); |
238
|
|
|
|
239
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
240
|
|
|
|
241
|
|
|
// This does not test if ttl is correctly set to 86400 sec. |
242
|
|
|
self::assertTrue($psrCache->setMultiple($values, $ttl_date)); |
|
|
|
|
243
|
|
|
self::assertSame($values, $psrCache->getMultiple($keys)); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testSetMultipleWithNonPositiveTTL() |
247
|
|
|
{ |
248
|
|
|
$values = [ |
249
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
250
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
251
|
|
|
]; |
252
|
|
|
$keys = array_keys($values); |
253
|
|
|
|
254
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
255
|
|
|
$psrCache->setMultiple($values); |
|
|
|
|
256
|
|
|
|
257
|
|
|
$volatile = [$keys[0] => uniqid('value3', true)]; |
258
|
|
|
$psrCache->setMultiple($volatile, -1); |
|
|
|
|
259
|
|
|
|
260
|
|
|
self::assertNull($psrCache->get($keys[0])); |
261
|
|
|
self::assertNotNull($psrCache->get($keys[1])); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param mixed $ttl |
266
|
|
|
* @dataProvider invalidTTLs |
267
|
|
|
*/ |
268
|
|
|
public function testSetMultipleWithInvalidTTL($ttl) |
269
|
|
|
{ |
270
|
|
|
$values = [ |
271
|
|
|
uniqid('key1', true) => uniqid('value1', true), |
272
|
|
|
uniqid('key2', true) => uniqid('value2', true), |
273
|
|
|
]; |
274
|
|
|
|
275
|
|
|
$psrCache = new SimpleCacheAdapter(new ArrayCache()); |
276
|
|
|
|
277
|
|
|
$this->expectException(InvalidArgumentException::class); |
278
|
|
|
$psrCache->setMultiple($values, $ttl); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
View Code Duplication |
public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
$keys = [ |
284
|
|
|
uniqid('key1', true), |
285
|
|
|
uniqid('key2', true), |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
289
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
290
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true); |
|
|
|
|
291
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
292
|
|
|
|
293
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
294
|
|
|
self::assertTrue($psrCache->deleteMultiple($keys)); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
View Code Duplication |
public function testDeleteMultipleReturnsFalseWhenOneDeleteFails() |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$keys = [ |
300
|
|
|
uniqid('key1', true), |
301
|
|
|
uniqid('key2', true), |
302
|
|
|
]; |
303
|
|
|
|
304
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
305
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
306
|
|
|
$doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false); |
|
|
|
|
307
|
|
|
$doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true); |
308
|
|
|
|
309
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
310
|
|
|
self::assertFalse($psrCache->deleteMultiple($keys)); |
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
View Code Duplication |
public function testHasProxiesToDoctrineContains() |
|
|
|
|
314
|
|
|
{ |
315
|
|
|
$key = uniqid('key', true); |
316
|
|
|
|
317
|
|
|
/** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */ |
318
|
|
|
$doctrineCache = $this->createMock(FullyImplementedCache::class); |
319
|
|
|
$doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true); |
|
|
|
|
320
|
|
|
|
321
|
|
|
$psrCache = new SimpleCacheAdapter($doctrineCache); |
|
|
|
|
322
|
|
|
self::assertTrue($psrCache->has($key)); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
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.