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