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