Forcing every call site to be explicit about admin status and password eliminates a class of bugs where an unintended isAdmin=false or empty passwordHash could silently slip through. The CreateUserTest case that asserted the isAdmin default is dropped since the default no longer exists.
85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?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'),
|
|
passwordHash: 'hashed:password1',
|
|
isAdmin: false,
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|