1212namespace CodeIgniter \Test \Mock ;
1313
1414use CodeIgniter \Cache \CacheInterface ;
15+ use CodeIgniter \Cache \Handlers \BaseHandler ;
1516use Closure ;
1617
17- class MockCache implements CacheInterface
18+ class MockCache extends BaseHandler implements CacheInterface
1819{
1920 /**
20- * Prefixed to all cache names .
21+ * Mock cache storage .
2122 *
22- * @var string
23+ * @var array
2324 */
24- protected $ prefix ;
25+ protected $ cache = [] ;
2526
2627 /**
27- * Mock cache storage .
28+ * Expiration times .
2829 *
29- * @var array
30+ * @var ?int[]
3031 */
31- protected $ cache = [];
32+ protected $ expirations = [];
3233
3334 //--------------------------------------------------------------------
3435
@@ -37,7 +38,6 @@ class MockCache implements CacheInterface
3738 */
3839 public function initialize ()
3940 {
40- // Not to see here...
4141 }
4242
4343 //--------------------------------------------------------------------
@@ -51,7 +51,7 @@ public function initialize()
5151 */
5252 public function get (string $ key )
5353 {
54- $ key = $ this ->prefix . $ key ;
54+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
5555
5656 return array_key_exists ($ key , $ this ->cache )
5757 ? $ this ->cache [$ key ]
@@ -96,13 +96,14 @@ public function remember(string $key, int $ttl, Closure $callback)
9696 * @param integer $ttl Time To Live, in seconds (default 60)
9797 * @param boolean $raw Whether to store the raw value.
9898 *
99- * @return mixed
99+ * @return boolean
100100 */
101101 public function save (string $ key , $ value , int $ ttl = 60 , bool $ raw = false )
102102 {
103- $ key = $ this ->prefix . $ key ;
103+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
104104
105- $ this ->cache [$ key ] = $ value ;
105+ $ this ->cache [$ key ] = $ value ;
106+ $ this ->expirations [$ key ] = $ ttl > 0 ? time () + $ ttl : null ;
106107
107108 return true ;
108109 }
@@ -114,11 +115,21 @@ public function save(string $key, $value, int $ttl = 60, bool $raw = false)
114115 *
115116 * @param string $key Cache item name
116117 *
117- * @return mixed
118+ * @return boolean
118119 */
119120 public function delete (string $ key )
120121 {
122+ $ key = static ::validateKey ($ key , $ this ->prefix );
123+
124+ if (! isset ($ this ->cache [$ key ]))
125+ {
126+ return false ;
127+ }
128+
121129 unset($ this ->cache [$ key ]);
130+ unset($ this ->expirations [$ key ]);
131+
132+ return true ;
122133 }
123134
124135 //--------------------------------------------------------------------
@@ -128,19 +139,22 @@ public function delete(string $key)
128139 *
129140 * @param string $pattern Cache items glob-style pattern
130141 *
131- * @return boolean
142+ * @return integer
132143 */
133144 public function deleteMatching (string $ pattern )
134145 {
146+ $ count = 0 ;
135147 foreach (array_keys ($ this ->cache ) as $ key )
136148 {
137149 if (fnmatch ($ pattern , $ key ))
138150 {
151+ $ count ++;
139152 unset($ this ->cache [$ key ]);
153+ unset($ this ->expirations [$ key ]);
140154 }
141155 }
142156
143- return true ;
157+ return $ count ;
144158 }
145159
146160 //--------------------------------------------------------------------
@@ -151,12 +165,11 @@ public function deleteMatching(string $pattern)
151165 * @param string $key Cache ID
152166 * @param integer $offset Step/value to increase by
153167 *
154- * @return mixed
168+ * @return boolean
155169 */
156170 public function increment (string $ key , int $ offset = 1 )
157171 {
158- $ key = $ this ->prefix . $ key ;
159-
172+ $ key = static ::validateKey ($ key , $ this ->prefix );
160173 $ data = $ this ->cache [$ key ] ?: null ;
161174
162175 if (empty ($ data ))
@@ -179,11 +192,11 @@ public function increment(string $key, int $offset = 1)
179192 * @param string $key Cache ID
180193 * @param integer $offset Step/value to increase by
181194 *
182- * @return mixed
195+ * @return boolean
183196 */
184197 public function decrement (string $ key , int $ offset = 1 )
185198 {
186- $ key = $ this ->prefix . $ key ;
199+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
187200
188201 $ data = $ this ->cache [$ key ] ?: null ;
189202
@@ -204,11 +217,14 @@ public function decrement(string $key, int $offset = 1)
204217 /**
205218 * Will delete all items in the entire cache.
206219 *
207- * @return mixed
220+ * @return boolean
208221 */
209222 public function clean ()
210223 {
211- $ this ->cache = [];
224+ $ this ->cache = [];
225+ $ this ->expirations = [];
226+
227+ return true ;
212228 }
213229
214230 //--------------------------------------------------------------------
@@ -233,11 +249,27 @@ public function getCacheInfo()
233249 *
234250 * @param string $key Cache item name.
235251 *
236- * @return mixed
252+ * @return array|null
253+ * Returns null if the item does not exist, otherwise array<string, mixed>
254+ * with at least the 'expire' key for absolute epoch expiry (or null).
237255 */
238256 public function getMetaData (string $ key )
239257 {
240- return false ;
258+ // Misses return null
259+ if (! array_key_exists ($ key , $ this ->expirations ))
260+ {
261+ return null ;
262+ }
263+
264+ // Count expired items as a miss
265+ if (is_int ($ this ->expirations [$ key ]) && $ this ->expirations [$ key ] > time ())
266+ {
267+ return null ;
268+ }
269+
270+ return [
271+ 'expire ' => $ this ->expirations [$ key ],
272+ ];
241273 }
242274
243275 //--------------------------------------------------------------------
0 commit comments