93 lines
3.3 KiB
PHP
93 lines
3.3 KiB
PHP
|
|
<?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. "Production Gateway")', $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. "Production Gateway")', $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. "Production Gateway")', '')
|
||
|
|
->expectsOutput('Client name cannot be empty.')
|
||
|
|
->expectsQuestion('Client name (e.g. "Production Gateway")', '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. "Production Gateway")', '')
|
||
|
|
->expectsOutput('Client name cannot be empty.')
|
||
|
|
->expectsQuestion('Client name (e.g. "Production Gateway")', '')
|
||
|
|
->expectsOutput('Client name cannot be empty.')
|
||
|
|
->expectsQuestion('Client name (e.g. "Production Gateway")', '')
|
||
|
|
->expectsOutput('Client name cannot be empty.')
|
||
|
|
->expectsOutput('Maximum number of attempts reached. Aborting.')
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
->assertFailed();
|
||
|
|
});
|
||
|
|
});
|