Files
moonwell-web/tests/Feature/LauncherRegistrationFeatureTest.php
T

98 lines
3.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Exceptions\DuplicateGameAccountException;
use App\Services\AzerothCoreAccountRegistrar;
use App\Services\InviteCodeService;
use Closure;
use Mockery\MockInterface;
use Tests\TestCase;
class LauncherRegistrationFeatureTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['moonwell.registration.require_invite_code' => true]);
}
public function test_launcher_can_register_game_account(): void
{
$this->mock(InviteCodeService::class, function (MockInterface $mock): void {
$mock->shouldReceive('redeemForRegistration')
->once()
->with('INVITE-1234', 'playerone', \Mockery::type(Closure::class))
->andReturnUsing(fn (string $code, string $username, Closure $callback): array => $callback());
});
$this->mock(AzerothCoreAccountRegistrar::class, function (MockInterface $mock): void {
$mock->shouldReceive('register')
->once()
->with('playerone', 'player@example.com', 'Pass1234')
->andReturn(['id' => 77, 'username' => 'PLAYERONE']);
});
$this->postJson('/api/launcher/register', [
'username' => 'playerone',
'email' => 'player@example.com',
'invite_code' => 'INVITE-1234',
'password' => 'Pass1234',
'password_confirmation' => 'Pass1234',
'terms' => true,
])
->assertCreated()
->assertJson([
'message' => 'Игровой аккаунт успешно создан.',
'account' => [
'id' => 77,
'username' => 'PLAYERONE',
],
]);
}
public function test_launcher_registration_returns_validation_errors_as_json(): void
{
$this->postJson('/api/launcher/register', [
'username' => 'игрок!',
'email' => 'invalid',
'password' => '123',
'password_confirmation' => '456',
'terms' => false,
])
->assertUnprocessable()
->assertJsonValidationErrors([
'username',
'email',
'invite_code',
'password',
'terms',
]);
}
public function test_launcher_registration_reports_duplicate_username(): void
{
$this->mock(InviteCodeService::class, function (MockInterface $mock): void {
$mock->shouldReceive('redeemForRegistration')
->once()
->andReturnUsing(fn (string $code, string $username, Closure $callback): array => $callback());
});
$this->mock(AzerothCoreAccountRegistrar::class, function (MockInterface $mock): void {
$mock->shouldReceive('register')
->once()
->andThrow(DuplicateGameAccountException::forUsername());
});
$this->postJson('/api/launcher/register', [
'username' => 'playerone',
'email' => 'player@example.com',
'invite_code' => 'INVITE-1234',
'password' => 'Pass1234',
'password_confirmation' => 'Pass1234',
'terms' => true,
])
->assertUnprocessable()
->assertJsonPath('errors.username.0', 'Такой игровой аккаунт уже существует.');
}
}