98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Application\Security\Usecases\RegenerateClientSecret;
|
|
|
|
use Application\Security\Exceptions\ClientNotFoundException;
|
|
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\Ports\Services\ClientCredentialGenerator;
|
|
use Application\Security\Usecases\RegenerateClientSecret\Input;
|
|
use Application\Security\Usecases\RegenerateClientSecret\Output;
|
|
use Application\Security\Usecases\RegenerateClientSecret\RegenerateClientSecret;
|
|
use Infra\Adapters\Repositories\InMemoryClientRepository;
|
|
use Mockery;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\DDD\Event\DomainEventDispatcher;
|
|
|
|
covers(
|
|
RegenerateClientSecret::class,
|
|
Input::class,
|
|
Output::class,
|
|
);
|
|
|
|
describe('RegenerateClientSecret Usecase Unit Tests', function (): void {
|
|
$repository = new InMemoryClientRepository();
|
|
|
|
beforeEach(function () use ($repository): void {
|
|
$repository->clear();
|
|
});
|
|
|
|
it('executes successfully and returns new credentials', function () use ($repository): void {
|
|
// GIVEN
|
|
$credentialGenerator = Mockery::mock(ClientCredentialGenerator::class);
|
|
$dispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
$usecase = new RegenerateClientSecret(
|
|
$repository,
|
|
$credentialGenerator,
|
|
$dispatcher
|
|
);
|
|
|
|
$clientId = new ClientId(Uuid::uuid7());
|
|
$input = new Input($clientId);
|
|
$clientIdentifier = new ClientIdentifier('arcadia_id_' . str_repeat('a', 32));
|
|
$oldSecret = new ClientSecret('arcadia_sec_' . str_repeat('b', 86));
|
|
$newSecret = new ClientSecret('arcadia_sec_' . str_repeat('c', 86));
|
|
|
|
$client = Client::hydrate(
|
|
id: $clientId,
|
|
name: new ClientName('Test Client'),
|
|
identifier: $clientIdentifier,
|
|
secret: $oldSecret,
|
|
isActive: true,
|
|
updatedAt: new \DateTimeImmutable(),
|
|
);
|
|
|
|
$repository->add($client);
|
|
|
|
$credentialGenerator->allows('generateSecret')
|
|
->andReturn($newSecret);
|
|
|
|
$dispatcher->allows('dispatch');
|
|
|
|
// WHEN
|
|
$result = $usecase->execute($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(Output::class)
|
|
->and($result->newSecret)->toBe($newSecret)
|
|
->and($result->clientIdentifier)->toBe($clientIdentifier)
|
|
->and($repository->find($clientId)?->secret)->toBe($newSecret);
|
|
});
|
|
|
|
it('throws ClientNotFoundException when client does not exist', function () use ($repository): void {
|
|
// GIVEN
|
|
$credentialGenerator = Mockery::mock(ClientCredentialGenerator::class);
|
|
$dispatcher = Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
$usecase = new RegenerateClientSecret(
|
|
$repository,
|
|
$credentialGenerator,
|
|
$dispatcher
|
|
);
|
|
|
|
$clientId = new ClientId(Uuid::uuid7());
|
|
$input = new Input($clientId);
|
|
|
|
// WHEN
|
|
$execution = fn () => $usecase->execute($input);
|
|
|
|
// THEN
|
|
expect($execution)->toThrow(ClientNotFoundException::class);
|
|
});
|
|
});
|