Goal-Calibration/tests/Unit/User/UseCases/CreateUserTest.php
Yisroel Baum cd40483cd4
remove default values from user constructors
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.
2026-04-26 10:46:07 +03:00

124 lines
3.5 KiB
PHP

<?php
namespace Tests\Unit\User\UseCases;
use App\Exceptions\BadRequestException;
use App\User\User;
use App\User\UseCases\CreateUser;
use App\User\UseCases\CreateUserRequest;
use Tests\Fakes\FakePasswordHasher;
use Tests\Fakes\FakeUserRepository;
use PHPUnit\Framework\TestCase;
class CreateUserTest extends TestCase
{
private FakeUserRepository $userRepo;
private FakePasswordHasher $passwordHasher;
private CreateUser $useCase;
public function setUp(): void
{
$this->userRepo = new FakeUserRepository();
$this->passwordHasher = new FakePasswordHasher();
$this->useCase = new CreateUser(
$this->userRepo,
$this->passwordHasher,
);
}
public function test_create_user(): void
{
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'password1',
isAdmin: false,
));
$user = $this->userRepo->find(0);
$this->assertInstanceOf(User::class, $user);
$this->assertEquals('test@test.com', $user->getEmail());
}
public function test_throws_if_email_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('email is required');
$this->useCase->execute(new CreateUserRequest(
email: null,
password: 'password1',
isAdmin: false,
));
}
public function test_is_admin_can_be_set_true(): void
{
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'password1',
isAdmin: true,
));
$user = $this->userRepo->find(0);
$this->assertTrue($user->isAdmin());
}
public function test_throws_when_email_already_taken(): void
{
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'password1',
isAdmin: false,
));
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('email already taken');
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'password1',
isAdmin: false
));
}
public function test_throws_if_password_is_null(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('password is required');
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: null,
isAdmin: false,
));
}
public function test_throws_if_password_too_short(): void
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage(
'password must be at least 8 characters'
);
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'short',
isAdmin: false,
));
}
public function test_stores_hashed_password(): void
{
$this->useCase->execute(new CreateUserRequest(
email: 'test@test.com',
password: 'password1',
isAdmin: false,
));
$user = $this->userRepo->find(0);
$this->assertNotEquals('password1', $user->getPasswordHash());
$this->assertTrue(
$this->passwordHasher->verify(
'password1',
$user->getPasswordHash()
)
);
}
}