test admin command

This commit is contained in:
Yisroel Baum 2026-07-02 01:18:28 +03:00
parent 4ee20396e8
commit 4462879b7d
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\User\UseCases;
use App\Console\Commands\EnsureInitialAdminCommand;
use App\Shared\ValueObject\EmailAddress;
use App\User\InitialAdminCredentials;
use App\User\InitialAdminCredentialsProvider;
use App\User\UseCases\EnsureInitialAdmin\EnsureInitialAdmin;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Fakes\FakeHasher;
use Tests\Fakes\FakeUserRepository;
use Tests\TestCase;
class EnsureInitialAdminCommandTest extends TestCase
{
public function testCommandCreatesInitialAdminFromProvider(): void
{
$userRepository = new FakeUserRepository();
$passwordHasher = new FakeHasher();
$command = new EnsureInitialAdminCommand(
new EnsureInitialAdmin($userRepository, $passwordHasher),
$this->credentialsProvider(
'admin@example.com',
'secret-password',
),
);
$commandTester = new CommandTester($command);
$statusCode = $commandTester->execute([]);
$createdUser = $userRepository->findByEmail(
new EmailAddress('admin@example.com')
);
$this->assertSame(0, $statusCode);
$this->assertNotNull($createdUser);
$this->assertSame(
'hashed-secret-password',
$createdUser->getPasswordHash(),
);
$this->assertStringContainsString(
'initial admin is ready',
$commandTester->getDisplay(),
);
}
private function credentialsProvider(
string $email,
string $password,
): InitialAdminCredentialsProvider {
return new class ($email, $password) implements
InitialAdminCredentialsProvider
{
public function __construct(
private string $email,
private string $password,
) {
}
public function getCredentials(): InitialAdminCredentials
{
return new InitialAdminCredentials(
email: $this->email,
password: $this->password,
);
}
};
}
}