test auth contracts

This commit is contained in:
Yisroel Baum 2026-08-02 20:53:15 +03:00
parent 3da9c586c3
commit b3266b38c8
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
15 changed files with 457 additions and 165 deletions

View file

@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\Auth\UseCases;
use App\Auth\UseCases\CreateSession\CreateSession;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
use DateTimeImmutable;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeSessionRepository;
use Tests\Fakes\FakeTokenGenerator;
class CreateSessionTest extends TestCase
{
private DateTimeImmutable $now;
private FakeSessionRepository $sessionRepository;
private CreateSession $useCase;
protected function setUp(): void
{
$this->now = new DateTimeImmutable(
'2026-07-31T12:00:00',
new DateTimeZone('UTC'),
);
$this->sessionRepository = new FakeSessionRepository;
$this->useCase = new CreateSession(
$this->sessionRepository,
new FakeTokenGenerator(['session-token']),
new FakeClock($this->now),
);
}
public function test_creates_a_seven_day_session_with_generated_token(): void
{
$user = $this->user();
$session = $this->useCase->execute($user);
$this->assertSame('session-token', $session->getToken());
$this->assertSame($user, $session->getUser());
$this->assertSame($this->now, $session->getCreatedAt());
$this->assertEquals(
$this->now->modify('+7 days'),
$session->getExpiresAt(),
);
}
public function test_created_session_is_findable_by_token(): void
{
$this->useCase->execute($this->user());
$session = $this->sessionRepository->findByToken('session-token');
$this->assertNotNull($session);
$this->assertSame(7, $session->getUser()->getId());
}
private function user(): User
{
return new User(
id: 7,
email: new EmailAddress('user@example.com'),
passwordHash: 'hashed:correct-password',
);
}
}