Skip to content

Commit e9f14d8

Browse files
committed
Update MockCache for interface expectations
1 parent f6051ae commit e9f14d8

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

system/Test/Mock/MockCache.php

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
class MockCache implements CacheInterface
1818
{
1919
/**
20-
* Prefixed to all cache names.
20+
* Mock cache storage.
2121
*
22-
* @var string
22+
* @var array
2323
*/
24-
protected $prefix;
24+
protected $cache = [];
2525

2626
/**
27-
* Mock cache storage.
27+
* Expiration times.
2828
*
29-
* @var array
29+
* @var ?int[]
3030
*/
31-
protected $cache = [];
31+
protected $expirations = [];
3232

3333
//--------------------------------------------------------------------
3434

@@ -37,7 +37,6 @@ class MockCache implements CacheInterface
3737
*/
3838
public function initialize()
3939
{
40-
// Not to see here...
4140
}
4241

4342
//--------------------------------------------------------------------
@@ -51,7 +50,7 @@ public function initialize()
5150
*/
5251
public function get(string $key)
5352
{
54-
$key = $this->prefix . $key;
53+
$key = static::validateKey($key, $this->prefix);
5554

5655
return array_key_exists($key, $this->cache)
5756
? $this->cache[$key]
@@ -96,13 +95,14 @@ public function remember(string $key, int $ttl, Closure $callback)
9695
* @param integer $ttl Time To Live, in seconds (default 60)
9796
* @param boolean $raw Whether to store the raw value.
9897
*
99-
* @return mixed
98+
* @return bool
10099
*/
101100
public function save(string $key, $value, int $ttl = 60, bool $raw = false)
102101
{
103-
$key = $this->prefix . $key;
102+
$key = static::validateKey($key, $this->prefix);
104103

105-
$this->cache[$key] = $value;
104+
$this->cache[$key] = $value;
105+
$this->expirations[$key] = $ttl > 0 ? time() + $ttl : null;
106106

107107
return true;
108108
}
@@ -114,11 +114,21 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false)
114114
*
115115
* @param string $key Cache item name
116116
*
117-
* @return mixed
117+
* @return bool
118118
*/
119119
public function delete(string $key)
120120
{
121+
$key = static::validateKey($key, $this->prefix);
122+
123+
if (! isset($this->cache[$key]))
124+
{
125+
return false;
126+
}
127+
121128
unset($this->cache[$key]);
129+
unset($this->expirations[$key]);
130+
131+
return true;
122132
}
123133

124134
//--------------------------------------------------------------------
@@ -137,6 +147,7 @@ public function deleteMatching(string $pattern)
137147
if (fnmatch($pattern, $key))
138148
{
139149
unset($this->cache[$key]);
150+
unset($this->expirations[$key]);
140151
}
141152
}
142153

@@ -151,12 +162,11 @@ public function deleteMatching(string $pattern)
151162
* @param string $key Cache ID
152163
* @param integer $offset Step/value to increase by
153164
*
154-
* @return mixed
165+
* @return bool
155166
*/
156167
public function increment(string $key, int $offset = 1)
157168
{
158-
$key = $this->prefix . $key;
159-
169+
$key = static::validateKey($key, $this->prefix);
160170
$data = $this->cache[$key] ?: null;
161171

162172
if (empty($data))
@@ -179,11 +189,11 @@ public function increment(string $key, int $offset = 1)
179189
* @param string $key Cache ID
180190
* @param integer $offset Step/value to increase by
181191
*
182-
* @return mixed
192+
* @return bool
183193
*/
184194
public function decrement(string $key, int $offset = 1)
185195
{
186-
$key = $this->prefix . $key;
196+
$key = static::validateKey($key, $this->prefix);
187197

188198
$data = $this->cache[$key] ?: null;
189199

@@ -204,11 +214,14 @@ public function decrement(string $key, int $offset = 1)
204214
/**
205215
* Will delete all items in the entire cache.
206216
*
207-
* @return mixed
217+
* @return bool
208218
*/
209219
public function clean()
210220
{
211-
$this->cache = [];
221+
$this->cache = [];
222+
$this->expirations = [];
223+
224+
return true;
212225
}
213226

214227
//--------------------------------------------------------------------
@@ -233,11 +246,27 @@ public function getCacheInfo()
233246
*
234247
* @param string $key Cache item name.
235248
*
236-
* @return mixed
249+
* @return array|null
250+
* Returns null if the item does not exist, otherwise array<string, mixed>
251+
* with at least the 'expire' key for absolute epoch expiry (or null).
237252
*/
238253
public function getMetaData(string $key)
239254
{
240-
return false;
255+
// Misses return null
256+
if (! array_key_exists($key, $this->expirations))
257+
{
258+
return null;
259+
}
260+
261+
// Count expired items as a miss
262+
if (is_int($this->expirations[$key]) && $this->expirations[$key] > time())
263+
{
264+
return null;
265+
}
266+
267+
return [
268+
'expire' => $this->expirations[$key],
269+
];
241270
}
242271

243272
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)