test create session
This commit is contained in:
parent
78ffb77f9f
commit
2a281386a5
1 changed files with 83 additions and 0 deletions
83
tests/Unit/Auth/UseCases/CreateSessionTest.php
Normal file
83
tests/Unit/Auth/UseCases/CreateSessionTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue