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