1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Mock for the time() function for caching classes. |
7
|
|
|
* @return int |
8
|
|
|
*/ |
9
|
|
|
function time(): int |
10
|
|
|
{ |
11
|
|
|
return \Yiisoft\Cache\Tests\BaseTest::$time ?: \time(); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
namespace Yiisoft\Cache\Tests; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
use DateInterval; |
18
|
|
|
use Psr\SimpleCache\CacheInterface; |
19
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
20
|
|
|
use ReflectionException; |
21
|
|
|
use Yiisoft\Cache\BaseCache; |
22
|
|
|
use Yiisoft\Cache\Cache; |
23
|
|
|
|
24
|
|
|
abstract class BaseTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var int virtual time to be returned by mocked time() function. |
28
|
|
|
* Null means normal time() behavior. |
29
|
|
|
*/ |
30
|
|
|
public static $time; |
31
|
|
|
|
32
|
|
|
protected function tearDown(): void |
33
|
|
|
{ |
34
|
|
|
static::$time = null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
abstract protected function createCacheInstance(): CacheInterface; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider dataProvider |
41
|
|
|
* @param $key |
42
|
|
|
* @param $value |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
|
|
public function testSet($key, $value): void |
46
|
|
|
{ |
47
|
|
|
$cache = $this->createCacheInstance(); |
48
|
|
|
$cache->clear(); |
49
|
|
|
|
50
|
|
|
for ($i = 0; $i < 2; $i++) { |
51
|
|
|
$this->assertTrue($cache->set($key, $value)); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider dataProvider |
57
|
|
|
* @param $key |
58
|
|
|
* @param $value |
59
|
|
|
* @throws InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
public function testGet($key, $value): void |
62
|
|
|
{ |
63
|
|
|
$cache = $this->createCacheInstance(); |
64
|
|
|
$cache->clear(); |
65
|
|
|
|
66
|
|
|
$cache->set($key, $value); |
67
|
|
|
$valueFromCache = $cache->get($key, 'default'); |
68
|
|
|
|
69
|
|
|
$this->assertSameExceptObject($value, $valueFromCache); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider dataProvider |
74
|
|
|
* @param $key |
75
|
|
|
* @param $value |
76
|
|
|
* @throws InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
|
public function testValueInCacheCannotBeChanged($key, $value): void |
79
|
|
|
{ |
80
|
|
|
$cache = $this->createCacheInstance(); |
81
|
|
|
$cache->clear(); |
82
|
|
|
|
83
|
|
|
$cache->set($key, $value); |
84
|
|
|
$valueFromCache = $cache->get($key, 'default'); |
85
|
|
|
|
86
|
|
|
$this->assertSameExceptObject($value, $valueFromCache); |
87
|
|
|
|
88
|
|
|
if (is_object($value)) { |
89
|
|
|
$originalValue = clone $value; |
90
|
|
|
$valueFromCache->test_field = 'changed'; |
91
|
|
|
$value->test_field = 'changed'; |
92
|
|
|
$valueFromCacheNew = $cache->get($key, 'default'); |
93
|
|
|
$this->assertSameExceptObject($originalValue, $valueFromCacheNew); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @dataProvider dataProvider |
99
|
|
|
* @param $key |
100
|
|
|
* @param $value |
101
|
|
|
* @throws InvalidArgumentException |
102
|
|
|
*/ |
103
|
|
|
public function testHas($key, $value): void |
104
|
|
|
{ |
105
|
|
|
$cache = $this->createCacheInstance(); |
106
|
|
|
$cache->clear(); |
107
|
|
|
|
108
|
|
|
$cache->set($key, $value); |
109
|
|
|
|
110
|
|
|
$this->assertTrue($cache->has($key)); |
111
|
|
|
// check whether exists affects the value |
112
|
|
|
$this->assertSameExceptObject($value, $cache->get($key)); |
113
|
|
|
|
114
|
|
|
$this->assertTrue($cache->has($key)); |
115
|
|
|
$this->assertFalse($cache->has('not_exists')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetNonExistent(): void |
119
|
|
|
{ |
120
|
|
|
$cache = $this->createCacheInstance(); |
121
|
|
|
$cache->clear(); |
122
|
|
|
|
123
|
|
|
$this->assertNull($cache->get('non_existent_key')); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// TODO |
127
|
|
|
/*public function testExpire(): void |
128
|
|
|
{ |
129
|
|
|
$cache = $this->createCacheInstance(); |
130
|
|
|
$cache->clear(); |
131
|
|
|
|
132
|
|
|
$this->assertTrue($cache->set('expire_test', 'expire_test', 2)); |
133
|
|
|
usleep(500000); |
134
|
|
|
$this->assertSameExceptObject('expire_test', $cache->get('expire_test')); |
135
|
|
|
usleep(2500000); |
136
|
|
|
$this->assertNull($cache->get('expire_test')); |
137
|
|
|
}*/ |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @dataProvider dataProvider |
141
|
|
|
* @param $key |
142
|
|
|
* @param $value |
143
|
|
|
* @throws InvalidArgumentException |
144
|
|
|
*/ |
145
|
|
|
public function testDelete($key, $value): void |
146
|
|
|
{ |
147
|
|
|
$cache = $this->createCacheInstance(); |
148
|
|
|
$cache->clear(); |
149
|
|
|
|
150
|
|
|
$cache->set($key, $value); |
151
|
|
|
|
152
|
|
|
$this->assertSameExceptObject($value, $cache->get($key)); |
153
|
|
|
$this->assertTrue($cache->delete($key)); |
154
|
|
|
$this->assertNull($cache->get($key)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @dataProvider dataProvider |
159
|
|
|
* @param $key |
160
|
|
|
* @param $value |
161
|
|
|
* @throws InvalidArgumentException |
162
|
|
|
*/ |
163
|
|
|
public function testClear($key, $value): void |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$cache = $this->createCacheInstance(); |
166
|
|
|
$cache = $this->prepare($cache); |
167
|
|
|
|
168
|
|
|
$this->assertTrue($cache->clear()); |
169
|
|
|
$this->assertNull($cache->get($key)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @dataProvider dataProviderSetMultiple |
174
|
|
|
* @param int|null $ttl |
175
|
|
|
* @throws InvalidArgumentException |
176
|
|
|
*/ |
177
|
|
|
public function testSetMultiple(?int $ttl): void |
178
|
|
|
{ |
179
|
|
|
$cache = $this->createCacheInstance(); |
180
|
|
|
$cache->clear(); |
181
|
|
|
|
182
|
|
|
$data = $this->getDataProviderData(); |
183
|
|
|
|
184
|
|
|
$cache->setMultiple($data, $ttl); |
185
|
|
|
|
186
|
|
|
foreach ($data as $key => $value) { |
187
|
|
|
$this->assertSameExceptObject($value, $cache->get($key)); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return array testing multiSet with and without expiry |
193
|
|
|
*/ |
194
|
|
|
public function dataProviderSetMultiple(): array |
195
|
|
|
{ |
196
|
|
|
return [ |
197
|
|
|
[null], |
198
|
|
|
[2], |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testGetMultiple(): void |
203
|
|
|
{ |
204
|
|
|
/** @var Cache $cache */ |
205
|
|
|
$cache = $this->createCacheInstance(); |
206
|
|
|
$cache->clear(); |
207
|
|
|
|
208
|
|
|
$data = $this->getDataProviderData(); |
209
|
|
|
|
210
|
|
|
$cache->setMultiple($data); |
211
|
|
|
|
212
|
|
|
$this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data))); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testDeleteMultiple(): void |
216
|
|
|
{ |
217
|
|
|
/** @var Cache $cache */ |
218
|
|
|
$cache = $this->createCacheInstance(); |
219
|
|
|
$cache->clear(); |
220
|
|
|
|
221
|
|
|
$data = $this->getDataProviderData(); |
222
|
|
|
|
223
|
|
|
$cache->setMultiple($data); |
224
|
|
|
|
225
|
|
|
$this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data))); |
226
|
|
|
|
227
|
|
|
$cache->deleteMultiple(array_keys($data)); |
228
|
|
|
|
229
|
|
|
$emptyData = array_map(static function ($v) { |
|
|
|
|
230
|
|
|
return null; |
231
|
|
|
}, $data); |
232
|
|
|
|
233
|
|
|
$this->assertSameExceptObject($emptyData, $cache->getMultiple(array_keys($data))); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function testZeroAndNegativeTtl() |
237
|
|
|
{ |
238
|
|
|
$cache = $this->createCacheInstance(); |
239
|
|
|
$cache->clear(); |
240
|
|
|
$cache->setMultiple([ |
241
|
|
|
'a' => 1, |
242
|
|
|
'b' => 2, |
243
|
|
|
]); |
244
|
|
|
|
245
|
|
|
$this->assertTrue($cache->has('a')); |
246
|
|
|
$this->assertTrue($cache->has('b')); |
247
|
|
|
|
248
|
|
|
$cache->set('a', 11, -1); |
249
|
|
|
|
250
|
|
|
$this->assertFalse($cache->has('a')); |
251
|
|
|
|
252
|
|
|
$cache->set('b', 22, 0); |
253
|
|
|
|
254
|
|
|
$this->assertFalse($cache->has('b')); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
public function testDefaultTtl(): void |
258
|
|
|
{ |
259
|
|
|
$cache = $this->createCacheInstance(); |
260
|
|
|
$cache->clear(); |
261
|
|
|
/** @var BaseCache $cache */ |
262
|
|
|
$cache->setDefaultTtl(2); |
263
|
|
|
$this->assertSameExceptObject(2, $cache->getDefaultTtl()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function testDateIntervalTtl(): void |
267
|
|
|
{ |
268
|
|
|
$interval = new DateInterval('PT3S'); |
269
|
|
|
$cache = $this->createCacheInstance(); |
270
|
|
|
$cache->clear(); |
271
|
|
|
/** @var BaseCache $cache */ |
272
|
|
|
$cache->setDefaultTtl($interval); |
273
|
|
|
$this->assertSameExceptObject(3, $cache->getDefaultTtl()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @dataProvider dataProviderNormalizeTtl |
278
|
|
|
* @covers \Yiisoft\Cache\BaseCache::normalizeTtl() |
279
|
|
|
* @param mixed $ttl |
280
|
|
|
* @param int $expectedResult |
281
|
|
|
* @throws ReflectionException |
282
|
|
|
*/ |
283
|
|
|
public function testNormalizeTtl($ttl, int $expectedResult): void |
284
|
|
|
{ |
285
|
|
|
/** @var BaseCache $cache */ |
286
|
|
|
$cache = $this->getMockBuilder(BaseCache::class)->getMockForAbstractClass(); |
287
|
|
|
$cache->setDefaultTtl(9999); |
288
|
|
|
$this->assertSameExceptObject($expectedResult, $this->invokeMethod($cache, 'normalizeTtl', [$ttl])); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Data provider for {@see testNormalizeTtl()} |
293
|
|
|
* @return array test data |
294
|
|
|
* |
295
|
|
|
* @throws \Exception |
296
|
|
|
*/ |
297
|
|
|
public function dataProviderNormalizeTtl(): array |
298
|
|
|
{ |
299
|
|
|
return [ |
300
|
|
|
[123, 123], |
301
|
|
|
['123', 123], |
302
|
|
|
[null, 9999], |
303
|
|
|
[0, 0], |
304
|
|
|
[new DateInterval('PT6H8M'), 6 * 3600 + 8 * 60], |
305
|
|
|
[new DateInterval('P2Y4D'), 2 * 365 * 24 * 3600 + 4 * 24 * 3600], |
306
|
|
|
]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @dataProvider iterableProvider |
312
|
|
|
* @param array $array |
313
|
|
|
* @param iterable $iterable |
314
|
|
|
* @throws InvalidArgumentException |
315
|
|
|
*/ |
316
|
|
|
public function testValuesAsIterable(array $array, iterable $iterable): void |
317
|
|
|
{ |
318
|
|
|
$cache = $this->createCacheInstance(); |
319
|
|
|
$cache->clear(); |
320
|
|
|
|
321
|
|
|
$cache->setMultiple($iterable); |
322
|
|
|
|
323
|
|
|
$this->assertSameExceptObject($array, $cache->getMultiple(array_keys($array))); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function iterableProvider(): array |
327
|
|
|
{ |
328
|
|
|
return [ |
329
|
|
|
'array' => [ |
330
|
|
|
['a' => 1, 'b' => 2,], |
331
|
|
|
['a' => 1, 'b' => 2,], |
332
|
|
|
], |
333
|
|
|
'ArrayIterator' => [ |
334
|
|
|
['a' => 1, 'b' => 2,], |
335
|
|
|
new \ArrayIterator(['a' => 1, 'b' => 2,]), |
336
|
|
|
], |
337
|
|
|
'IteratorAggregate' => [ |
338
|
|
|
['a' => 1, 'b' => 2,], |
339
|
|
|
new class() implements \IteratorAggregate |
340
|
|
|
{ |
341
|
|
|
public function getIterator() |
342
|
|
|
{ |
343
|
|
|
return new \ArrayIterator(['a' => 1, 'b' => 2,]); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
], |
347
|
|
|
'Generator instance' => [ |
348
|
|
|
['a' => 1, 'b' => 2,], |
349
|
|
|
(static function () { |
350
|
|
|
yield 'a' => 1; |
351
|
|
|
yield 'b' => 2; |
352
|
|
|
})() |
353
|
|
|
] |
354
|
|
|
]; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.