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
|
|
|
public $value = 42; |
127
|
|
|
}; |
128
|
|
|
$cache = $this->createCacheInstance(); |
129
|
|
|
$cache->clear(); |
130
|
|
|
|
131
|
|
|
$this->assertNull($cache->get($key)); |
132
|
|
|
|
133
|
|
|
$cache->set($key, 42); |
134
|
|
|
$this->assertSame(42, $cache->get($key)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testNormalizeKey(): void |
138
|
|
|
{ |
139
|
|
|
/** @var Cache $cache */ |
140
|
|
|
$cache = $this->createCacheInstance(); |
141
|
|
|
$cache->clear(); |
142
|
|
|
$cache->enableKeyNormalization(); |
143
|
|
|
|
144
|
|
|
$cache->set('test_normalized', 1); |
145
|
|
|
|
146
|
|
|
$cache->disableKeyNormalization(); |
147
|
|
|
|
148
|
|
|
$cache->set('test_not_normalized', 2); |
149
|
|
|
|
150
|
|
|
$cache->disableKeyNormalization(); |
151
|
|
|
$this->assertFalse($cache->has('test_normalized')); |
152
|
|
|
$this->assertSameExceptObject(1, $cache->get('2753a58fdc3bf713af86cb3a97a55e57')); |
153
|
|
|
$this->assertSameExceptObject(2, $cache->get('test_not_normalized')); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testGetWithPrefix(): void |
157
|
|
|
{ |
158
|
|
|
/** @var Cache $cache */ |
159
|
|
|
$cache = $this->createCacheInstance(); |
160
|
|
|
$cache->setKeyPrefix('prefix'); |
161
|
|
|
$cache = $this->prepare($cache); |
162
|
|
|
$this->assertSameExceptObject(1, $cache->get('test_integer')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testKeyPrefix(): void |
166
|
|
|
{ |
167
|
|
|
/** @var Cache $cache */ |
168
|
|
|
$cache = $this->createCacheInstance(); |
169
|
|
|
$cache->clear(); |
170
|
|
|
$cache->disableKeyNormalization(); |
171
|
|
|
$cache->setKeyPrefix('prefix'); |
172
|
|
|
|
173
|
|
|
$cache->set('test_with_prefix', 1); |
174
|
|
|
|
175
|
|
|
$cache->setKeyPrefix(''); |
176
|
|
|
|
177
|
|
|
$cache->set('test_without_prefix', 2); |
178
|
|
|
|
179
|
|
|
$cache->setKeyPrefix(''); |
180
|
|
|
$this->assertFalse($cache->has('test_with_prefix')); |
181
|
|
|
$this->assertSameExceptObject(1, $cache->get('prefixtest_with_prefix')); |
182
|
|
|
$this->assertSameExceptObject(2, $cache->get('test_without_prefix')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @dataProvider featuresProvider |
187
|
|
|
* @param $features |
188
|
|
|
*/ |
189
|
|
|
public function testFeatures($features): void |
190
|
|
|
{ |
191
|
|
|
/** @var Cache $cache */ |
192
|
|
|
$cache = $this->createCacheInstance(); |
193
|
|
|
$cache->setKeyPrefix($features[0]); |
194
|
|
|
$features[1] ? $cache->enableKeyNormalization() : $cache->disableKeyNormalization(); |
195
|
|
|
|
196
|
|
|
$this->featuresTest($cache); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function featuresProvider() |
200
|
|
|
{ |
201
|
|
|
// [prefix, normalization] |
202
|
|
|
return [ |
203
|
|
|
[['', false]], |
204
|
|
|
[['testprefix', false]], |
205
|
|
|
[['testprefix', true]], |
206
|
|
|
[['', true]], |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
private function featuresTest(Cache $cache) |
211
|
|
|
{ |
212
|
|
|
$this->prepare($cache); |
213
|
|
|
|
214
|
|
|
$dataWithPrefix = $this->getDataProviderData('for_multiple_'); |
215
|
|
|
$cache->setMultiple($dataWithPrefix); |
216
|
|
|
|
217
|
|
|
$data = array_merge($this->getDataProviderData(), $dataWithPrefix, ['nonexistent-key' => null]); |
218
|
|
|
$dataWithDefault = $data; |
219
|
|
|
$dataWithDefault['nonexistent-key'] = 'default'; |
220
|
|
|
|
221
|
|
|
foreach ($data as $key => $value) { |
222
|
|
|
if ($key === 'nonexistent-key') { |
223
|
|
|
$this->assertFalse($cache->has($key)); |
224
|
|
|
$this->assertSameExceptObject(null, $cache->get($key)); |
225
|
|
|
$this->assertSameExceptObject('default', $cache->get($key, 'default')); |
226
|
|
|
} else { |
227
|
|
|
$this->assertTrue($cache->has($key)); |
228
|
|
|
$this->assertSameExceptObject($value, $cache->get($key)); |
229
|
|
|
$this->assertSameExceptObject($value, $cache->get($key, 'default')); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->assertSameExceptObject($data, $cache->getMultiple(array_keys($data))); |
234
|
|
|
$this->assertSameExceptObject($dataWithDefault, $cache->getMultiple(array_keys($data), 'default')); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testDefaultTtl(): void |
238
|
|
|
{ |
239
|
|
|
$cache = $this->createCacheInstance(); |
240
|
|
|
$cache->clear(); |
241
|
|
|
/** @var Cache $cache */ |
242
|
|
|
$cache->setDefaultTtl(2); |
243
|
|
|
$this->assertSameExceptObject(2, $cache->getDefaultTtl()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testDateIntervalTtl(): void |
247
|
|
|
{ |
248
|
|
|
$interval = new DateInterval('PT3S'); |
249
|
|
|
$cache = $this->createCacheInstance(); |
250
|
|
|
$cache->clear(); |
251
|
|
|
/** @var Cache $cache */ |
252
|
|
|
$cache->setDefaultTtl($interval); |
253
|
|
|
$this->assertSameExceptObject(3, $cache->getDefaultTtl()); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|