Files
e-ticket/tests/Service/CacheServiceTest.php
Serreau Jovann dc3d464b17 Add PHPUnit tests with coverage for all src classes
- 21 test files covering controllers, services, entities, enums, messages
- CI: add test job with Xdebug coverage (clover + text)
- SonarQube: configure coverage report path and test sources

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 22:50:23 +01:00

92 lines
3.1 KiB
PHP

<?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);
}
public function testGetReturnsNullOnCacheMiss(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->method('isHit')->willReturn(false);
$this->pool->method('getItem')->willReturn($item);
self::assertNull($this->service->get(CacheKey::HOME_PAGE));
}
public function testGetReturnsValueOnCacheHit(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->method('isHit')->willReturn(true);
$item->method('get')->willReturn('cached-data');
$this->pool->method('getItem')->willReturn($item);
self::assertSame('cached-data', $this->service->get(CacheKey::HOME_PAGE));
}
public function testSetStoresValueWithTtl(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->expects(self::once())->method('set')->with('value');
$item->expects(self::once())->method('expiresAfter')->with(CacheKey::HOME_PAGE->ttl());
$this->pool->method('getItem')->willReturn($item);
$this->pool->expects(self::once())->method('save')->with($item);
$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
{
$this->pool->expects(self::once())->method('deleteItem');
$this->service->delete(CacheKey::HOME_PAGE);
}
public function testRememberReturnsCachedValueOnHit(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->method('isHit')->willReturn(true);
$item->method('get')->willReturn('cached');
$this->pool->method('getItem')->willReturn($item);
$result = $this->service->remember(CacheKey::HOME_PAGE, fn () => 'fresh');
self::assertSame('cached', $result);
}
public function testRememberCallsCallbackOnMiss(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->method('isHit')->willReturn(false);
$item->expects(self::once())->method('set')->with('fresh');
$item->expects(self::once())->method('expiresAfter')->with(CacheKey::HOME_PAGE->ttl());
$this->pool->method('getItem')->willReturn($item);
$this->pool->expects(self::once())->method('save')->with($item);
$result = $this->service->remember(CacheKey::HOME_PAGE, fn () => 'fresh');
self::assertSame('fresh', $result);
}
}