98 lines
3.1 KiB
PHP
98 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\MyEp\Cache;
|
|
|
|
use App\MyEp\Cache\StaleWhileErrorCache;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
|
|
class StaleWhileErrorCacheTest extends TestCase
|
|
{
|
|
public function testTheProducedValueIsReturnedAndCached(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$cache = new StaleWhileErrorCache($pool, new NullLogger());
|
|
|
|
$value = $cache->get('myep.hotels.v1', 3600, fn () => ['a' => 1]);
|
|
|
|
$this->assertEquals(['a' => 1], $value);
|
|
$this->assertTrue($pool->getItem('myep.hotels.v1')->isHit());
|
|
$this->assertTrue($pool->getItem('myep.hotels.v1.last_good')->isHit());
|
|
}
|
|
|
|
public function testACachedValueIsServedWithoutProducingAgain(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$cache = new StaleWhileErrorCache($pool, new NullLogger());
|
|
$calls = 0;
|
|
|
|
$producer = function () use (&$calls) {
|
|
++$calls;
|
|
|
|
return ['a' => 1];
|
|
};
|
|
|
|
$cache->get('myep.hotels.v1', 3600, $producer);
|
|
$value = $cache->get('myep.hotels.v1', 3600, $producer);
|
|
|
|
$this->assertEquals(['a' => 1], $value);
|
|
$this->assertEquals(1, $calls);
|
|
}
|
|
|
|
public function testTheLastKnownValueIsServedWhenProducingFails(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$cache = new StaleWhileErrorCache($pool, new NullLogger());
|
|
|
|
$cache->get('myep.hotels.v1', 3600, fn () => ['a' => 1]);
|
|
$pool->deleteItem('myep.hotels.v1');
|
|
|
|
$value = $cache->get('myep.hotels.v1', 3600, fn () => throw new \RuntimeException('upstream is down'));
|
|
|
|
$this->assertEquals(['a' => 1], $value);
|
|
}
|
|
|
|
public function testAServedStaleValueSuppressesFurtherAttempts(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$cache = new StaleWhileErrorCache($pool, new NullLogger());
|
|
$calls = 0;
|
|
|
|
$failing = function () use (&$calls) {
|
|
++$calls;
|
|
|
|
throw new \RuntimeException('upstream is down');
|
|
};
|
|
|
|
$cache->get('myep.hotels.v1', 3600, fn () => ['a' => 1]);
|
|
$pool->deleteItem('myep.hotels.v1');
|
|
|
|
$cache->get('myep.hotels.v1', 3600, $failing);
|
|
$cache->get('myep.hotels.v1', 3600, $failing);
|
|
|
|
$this->assertEquals(1, $calls);
|
|
}
|
|
|
|
public function testAFailureWithoutAnyCachedValueIsEmpty(): void
|
|
{
|
|
$cache = new StaleWhileErrorCache(new ArrayAdapter(), new NullLogger());
|
|
|
|
$value = $cache->get('myep.hotels.v1', 3600, fn () => throw new \RuntimeException('upstream is down'));
|
|
|
|
$this->assertEquals([], $value);
|
|
}
|
|
|
|
public function testEachKeyKeepsItsOwnValue(): void
|
|
{
|
|
$pool = new ArrayAdapter();
|
|
$cache = new StaleWhileErrorCache($pool, new NullLogger());
|
|
|
|
$cache->get('myep.hotels.v1', 3600, fn () => ['hotel']);
|
|
$cache->get('myep.pickups.v1', 3600, fn () => ['pickup']);
|
|
|
|
$this->assertEquals(['hotel'], $cache->get('myep.hotels.v1', 3600, fn () => ['other']));
|
|
$this->assertEquals(['pickup'], $cache->get('myep.pickups.v1', 3600, fn () => ['other']));
|
|
}
|
|
}
|