test: add failing authenticate user test
Red phase: write AuthenticateUserTest with cases for valid credentials, empty email/password (null and empty string), unknown email, wrong password, and fresh instance guarantee. Fakes included.
This commit is contained in:
parent
9d9771de6e
commit
57c75f64c4
9 changed files with 2175 additions and 2 deletions
|
|
@ -20,5 +20,8 @@
|
|||
"slim/slim": "4.*",
|
||||
"slim/psr7": "^1.8",
|
||||
"php-di/slim-bridge": "^3.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^13.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1855
backend/composer.lock
generated
1855
backend/composer.lock
generated
File diff suppressed because it is too large
Load diff
19
backend/phpunit.xml
Normal file
19
backend/phpunit.xml
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
cacheDirectory=".phpunit.cache"
|
||||
executionOrder="depends,defects"
|
||||
|
||||
colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="unit">
|
||||
<directory>tests/Unit</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<source>
|
||||
<include>
|
||||
<directory>app</directory>
|
||||
</include>
|
||||
</source>
|
||||
</phpunit>
|
||||
21
backend/tests/Fakes/FakeClock.php
Normal file
21
backend/tests/Fakes/FakeClock.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class FakeClock implements Clock
|
||||
{
|
||||
public function __construct(private DateTimeImmutable $currentTime) {}
|
||||
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return $this->currentTime;
|
||||
}
|
||||
|
||||
public function setTime(DateTimeImmutable $newTime): void
|
||||
{
|
||||
$this->currentTime = $newTime;
|
||||
}
|
||||
}
|
||||
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\PasswordHasher;
|
||||
|
||||
class FakePasswordHasher implements PasswordHasher
|
||||
{
|
||||
public function hash(string $password): string
|
||||
{
|
||||
return 'hashed:'.$password;
|
||||
}
|
||||
|
||||
public function verify(string $password, string $hash): bool
|
||||
{
|
||||
return $this->hash($password) === $hash;
|
||||
}
|
||||
}
|
||||
46
backend/tests/Fakes/FakeSessionRepository.php
Normal file
46
backend/tests/Fakes/FakeSessionRepository.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\CreateSessionDto;
|
||||
use App\Auth\Session;
|
||||
use App\Auth\SessionRepository;
|
||||
|
||||
class FakeSessionRepository implements SessionRepository
|
||||
{
|
||||
private array $sessions = [];
|
||||
|
||||
public function create(CreateSessionDto $dto): Session
|
||||
{
|
||||
$session = new Session(
|
||||
token: $dto->token,
|
||||
user: $dto->user,
|
||||
createdAt: $dto->createdAt,
|
||||
expiresAt: $dto->expiresAt,
|
||||
);
|
||||
$this->sessions[$dto->token] = $session;
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
public function findByToken(string $token): ?Session
|
||||
{
|
||||
$session = $this->sessions[$token] ?? null;
|
||||
|
||||
if ($session === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Session(
|
||||
token: $session->getToken(),
|
||||
user: $session->getUser(),
|
||||
createdAt: $session->getCreatedAt(),
|
||||
expiresAt: $session->getExpiresAt(),
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteByToken(string $token): void
|
||||
{
|
||||
unset($this->sessions[$token]);
|
||||
}
|
||||
}
|
||||
26
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
26
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\TokenGenerator;
|
||||
use RuntimeException;
|
||||
|
||||
class FakeTokenGenerator implements TokenGenerator
|
||||
{
|
||||
private int $callCount = 0;
|
||||
|
||||
public function __construct(private array $tokens) {}
|
||||
|
||||
public function generate(): string
|
||||
{
|
||||
if ($this->callCount >= count($this->tokens)) {
|
||||
throw new RuntimeException(
|
||||
'FakeTokenGenerator exhausted'
|
||||
);
|
||||
}
|
||||
$token = $this->tokens[$this->callCount];
|
||||
$this->callCount++;
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
63
backend/tests/Fakes/FakeUserRepository.php
Normal file
63
backend/tests/Fakes/FakeUserRepository.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\CreateUserDto;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
|
||||
class FakeUserRepository implements UserRepository
|
||||
{
|
||||
private array $users = [];
|
||||
|
||||
public function create(CreateUserDto $dto): User
|
||||
{
|
||||
$id = $this->nextId();
|
||||
|
||||
$user = new User(
|
||||
id: $id,
|
||||
email: $dto->email,
|
||||
passwordHash: $dto->passwordHash,
|
||||
);
|
||||
|
||||
$this->users[$id] = $user;
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User
|
||||
{
|
||||
foreach ($this->users as $user) {
|
||||
if ($user->getEmail()->value() === $email->value()) {
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function find(int $id): ?User
|
||||
{
|
||||
$user = $this->users[$id] ?? null;
|
||||
|
||||
if ($user === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new User(
|
||||
id: $user->getId(),
|
||||
email: $user->getEmail(),
|
||||
passwordHash: $user->getPasswordHash(),
|
||||
);
|
||||
}
|
||||
|
||||
private function nextId(): int
|
||||
{
|
||||
return count($this->users);
|
||||
}
|
||||
}
|
||||
126
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
126
backend/tests/Unit/Auth/UseCases/AuthenticateUserTest.php
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Auth\UseCases;
|
||||
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\CreateUserDto;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tests\Fakes\FakePasswordHasher;
|
||||
use Tests\Fakes\FakeUserRepository;
|
||||
|
||||
class AuthenticateUserTest extends TestCase
|
||||
{
|
||||
private FakeUserRepository $userRepo;
|
||||
private FakePasswordHasher $hasher;
|
||||
private AuthenticateUser $useCase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->userRepo = new FakeUserRepository();
|
||||
$this->hasher = new FakePasswordHasher();
|
||||
$this->useCase = new AuthenticateUser(
|
||||
$this->userRepo,
|
||||
$this->hasher,
|
||||
);
|
||||
|
||||
$this->userRepo->create(new CreateUserDto(
|
||||
email: new EmailAddress('user@example.com'),
|
||||
passwordHash: $this->hasher->hash('correct-password'),
|
||||
));
|
||||
}
|
||||
|
||||
public function testAuthenticatesWithValidCredentials(): void
|
||||
{
|
||||
$user = $this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'correct-password',
|
||||
));
|
||||
|
||||
$this->assertSame('user@example.com', $user->getEmail()->value());
|
||||
}
|
||||
|
||||
public function testThrowsBadRequestWhenEmailIsEmpty(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('email is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: '',
|
||||
password: 'some-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsBadRequestWhenPasswordIsEmpty(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('password is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: '',
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsBadRequestWhenEmailIsNull(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('email is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: null,
|
||||
password: 'some-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsBadRequestWhenPasswordIsNull(): void
|
||||
{
|
||||
$this->expectException(BadRequestException::class);
|
||||
$this->expectExceptionMessage('password is required');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: null,
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsUnauthorizedForUnknownEmail(): void
|
||||
{
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->expectExceptionMessage('invalid credentials');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'unknown@example.com',
|
||||
password: 'some-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function testThrowsUnauthorizedForWrongPassword(): void
|
||||
{
|
||||
$this->expectException(UnauthorizedException::class);
|
||||
$this->expectExceptionMessage('invalid credentials');
|
||||
|
||||
$this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'wrong-password',
|
||||
));
|
||||
}
|
||||
|
||||
public function testReturnsNewInstanceOnEachCall(): void
|
||||
{
|
||||
$user1 = $this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'correct-password',
|
||||
));
|
||||
|
||||
$user2 = $this->useCase->execute(new AuthenticateUserRequest(
|
||||
email: 'user@example.com',
|
||||
password: 'correct-password',
|
||||
));
|
||||
|
||||
$this->assertNotSame($user1, $user2);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue