Skip to content

Commit 9e03806

Browse files
fferriereshavounet
authored andcommitted
fix(tests): fix tests for default_lifetime usage (#1)
1 parent 91ca31c commit 9e03806

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ You can execute `make test` to execute all tests.
4949

5050
* [ ] Create Github Actions to execute tests
5151
* [ ] Cleanup configuration
52-
* [ ] Create version 1.0 for Symfony 5.4 + PHP 7.4 and version 2.0 for Symfony 6.0 and PHP 8.0, using branch `feat/php8`
52+
* [ ] Create version 1.0 for Symfony 5.4 + PHP 7.4 and version 2.0 for Symfony 6.0 and PHP 8.0, using branch `feat/php8`

src/DependencyInjection/AddStaleCacheLifetime.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class AddStaleCacheLifetime implements CompilerPassInterface
1111
{
12-
public function process(ContainerBuilder $container)
12+
public function process(ContainerBuilder $container): void
1313
{
1414
$cachePoolServices = $container->findTaggedServiceIds('cache.pool');
1515
$staleCacheServices = $container->findTaggedServiceIds('bedrock_stale_cache.stale_cache');
@@ -25,6 +25,12 @@ public function process(ContainerBuilder $container)
2525
}
2626
}
2727

28+
/**
29+
* @param array<string, array<array{
30+
* 'name'?: string,
31+
* 'default_lifetime'?: int,
32+
* }>> $cachePoolServices
33+
*/
2834
private function findCachePoolDefaultLifetime(string $cachePool, array $cachePoolServices): ?int
2935
{
3036
foreach ($cachePoolServices as $serviceId => $tags) {

tests/Unit/Cache/StaleTest.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)