test fake session repository

This commit is contained in:
Yisroel Baum 2026-04-24 13:21:51 +03:00
parent 503df8be7a
commit c2ade8a601
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,68 @@
<?php
namespace Tests\Unit\Auth;
use App\Auth\CreateSessionDto;
use App\Auth\Session;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeSessionRepository;
class FakeSessionRepositoryTest extends TestCase
{
private FakeSessionRepository $sessionRepo;
public function setUp(): void
{
$this->sessionRepo = new FakeSessionRepository();
}
public function test_create_and_find_by_token(): void
{
$this->sessionRepo->create(new CreateSessionDto(
token: 'abc123',
userId: 0,
createdAt: new DateTimeImmutable('2025-01-01'),
expiresAt: new DateTimeImmutable('2025-01-08'),
));
$session = $this->sessionRepo->findByToken('abc123');
$this->assertInstanceOf(Session::class, $session);
$this->assertEquals('abc123', $session->getToken());
$this->assertEquals(0, $session->getUserId());
}
public function test_find_by_token_returns_null_when_missing(): void
{
$this->assertNull($this->sessionRepo->findByToken('nope'));
}
public function test_find_by_token_returns_fresh_instance(): void
{
$created = $this->sessionRepo->create(new CreateSessionDto(
token: 'abc123',
userId: 0,
createdAt: new DateTimeImmutable('2025-01-01'),
expiresAt: new DateTimeImmutable('2025-01-08'),
));
$fetched = $this->sessionRepo->findByToken('abc123');
$this->assertNotSame($created, $fetched);
}
public function test_delete_by_token(): void
{
$this->sessionRepo->create(new CreateSessionDto(
token: 'abc123',
userId: 0,
createdAt: new DateTimeImmutable('2025-01-01'),
expiresAt: new DateTimeImmutable('2025-01-08'),
));
$this->sessionRepo->deleteByToken('abc123');
$this->assertNull($this->sessionRepo->findByToken('abc123'));
}
}