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

199 lines
7.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Console\Commands;
use Infra\Database\Models\ApiClient;
use Presentation\Console\Commands\ToggleClientStatusCommand;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Console\Tester\CommandCompletionTester;
covers(ToggleClientStatusCommand::class);
describe('ToggleClientStatusCommand', function (): void {
it('errors when an invalid identifier is provided', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$invalidId = 'not-a-valid-identifier';
// WHEN
$this->artisan('client:toggle-status', ['identifier' => $invalidId])
// THEN
->expectsOutput("Invalid client identifier '{$invalidId}'. Expected a valid client identifier.")
->assertFailed();
});
it('errors when client does not exist', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$nonExistentId = 'arcadia_id_' . str_repeat('9', 32);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => $nonExistentId])
// THEN
->expectsOutput("Client '{$nonExistentId}' not found.")
->assertFailed();
});
it('toggles an active client to inactive when no status argument is passed', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$client = ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Production Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('a', 32),
'secret' => 'arcadia_sec_' . str_repeat('b', 86),
'is_active' => true,
]);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => (string) $client->identifier->value])
// THEN
->expectsOutput("Client 'Production Gateway' (ID: {$client->id}) is now Inactive.")
->assertSuccessful();
$client->refresh();
expect($client->is_active)->toBeFalse();
});
it('toggles an inactive client to active when no status argument is passed', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$client = ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Backup Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('c', 32),
'secret' => 'arcadia_sec_' . str_repeat('d', 86),
'is_active' => false,
]);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => (string) $client->identifier->value])
// THEN
->expectsOutput("Client 'Backup Gateway' (ID: {$client->id}) is now Active.")
->assertSuccessful();
$client->refresh();
expect($client->is_active)->toBeTrue();
});
it('activates an inactive client when activate status is passed', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$client = ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Special Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('e', 32),
'secret' => 'arcadia_sec_' . str_repeat('f', 86),
'is_active' => false,
]);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => (string) $client->identifier->value, 'status' => 'activate'])
// THEN
->expectsOutput("Client 'Special Gateway' (ID: {$client->id}) is now Active.")
->assertSuccessful();
$client->refresh();
expect($client->is_active)->toBeTrue();
});
it('deactivates an active client when deactivate status is passed', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$client = ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Special Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('g', 32),
'secret' => 'arcadia_sec_' . str_repeat('h', 86),
'is_active' => true,
]);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => (string) $client->identifier->value, 'status' => 'deactivate'])
// THEN
->expectsOutput("Client 'Special Gateway' (ID: {$client->id}) is now Inactive.")
->assertSuccessful();
$client->refresh();
expect($client->is_active)->toBeFalse();
});
it('errors when an invalid status is passed', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
$identifier = 'arcadia_id_' . str_repeat('z', 32);
ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Some Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => $identifier,
'secret' => 'arcadia_sec_' . str_repeat('z', 86),
'is_active' => true,
]);
// WHEN
$this->artisan('client:toggle-status', ['identifier' => $identifier, 'status' => 'unknown'])
// THEN
->expectsOutput("Invalid status 'unknown'. Expected 'activate' or 'deactivate'.")
->assertFailed();
});
it('provides autocompletion suggestions for identifier', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
ApiClient::query()->delete();
ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Production Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('a', 32),
'secret' => 'arcadia_sec_' . str_repeat('b', 86),
'is_active' => true,
]);
ApiClient::query()->create([
'id' => (string) Uuid::uuid7(),
'name' => 'Backup Gateway',
'slug' => 'test-' . uniqid(),
'identifier' => 'arcadia_id_' . str_repeat('c', 32),
'secret' => 'arcadia_sec_' . str_repeat('d', 86),
'is_active' => false,
]);
/** @var ToggleClientStatusCommand $command */
$command = app(ToggleClientStatusCommand::class);
$tester = new CommandCompletionTester($command);
// WHEN
$suggestions = $tester->complete(['']);
// THEN
expect($suggestions)
->toContain('arcadia_id_' . str_repeat('a', 32))
->toContain('arcadia_id_' . str_repeat('c', 32));
});
it('provides autocompletion suggestions for status argument', function (): void {
// GIVEN
/** @var \Tests\TestCase $this */
/** @var ToggleClientStatusCommand $command */
$command = app(ToggleClientStatusCommand::class);
$tester = new CommandCompletionTester($command);
$someId = 'arcadia_id_' . str_repeat('1', 32);
// WHEN
$suggestions = $tester->complete([$someId, '']);
// THEN
expect($suggestions)
->toContain('activate')
->toContain('deactivate');
});
});