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