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