Add a trivial prefix-based PasswordHasher fake and inject it into the three test files that exercise CreateUser or AuthenticateUser. Drops the full phpunit suite from ~7.4s to ~30ms (about 224x) without losing coverage: the round-trip through hash/verify still validates that CreateUser stores something other than the plaintext and that AuthenticateUser only succeeds on a matching hash. CreateUserTest is also refactored to use a setUp method, matching the pattern already used in AuthenticateUserTest and AuthControllerTest.
94 lines
2.8 KiB
PHP
94 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\User\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\UnauthorizedException;
|
|
use App\User\UseCases\AuthenticateUser;
|
|
use App\User\UseCases\AuthenticateUserRequest;
|
|
use App\User\UseCases\CreateUser;
|
|
use App\User\UseCases\CreateUserRequest;
|
|
use App\User\User;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Tests\Fakes\FakePasswordHasher;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
|
|
class AuthenticateUserTest extends TestCase
|
|
{
|
|
private FakeUserRepository $userRepo;
|
|
private FakePasswordHasher $passwordHasher;
|
|
private AuthenticateUser $useCase;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->userRepo = new FakeUserRepository();
|
|
$this->passwordHasher = new FakePasswordHasher();
|
|
$createUser = new CreateUser(
|
|
$this->userRepo,
|
|
$this->passwordHasher,
|
|
);
|
|
$createUser->execute(new CreateUserRequest(
|
|
email: 'test@test.com',
|
|
password: 'password1',
|
|
));
|
|
$this->useCase = new AuthenticateUser(
|
|
$this->userRepo,
|
|
$this->passwordHasher,
|
|
);
|
|
}
|
|
|
|
public function test_returns_user_on_valid_credentials(): void
|
|
{
|
|
$user = $this->useCase->execute(new AuthenticateUserRequest(
|
|
email: 'test@test.com',
|
|
password: 'password1',
|
|
));
|
|
|
|
$this->assertInstanceOf(User::class, $user);
|
|
$this->assertEquals('test@test.com', (string) $user->getEmail());
|
|
}
|
|
|
|
public function test_throws_bad_request_when_email_null(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('email is required');
|
|
|
|
$this->useCase->execute(new AuthenticateUserRequest(
|
|
email: null,
|
|
password: 'password1',
|
|
));
|
|
}
|
|
|
|
public function test_throws_bad_request_when_password_null(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('password is required');
|
|
|
|
$this->useCase->execute(new AuthenticateUserRequest(
|
|
email: 'test@test.com',
|
|
password: null,
|
|
));
|
|
}
|
|
|
|
public function test_throws_unauthorized_on_wrong_password(): void
|
|
{
|
|
$this->expectException(UnauthorizedException::class);
|
|
$this->expectExceptionMessage('invalid credentials');
|
|
|
|
$this->useCase->execute(new AuthenticateUserRequest(
|
|
email: 'test@test.com',
|
|
password: 'wrongpassword',
|
|
));
|
|
}
|
|
|
|
public function test_throws_unauthorized_when_email_not_found(): void
|
|
{
|
|
$this->expectException(UnauthorizedException::class);
|
|
$this->expectExceptionMessage('invalid credentials');
|
|
|
|
$this->useCase->execute(new AuthenticateUserRequest(
|
|
email: 'missing@test.com',
|
|
password: 'password1',
|
|
));
|
|
}
|
|
}
|