2026-03-18 22:50:23 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Tests\Service;
|
|
|
|
|
|
|
|
|
|
use App\Enum\CacheKey;
|
|
|
|
|
use App\Service\CacheService;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Psr\Cache\CacheItemInterface;
|
|
|
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
|
|
|
|
|
|
|
|
class CacheServiceTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
private CacheItemPoolInterface $pool;
|
|
|
|
|
private CacheService $service;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->pool = $this->createMock(CacheItemPoolInterface::class);
|
|
|
|
|
$this->service = new CacheService($this->pool);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 23:14:04 +01:00
|
|
|
private function mockCacheItem(bool $isHit, mixed $value = null): CacheItemInterface
|
2026-03-18 22:50:23 +01:00
|
|
|
{
|
|
|
|
|
$item = $this->createMock(CacheItemInterface::class);
|
2026-03-18 23:14:04 +01:00
|
|
|
$item->method('isHit')->willReturn($isHit);
|
|
|
|
|
$item->method('get')->willReturn($value);
|
|
|
|
|
$item->method('set')->willReturnSelf();
|
|
|
|
|
$item->method('expiresAfter')->willReturnSelf();
|
|
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetReturnsNullOnCacheMiss(): void
|
|
|
|
|
{
|
|
|
|
|
$this->pool->method('getItem')->willReturn($this->mockCacheItem(false));
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
self::assertNull($this->service->get(CacheKey::HOME_PAGE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetReturnsValueOnCacheHit(): void
|
|
|
|
|
{
|
2026-03-18 23:14:04 +01:00
|
|
|
$this->pool->method('getItem')->willReturn($this->mockCacheItem(true, 'cached-data'));
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
self::assertSame('cached-data', $this->service->get(CacheKey::HOME_PAGE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetStoresValueWithTtl(): void
|
|
|
|
|
{
|
2026-03-18 23:14:04 +01:00
|
|
|
$item = $this->mockCacheItem(false);
|
2026-03-18 22:50:23 +01:00
|
|
|
$this->pool->method('getItem')->willReturn($item);
|
2026-03-18 23:14:04 +01:00
|
|
|
$this->pool->expects(self::once())->method('save')->with($item)->willReturn(true);
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
$this->service->set(CacheKey::HOME_PAGE, 'value');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExistsDelegatesToHasItem(): void
|
|
|
|
|
{
|
|
|
|
|
$this->pool->expects(self::once())->method('hasItem')->willReturn(true);
|
|
|
|
|
|
|
|
|
|
self::assertTrue($this->service->exists(CacheKey::HOME_PAGE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDeleteDelegatesToDeleteItem(): void
|
|
|
|
|
{
|
2026-03-18 23:14:04 +01:00
|
|
|
$this->pool->expects(self::once())->method('deleteItem')->willReturn(true);
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
$this->service->delete(CacheKey::HOME_PAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRememberReturnsCachedValueOnHit(): void
|
|
|
|
|
{
|
2026-03-18 23:14:04 +01:00
|
|
|
$this->pool->method('getItem')->willReturn($this->mockCacheItem(true, 'cached'));
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
$result = $this->service->remember(CacheKey::HOME_PAGE, fn () => 'fresh');
|
|
|
|
|
|
|
|
|
|
self::assertSame('cached', $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRememberCallsCallbackOnMiss(): void
|
|
|
|
|
{
|
2026-03-18 23:14:04 +01:00
|
|
|
$item = $this->mockCacheItem(false);
|
2026-03-18 22:50:23 +01:00
|
|
|
$this->pool->method('getItem')->willReturn($item);
|
2026-03-18 23:14:04 +01:00
|
|
|
$this->pool->expects(self::once())->method('save')->with($item)->willReturn(true);
|
2026-03-18 22:50:23 +01:00
|
|
|
|
|
|
|
|
$result = $this->service->remember(CacheKey::HOME_PAGE, fn () => 'fresh');
|
|
|
|
|
|
|
|
|
|
self::assertSame('fresh', $result);
|
|
|
|
|
}
|
|
|
|
|
}
|