@@ -295,6 +295,49 @@ public function testGetItemMissWithFailingCallback(): void
295295 $ this ->testedInstance ->get ($ key , $ callback , $ beta , $ metadata );
296296 }
297297
298+ public function testGetItemWithDefaultLifetime ()
299+ {
300+ $ defaultLifetime = rand (100 , 200 );
301+
302+ $ key = uniqid ('key_ ' , true );
303+ $ value = uniqid ('value_ ' , true );
304+ $ callback = fn (ItemInterface $ item ) => $ value ;
305+ $ beta = (float ) rand (1 , 10 );
306+
307+ $ cacheItem = new CacheItem ();
308+
309+ $ expectedExpiryMin = microtime (true ) + self ::DEFAULT_MAX_STALE + $ defaultLifetime ;
310+
311+ $ metadataArgument = Argument::any ();
312+ $ this ->internalCache ->get ($ key , Argument::any (), 0 , $ metadataArgument )
313+ // Use cached value
314+ ->willReturn ($ value );
315+
316+ $ this ->internalCache ->get ($ key , Argument::any (), \INF , $ metadataArgument )
317+ ->shouldBeCalledOnce ()
318+ // Execute $callback
319+ ->will (function ($ args ) use ($ cacheItem ) {
320+ $ save = true ;
321+
322+ return $ args [1 ]($ cacheItem , $ save );
323+ });
324+
325+ $ this ->testedInstance ->setDefaultLifetime ($ defaultLifetime );
326+
327+ // Item is in cache, but in stale mode
328+ // Value cannot be refreshed due to failing source
329+ $ metadata = [ItemInterface::METADATA_EXPIRY => microtime (true ) + self ::DEFAULT_MAX_STALE / 2 ];
330+ $ result = $ this ->testedInstance ->get ($ key , $ callback , $ beta , $ metadata );
331+
332+ $ expectedExpiryMax = microtime (true ) + self ::DEFAULT_MAX_STALE + $ defaultLifetime ;
333+
334+ self ::assertEquals ($ value , $ result );
335+
336+ $ cacheItemExpiry = self ::getCacheItemExpiry ($ cacheItem );
337+ self ::assertGreaterThan ($ expectedExpiryMin , $ cacheItemExpiry );
338+ self ::assertLessThan ($ expectedExpiryMax , $ cacheItemExpiry );
339+ }
340+
298341 public function testDelete (): void
299342 {
300343 $ key = uniqid ('key_ ' , true );
@@ -321,16 +364,18 @@ public function testInvalidateTags(): void
321364 self ::assertEquals ($ success , $ result );
322365 }
323366
324- public function testGetItemWithDefaultLifetime ()
325- {
326- // TODO
327- }
328-
329367 private static function assertCacheItemExpiryEquals (float $ expiry , CacheItem $ cacheItem )
330368 {
331369 $ cacheItemExpiry = (\Closure::bind (function (CacheItem $ item ) {
332370 return $ item ->expiry ;
333371 }, null , CacheItem::class))($ cacheItem );
334372 self ::assertEquals ($ expiry , $ cacheItemExpiry );
335373 }
374+
375+ private static function getCacheItemExpiry (CacheItem $ cacheItem )
376+ {
377+ return (\Closure::bind (function (CacheItem $ item ) {
378+ return $ item ->expiry ;
379+ }, null , CacheItem::class))($ cacheItem );
380+ }
336381}
0 commit comments