ldc-arcadia-api/tests/Unit/Application/Security/Usecases/FindClient/FindClientTest.php

91 lines
3.5 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\Application\Security\Usecases\FindClient;
use Application\Security\Models\Entities\Client;
use Application\Security\Models\ValueObjects\ClientIdentifier;
use Application\Security\Models\ValueObjects\ClientName;
use Application\Security\Models\ValueObjects\ClientSecret;
use Application\Security\Models\ValueObjects\Entities\ClientId;
use Application\Security\Usecases\FindClient\FindClient;
use Application\Security\Usecases\FindClient\Input;
use Application\Security\Usecases\FindClient\Output;
use Infra\Adapters\Repositories\InMemoryClientRepository;
use Ramsey\Uuid\Uuid;
use Shared\Paginated;
use Shared\Pagination;
covers(
FindClient::class,
Input::class,
Output::class,
);
describe('FindClient Usecase Unit Tests', function (): void {
$repository = new InMemoryClientRepository();
beforeEach(function () use ($repository): void {
$repository->clear();
});
it('searches and finds clients matching query with pagination', function () use ($repository): void {
// GIVEN
$client1 = Client::hydrate(
id: new ClientId(Uuid::uuid7()),
name: new ClientName('Production Gateway'),
identifier: new ClientIdentifier('arcadia_id_' . str_repeat('a', 32)),
secret: new ClientSecret('arcadia_sec_' . str_repeat('1', 86)),
isActive: true,
updatedAt: new \DateTimeImmutable(),
);
$client2 = Client::hydrate(
id: new ClientId(Uuid::uuid7()),
name: new ClientName('Staging Hub'),
identifier: new ClientIdentifier('arcadia_id_' . str_repeat('b', 32)),
secret: new ClientSecret('arcadia_sec_' . str_repeat('2', 86)),
isActive: false,
updatedAt: new \DateTimeImmutable(),
);
$repository->add($client1, $client2);
$usecase = new FindClient($repository);
// WHEN
$resultByName = $usecase->execute(new Input('production', Pagination::ofOffsetAndLimit(0, 10)));
$resultByIdentifier = $usecase->execute(new Input('arcadia_id_' . str_repeat('b', 32)));
$resultBySlug = $usecase->execute(new Input('staging-hub'));
// THEN
expect($resultByName)->toBeInstanceOf(Output::class)
->and($resultByName->clients)->toBeInstanceOf(Paginated::class)
->and($resultByName->clients->getTotal())->toBe(1)
->and($resultByName->clients->getItems()[0]->name->value)->toBe('Production Gateway')
->and($resultByIdentifier->clients->getTotal())->toBe(1)
->and($resultByIdentifier->clients->getItems()[0]->name->value)->toBe('Staging Hub')
->and($resultBySlug->clients->getTotal())->toBe(1)
->and($resultBySlug->clients->getItems()[0]->name->value)->toBe('Staging Hub');
});
it('throws InvalidArgumentException when query is empty', function (): void {
// GIVEN & WHEN & THEN
expect(fn () => new Input(' '))
->toThrow(\InvalidArgumentException::class, 'Search query cannot be empty.');
});
it('returns empty paginated result when no clients match', function () use ($repository): void {
// GIVEN
$usecase = new FindClient($repository);
// WHEN
$result = $usecase->execute(new Input('non-existent'));
// THEN
expect($result->clients)->toBeInstanceOf(Paginated::class)
->and($result->clients->getTotal())->toBe(0)
->and($result->clients->getItems())->toBeEmpty();
});
});