1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Cache\Tests\ArrayCache; |
4
|
|
|
|
5
|
|
|
require_once __DIR__ . '/../MockHelper.php'; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
9
|
|
|
use Yiisoft\Cache\ArrayCache; |
10
|
|
|
use Yiisoft\Cache\Cache; |
11
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
12
|
|
|
use Yiisoft\Cache\Exception\InvalidArgumentException; |
13
|
|
|
use Yiisoft\Cache\Exception\SetCacheException; |
14
|
|
|
use Yiisoft\Cache\Tests\FalseCache; |
15
|
|
|
use Yiisoft\Cache\Tests\MockHelper; |
16
|
|
|
use Yiisoft\Cache\Tests\TestCase; |
17
|
|
|
|
18
|
|
|
class ArrayCacheDecoratorExtraTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected function tearDown(): void |
22
|
|
|
{ |
23
|
|
|
MockHelper::resetMocks(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function createCacheInstance(): CacheInterface |
27
|
|
|
{ |
28
|
|
|
return new Cache(new ArrayCache()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testAdd(): void |
32
|
|
|
{ |
33
|
|
|
/** @var Cache $cache */ |
34
|
|
|
$cache = $this->createCacheInstance(); |
35
|
|
|
$cache = $this->prepare($cache); |
36
|
|
|
|
37
|
|
|
// should not change existing keys |
38
|
|
|
$this->assertSameExceptObject('a', $cache->get('test_string')); |
39
|
|
|
$this->assertFalse($cache->add('test_string', 'b')); |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
// should store data if it's not there yet |
43
|
|
|
$this->assertNull($cache->get('add_test')); |
44
|
|
|
$this->assertTrue($cache->add('add_test', 13)); |
45
|
|
|
$this->assertSameExceptObject(13, $cache->get('add_test')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testAddWithDependency(): void |
49
|
|
|
{ |
50
|
|
|
/** @var Cache $cache */ |
51
|
|
|
$cache = $this->createCacheInstance(); |
52
|
|
|
|
53
|
|
|
$dependency = new TagDependency('tag_test'); |
54
|
|
|
|
55
|
|
|
$cache->add('a', 1, null, $dependency); |
56
|
|
|
|
57
|
|
|
$this->assertSameExceptObject(1, $cache->get('a')); |
58
|
|
|
|
59
|
|
|
TagDependency::invalidate($cache, 'tag_test'); |
60
|
|
|
|
61
|
|
|
$this->assertSameExceptObject(null, $cache->get('a')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testSetMultipleWithDependency(): void |
65
|
|
|
{ |
66
|
|
|
/** @var Cache $cache */ |
67
|
|
|
$cache = $this->createCacheInstance(); |
68
|
|
|
|
69
|
|
|
$dependency = new TagDependency('tag_test'); |
70
|
|
|
|
71
|
|
|
$cache->setMultiple(['a' => 1, 'b' => 2], null, $dependency); |
72
|
|
|
|
73
|
|
|
$this->assertSameExceptObject(['a' => 1, 'b' => 2], $cache->getMultiple(['a', 'b'])); |
74
|
|
|
|
75
|
|
|
TagDependency::invalidate($cache, 'tag_test'); |
76
|
|
|
|
77
|
|
|
$this->assertSameExceptObject(['a' => null, 'b' => null], $cache->getMultiple(['a', 'b'])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testAddMultiple(): void |
81
|
|
|
{ |
82
|
|
|
/** @var Cache $cache */ |
83
|
|
|
$cache = $this->createCacheInstance(); |
84
|
|
|
$cache = $this->prepare($cache); |
85
|
|
|
|
86
|
|
|
$this->assertNull($cache->get('add_test')); |
87
|
|
|
|
88
|
|
|
$this->assertTrue( |
89
|
|
|
$cache->addMultiple( |
90
|
|
|
[ |
91
|
|
|
'test_integer' => 13, |
92
|
|
|
'add_test' => 13, |
93
|
|
|
'444' => 14, |
94
|
|
|
] |
95
|
|
|
) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->assertSameExceptObject(1, $cache->get('test_integer')); |
99
|
|
|
$this->assertSameExceptObject(13, $cache->get('add_test')); |
100
|
|
|
$this->assertSameExceptObject(14, $cache->get('444')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetOrSet(): void |
104
|
|
|
{ |
105
|
|
|
$cache = $this->createCacheInstance(); |
106
|
|
|
$cache = $this->prepare($cache); |
107
|
|
|
|
108
|
|
|
$expected = get_class($cache); |
109
|
|
|
|
110
|
|
|
$this->assertSameExceptObject(null, $cache->get('something')); |
111
|
|
|
$this->assertSameExceptObject($expected, $cache->getOrSet('something', static function (CacheInterface $cache): string { |
112
|
|
|
return get_class($cache); |
113
|
|
|
})); |
114
|
|
|
$this->assertSameExceptObject($expected, $cache->get('something')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testGetOrSetException(): void |
118
|
|
|
{ |
119
|
|
|
$this->expectException(SetCacheException::class); |
120
|
|
|
|
121
|
|
|
$cache = new Cache(new FalseCache()); |
122
|
|
|
|
123
|
|
|
$cache->getOrSet('something', static function (CacheInterface $cache): string { |
124
|
|
|
return get_class($cache); |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testSetCacheException(): void |
129
|
|
|
{ |
130
|
|
|
$cache = new Cache(new FalseCache()); |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
$cache->getOrSet('something', static function (CacheInterface $cache): string { |
134
|
|
|
return get_class($cache); |
135
|
|
|
}); |
136
|
|
|
} catch (SetCacheException $e) { |
137
|
|
|
$this->assertEquals('something', $e->getKey()); |
138
|
|
|
$this->assertEquals('Yiisoft\Cache\Cache', $e->getValue()); |
139
|
|
|
$this->assertEquals($cache, $e->getCache()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetOrSetWithDependencies(): void |
144
|
|
|
{ |
145
|
|
|
/** @var Cache $cache */ |
146
|
|
|
$cache = $this->createCacheInstance(); |
147
|
|
|
|
148
|
|
|
$dependency = new TagDependency('test'); |
149
|
|
|
|
150
|
|
|
$expected = 'SilverFire'; |
151
|
|
|
$loginClosure = static function (): string { |
152
|
|
|
return 'SilverFire'; |
153
|
|
|
}; |
154
|
|
|
$this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency)); |
155
|
|
|
|
156
|
|
|
// Call again with another login to make sure that value is cached |
157
|
|
|
$loginClosure = static function (): string { |
158
|
|
|
return 'SamDark'; |
159
|
|
|
}; |
160
|
|
|
$cache->getOrSet('some-login', $loginClosure, null, $dependency); |
161
|
|
|
$this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency)); |
162
|
|
|
|
163
|
|
|
TagDependency::invalidate($cache, 'test'); |
164
|
|
|
$expected = 'SamDark'; |
165
|
|
|
$this->assertSameExceptObject($expected, $cache->getOrSet('some-login', $loginClosure, null, $dependency)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testWithArrayKeys(): void |
169
|
|
|
{ |
170
|
|
|
$key = [42]; |
171
|
|
|
$cache = $this->createCacheInstance(); |
172
|
|
|
$cache->clear(); |
173
|
|
|
|
174
|
|
|
$this->assertNull($cache->get($key)); |
175
|
|
|
|
176
|
|
|
$cache->set($key, 42); |
177
|
|
|
$this->assertSame(42, $cache->get($key)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testInvalidKey(): void |
181
|
|
|
{ |
182
|
|
|
$this->expectException(InvalidArgumentException::class); |
183
|
|
|
MockHelper::$mock_json_encode = false; |
184
|
|
|
$key = [42]; |
185
|
|
|
$cache = $this->createCacheInstance(); |
186
|
|
|
$cache->clear(); |
187
|
|
|
$cache->set($key, 42); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testWithObjectKeys(): void |
191
|
|
|
{ |
192
|
|
|
$key = new class { |
193
|
|
|
public $value = 42; |
194
|
|
|
}; |
195
|
|
|
$cache = $this->createCacheInstance(); |
196
|
|
|
$cache->clear(); |
197
|
|
|
|
198
|
|
|
$this->assertNull($cache->get($key)); |
199
|
|
|
|
200
|
|
|
$cache->set($key, 42); |
201
|
|
|
$this->assertSame(42, $cache->get($key)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testNormalizeKey(): void |
205
|
|
|
{ |
206
|
|
|
/** @var Cache $cache */ |
207
|
|
|
$cache = $this->createCacheInstance(); |
208
|
|
|
$cache->clear(); |
209
|
|
|
$cache->enableKeyNormalization(); |
210
|
|
|
|
211
|
|
|
$cache->set('test_normalized', 1); |
212
|
|
|
|
213
|
|
|
$cache->disableKeyNormalization(); |
214
|
|
|
|
215
|
|
|
$cache->set('test_not_normalized', 2); |
216
|
|
|
|
217
|
|
|
$cache->disableKeyNormalization(); |
218
|
|
|
$this->assertFalse($cache->has('test_normalized')); |
219
|
|
|
$this->assertSameExceptObject(1, $cache->get('2753a58fdc3bf713af86cb3a97a55e57')); |
220
|
|
|
$this->assertSameExceptObject(2, $cache->get('test_not_normalized')); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testGetWithPrefix(): void |
224
|
|
|
{ |
225
|
|
|
/** @var Cache $cache */ |
226
|
|
|
$cache = $this->createCacheInstance(); |
227
|
|
|
$cache->setKeyPrefix('prefix'); |
228
|
|
|
$cache = $this->prepare($cache); |
229
|
|
|
$this->assertSameExceptObject(1, $cache->get('test_integer')); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testInvalidKeyPrefix(): void |
233
|
|
|
{ |
234
|
|
|
$this->expectException(InvalidArgumentException::class); |
235
|
|
|
|
236
|
|
|
/** @var Cache $cache */ |
237
|
|
|
$cache = $this->createCacheInstance(); |
238
|
|
|
$cache->setKeyPrefix('_prefix'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testKeyPrefix(): void |
242
|
|
|
{ |
243
|
|
|
/** @var Cache $cache */ |
244
|
|
|
$cache = $this->createCacheInstance(); |
245
|
|
|
$cache->clear(); |
246
|
|
|
$cache->disableKeyNormalization(); |
247
|
|
|
$cache->setKeyPrefix('prefix'); |
248
|
|
|
|
249
|
|
|
$cache->set('test_with_prefix', 1); |
250
|
|
|
|
251
|
|
|
$cache->setKeyPrefix(''); |
252
|
|
|
|
253
|
|
|
$cache->set('test_without_prefix', 2); |
254
|
|
|
|
255
|
|
|
$cache->setKeyPrefix(''); |
256
|
|
|
$this->assertFalse($cache->has('test_with_prefix')); |
257
|
|
|
$this->assertSameExceptObject(1, $cache->get('prefixtest_with_prefix')); |
258
|
|
|
$this->assertSameExceptObject(2, $cache->get('test_without_prefix')); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @dataProvider featuresProvider |
263
|
|
|
* @param $features |
264
|
|
|
*/ |
265
|
|
|
public function testFeatures($features): void |
266
|
|
|
{ |
267
|
|
|
/** @var Cache $cache */ |
268
|
|
|
$cache = $this->createCacheInstance(); |
269
|
|
|
$cache->setKeyPrefix($features[0]); |
270
|
|
|
$features[1] ? $cache->enableKeyNormalization() : $cache->disableKeyNormalization(); |
271
|
|
|
|
272
|
|
|
$this->featuresTest($cache); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function featuresProvider(): array |
276
|
|
|
{ |
277
|
|
|
// [prefix, normalization] |
278
|
|
|
return [ |
279
|
|
|
[['', false]], |
280
|
|
|
[['testprefix', false]], |
281
|
|
|
[['testprefix', true]], |
282
|
|
|
[['', true]], |
283
|
|
|
]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
private function featuresTest(Cache $cache): void |
287
|
|
|
{ |
288
|
|
|
$this->prepare($cache); |
289
|
|
|
|
290
|
|
|
$dataWithPrefix = $this->getDataProviderData('for_multiple_'); |
291
|
|
|
$cache->setMultiple($dataWithPrefix); |
292
|
|
|
|
293
|
|
|
$data = $this->getDataProviderData() + $dataWithPrefix + ['nonexistent-key' => null]; |
294
|
|
|
$keys = array_map('strval', array_keys($data)); |
295
|
|
|
$dataWithDefault = $data; |
296
|
|
|
$dataWithDefault['nonexistent-key'] = 'default'; |
297
|
|
|
|
298
|
|
|
foreach ($data as $key => $value) { |
299
|
|
|
$key = (string)$key; |
300
|
|
|
if ($key === 'nonexistent-key') { |
301
|
|
|
$this->assertFalse($cache->has($key)); |
302
|
|
|
$this->assertSameExceptObject(null, $cache->get($key)); |
303
|
|
|
$this->assertSameExceptObject('default', $cache->get($key, 'default')); |
304
|
|
|
} else { |
305
|
|
|
$this->assertTrue($cache->has($key)); |
306
|
|
|
$this->assertSameExceptObject($value, $cache->get($key)); |
307
|
|
|
$this->assertSameExceptObject($value, $cache->get($key, 'default')); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$this->assertSameExceptObject($data, $cache->getMultiple($keys)); |
312
|
|
|
$this->assertSameExceptObject($dataWithDefault, $cache->getMultiple($keys, 'default')); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function testDefaultTtl(): void |
316
|
|
|
{ |
317
|
|
|
$cache = $this->createCacheInstance(); |
318
|
|
|
$cache->clear(); |
319
|
|
|
/** @var Cache $cache */ |
320
|
|
|
$cache->setDefaultTtl(2); |
321
|
|
|
$this->assertSameExceptObject(2, $cache->getDefaultTtl()); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testDateIntervalTtl(): void |
325
|
|
|
{ |
326
|
|
|
$interval = new DateInterval('PT3S'); |
327
|
|
|
$cache = $this->createCacheInstance(); |
328
|
|
|
$cache->clear(); |
329
|
|
|
/** @var Cache $cache */ |
330
|
|
|
$cache->setDefaultTtl($interval); |
331
|
|
|
$this->assertSameExceptObject(3, $cache->getDefaultTtl()); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|