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