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