78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\User\UseCases;
|
|
|
|
use App\Auth\PasswordHasher;
|
|
use App\Shared\ValueObject\EmailAddress;
|
|
use App\User\CreateUserDto;
|
|
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdmin;
|
|
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdminRequest;
|
|
use Tests\Fakes\FakeHasher;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
use Tests\TestCase;
|
|
|
|
class EnsureInitialAdminTest extends TestCase
|
|
{
|
|
private FakeUserRepository $userRepository;
|
|
|
|
private PasswordHasher $passwordHasher;
|
|
|
|
private EnsureInitialAdmin $ensureInitialAdmin;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->userRepository = new FakeUserRepository();
|
|
$this->passwordHasher = new FakeHasher();
|
|
$this->ensureInitialAdmin = new EnsureInitialAdmin(
|
|
$this->userRepository,
|
|
$this->passwordHasher,
|
|
);
|
|
}
|
|
|
|
public function testCreatesInitialAdminWhenEmailIsMissing(): void
|
|
{
|
|
$this->ensureInitialAdmin->execute(
|
|
new EnsureInitialAdminRequest(
|
|
email: 'admin@example.com',
|
|
password: 'secret-password',
|
|
)
|
|
);
|
|
|
|
$createdUser = $this->userRepository->findByEmail(
|
|
new EmailAddress('admin@example.com')
|
|
);
|
|
|
|
$this->assertNotNull($createdUser);
|
|
$this->assertSame(
|
|
'hashed-secret-password',
|
|
$createdUser->getPasswordHash(),
|
|
);
|
|
}
|
|
|
|
public function testLeavesExistingAdminPasswordUnchanged(): void
|
|
{
|
|
$this->userRepository->create(
|
|
new CreateUserDto(
|
|
email: new EmailAddress('admin@example.com'),
|
|
passwordHash: 'existing-password-hash',
|
|
)
|
|
);
|
|
|
|
$this->ensureInitialAdmin->execute(
|
|
new EnsureInitialAdminRequest(
|
|
email: 'admin@example.com',
|
|
password: 'new-secret-password',
|
|
)
|
|
);
|
|
|
|
$existingUser = $this->userRepository->findByEmail(
|
|
new EmailAddress('admin@example.com')
|
|
);
|
|
|
|
$this->assertNotNull($existingUser);
|
|
$this->assertSame(
|
|
'existing-password-hash',
|
|
$existingUser->getPasswordHash(),
|
|
);
|
|
}
|
|
}
|