test create session

This commit is contained in:
Yisroel Baum 2026-04-24 13:24:01 +03:00
parent 78ffb77f9f
commit 2a281386a5
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,83 @@
<?php
namespace Tests\Unit\Auth\UseCases;
use App\Auth\UseCases\CreateSession;
use App\User\User;
use App\ValueObjects\EmailAddress;
use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeSessionRepository;
use Tests\Fakes\FakeTokenGenerator;
class CreateSessionTest extends TestCase
{
private FakeSessionRepository $sessionRepo;
private FakeTokenGenerator $tokenGenerator;
private FakeClock $clock;
private CreateSession $useCase;
private User $user;
public function setUp(): void
{
$this->sessionRepo = new FakeSessionRepository();
$this->tokenGenerator = new FakeTokenGenerator(
['generated-token-abc']
);
$this->clock = new FakeClock(
new DateTimeImmutable('2025-01-01T12:00:00+00:00')
);
$this->useCase = new CreateSession(
$this->sessionRepo,
$this->tokenGenerator,
$this->clock,
);
$this->user = new User(
id: 7,
email: new EmailAddress('test@test.com'),
);
}
public function test_creates_session_for_user(): void
{
$session = $this->useCase->execute($this->user);
$this->assertEquals(7, $session->getUserId());
}
public function test_session_token_comes_from_generator(): void
{
$session = $this->useCase->execute($this->user);
$this->assertEquals('generated-token-abc', $session->getToken());
}
public function test_session_created_at_is_now(): void
{
$session = $this->useCase->execute($this->user);
$this->assertEquals(
new DateTimeImmutable('2025-01-01T12:00:00+00:00'),
$session->getCreatedAt()
);
}
public function test_session_expires_in_seven_days(): void
{
$session = $this->useCase->execute($this->user);
$this->assertEquals(
new DateTimeImmutable('2025-01-08T12:00:00+00:00'),
$session->getExpiresAt()
);
}
public function test_session_is_persisted(): void
{
$this->useCase->execute($this->user);
$found = $this->sessionRepo->findByToken('generated-token-abc');
$this->assertNotNull($found);
}
}