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