ldc-arcadia-api/tests/Feature/Console/Commands/CreateClientCommandTest.php

114 lines
4.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Tests\Feature\Console\Commands;
use Infra\Database\Models\ApiClient;
use Presentation\Console\Commands\CreateClientCommand;
covers(CreateClientCommand::class);
describe('CreateClientCommand', function (): void {
it('creates a client through the wizard', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$clientName = 'Production Gateway';
// WHEN
$this->artisan('client:create')
// THEN
->expectsQuestion('Client name (e.g. "Ones Branding website")', $clientName)
->expectsConfirmation('Create this API client?', 'yes')
->expectsOutputToContain("API client '{$clientName}' created successfully.")
->expectsOutputToContain('Store the secret securely')
->assertSuccessful();
$client = ApiClient::query()->where('name', $clientName)->first();
expect($client)->not->toBeNull()
->and($client->is_active)->toBeTrue()
->and($client->name->value)->toBe($clientName)
->and((string) $client->identifier->value)->toStartWith('arcadia_id_')
->and((string) $client->secret->value)->toStartWith('arcadia_sec_');
});
it('aborts when user cancels confirmation', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$clientName = 'Cancelled Gateway';
// WHEN
$this->artisan('client:create')
// THEN
->expectsQuestion('Client name (e.g. "Ones Branding website")', $clientName)
->expectsConfirmation('Create this API client?', 'no')
->expectsOutput('Client creation cancelled.')
->assertSuccessful();
$client = ApiClient::query()->where('name', $clientName)->first();
expect($client)->toBeNull();
});
it('retries when an empty name is provided', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$this->artisan('client:create')
->expectsQuestion('Client name (e.g. "Ones Branding website")', '')
->expectsOutput('Client name cannot be empty.')
->expectsQuestion('Client name (e.g. "Ones Branding website")', 'Valid Gateway')
// WHEN
->expectsConfirmation('Create this API client?', 'yes')
->expectsOutputToContain("API client 'Valid Gateway' created successfully.")
// THEN
->assertSuccessful();
$client = ApiClient::query()
->where('name', 'Valid Gateway')
->first();
expect($client)->not->toBeNull();
});
it('fails after maximum retries with invalid names', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$this->artisan('client:create')
// WHEN
->expectsQuestion('Client name (e.g. "Ones Branding website")', '')
->expectsOutput('Client name cannot be empty.')
->expectsQuestion('Client name (e.g. "Ones Branding website")', '')
->expectsOutput('Client name cannot be empty.')
->expectsQuestion('Client name (e.g. "Ones Branding website")', '')
->expectsOutput('Client name cannot be empty.')
->expectsOutput('Maximum number of attempts reached. Aborting.')
->assertFailed();
});
it('retries when the client name already exists', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
ApiClient::factory()->create([
'name' => 'Existing Gateway',
'slug' => 'existing-gateway',
]);
// WHEN
$this->artisan('client:create')
->expectsQuestion('Client name (e.g. "Ones Branding website")', 'Existing Gateway')
->expectsOutput("A client with the name 'Existing Gateway' (or similar) already exists.")
->expectsQuestion('Client name (e.g. "Ones Branding website")', 'New Gateway')
->expectsConfirmation('Create this API client?', 'yes')
// THEN
->expectsOutputToContain("API client 'New Gateway' created successfully.")
->assertSuccessful();
$client = ApiClient::query()->where('name', 'New Gateway')->first();
expect($client)->not->toBeNull();
});
});