forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockCache.php
More file actions
308 lines (260 loc) · 7.39 KB
/
MockCache.php
File metadata and controls
308 lines (260 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test\Mock;
use Closure;
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Cache\Handlers\BaseHandler;
use CodeIgniter\I18n\Time;
use PHPUnit\Framework\Assert;
class MockCache extends BaseHandler implements CacheInterface
{
/**
* Mock cache storage.
*
* @var array<string, mixed>
*/
protected $cache = [];
/**
* Expiration times.
*
* @var array<string, int|null>
*/
protected $expirations = [];
/**
* If true, will not cache any data.
*
* @var bool
*/
protected $bypass = false;
/**
* Takes care of any handler-specific setup that must be done.
*
* @return void
*/
public function initialize()
{
}
/**
* Attempts to fetch an item from the cache store.
*
* @param string $key Cache item name
*
* @return bool|null
*/
public function get(string $key)
{
$key = static::validateKey($key, $this->prefix);
return array_key_exists($key, $this->cache) ? $this->cache[$key] : null;
}
/**
* Get an item from the cache, or execute the given Closure and store the result.
*
* @return bool|null
*/
public function remember(string $key, int $ttl, Closure $callback)
{
$value = $this->get($key);
if ($value !== null) {
return $value;
}
$this->save($key, $value = $callback(), $ttl);
return $value;
}
/**
* Saves an item to the cache store.
*
* The $raw parameter is only utilized by Mamcache in order to
* allow usage of increment() and decrement().
*
* @param string $key Cache item name
* @param mixed $value the data to save
* @param int $ttl Time To Live, in seconds (default 60)
*
* @return bool
*/
public function save(string $key, $value, int $ttl = 60)
{
if ($this->bypass) {
return false;
}
$key = static::validateKey($key, $this->prefix);
$this->cache[$key] = $value;
$this->expirations[$key] = $ttl > 0 ? Time::now()->getTimestamp() + $ttl : null;
return true;
}
/**
* Deletes a specific item from the cache store.
*
* @return bool
*/
public function delete(string $key)
{
$key = static::validateKey($key, $this->prefix);
if (! isset($this->cache[$key])) {
return false;
}
unset($this->cache[$key], $this->expirations[$key]);
return true;
}
/**
* Deletes items from the cache store matching a given pattern.
*
* @return int
*/
public function deleteMatching(string $pattern)
{
$count = 0;
foreach (array_keys($this->cache) as $key) {
if (fnmatch($pattern, $key)) {
$count++;
unset($this->cache[$key], $this->expirations[$key]);
}
}
return $count;
}
/**
* Performs atomic incrementation of a raw stored value.
*
* @return bool
*/
public function increment(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);
$data = $this->cache[$key] ?: null;
if ($data === null) {
$data = 0;
} elseif (! is_int($data)) {
return false;
}
return $this->save($key, $data + $offset);
}
/**
* Performs atomic decrementation of a raw stored value.
*
* @return bool
*/
public function decrement(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);
$data = $this->cache[$key] ?: null;
if ($data === null) {
$data = 0;
} elseif (! is_int($data)) {
return false;
}
return $this->save($key, $data - $offset);
}
/**
* Will delete all items in the entire cache.
*
* @return bool
*/
public function clean()
{
$this->cache = [];
$this->expirations = [];
return true;
}
/**
* Returns information on the entire cache.
*
* The information returned and the structure of the data
* varies depending on the handler.
*
* @return list<string> Keys currently present in the store
*/
public function getCacheInfo()
{
return array_keys($this->cache);
}
/**
* Returns detailed information about the specific item in the cache.
*
* @return array{expire: int|null}|null Returns null if the item does not exist,
* otherwise, array with the 'expire' key for
* absolute epoch expiry (or null).
*/
public function getMetaData(string $key)
{
// Misses return null
if (! array_key_exists($key, $this->expirations)) {
return null;
}
// Count expired items as a miss
if (is_int($this->expirations[$key]) && $this->expirations[$key] > Time::now()->getTimestamp()) {
return null;
}
return ['expire' => $this->expirations[$key]];
}
/**
* Determine if the driver is supported on this system.
*/
public function isSupported(): bool
{
return true;
}
// --------------------------------------------------------------------
// Test Helpers
// --------------------------------------------------------------------
/**
* Instructs the class to ignore all
* requests to cache an item, and always "miss"
* when checked for existing data.
*
* @return $this
*/
public function bypass(bool $bypass = true)
{
$this->clean();
$this->bypass = $bypass;
return $this;
}
// --------------------------------------------------------------------
// Additional Assertions
// --------------------------------------------------------------------
/**
* Asserts that the cache has an item named $key.
* The value is not checked since storing false or null
* values is valid.
*
* @return void
*/
public function assertHas(string $key)
{
Assert::assertNotNull($this->get($key), "The cache does not have an item named: `{$key}`");
}
/**
* Asserts that the cache has an item named $key with a value matching $value.
*
* @param mixed $value
*
* @return void
*/
public function assertHasValue(string $key, $value = null)
{
$item = $this->get($key);
// Let assertHas() handle throwing the error for consistency
// if the key is not found
if ($item === null) {
$this->assertHas($key);
}
Assert::assertSame($value, $this->get($key), "The cached item `{$key}` does not equal match expectation. Found: " . print_r($value, true));
}
/**
* Asserts that the cache does NOT have an item named $key.
*
* @return void
*/
public function assertMissing(string $key)
{
Assert::assertArrayNotHasKey($key, $this->cache, "The cached item named `{$key}` exists.");
}
}