107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Application\Security\Models\Entities;
|
|
|
|
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 Ramsey\Uuid\Uuid;
|
|
use Shared\DDD\Model\HasTimestampIdentifier;
|
|
|
|
covers(Client::class);
|
|
|
|
describe('Client Entity', function (): void {
|
|
it('creates a new client and derives createdAt from ClientId', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
$clientId = new ClientId($uuid);
|
|
$name = new ClientName('Test Client');
|
|
$identifier = new ClientIdentifier('arcadia_id_' . str_repeat('a', 32));
|
|
$secret = new ClientSecret('arcadia_sec_' . str_repeat('b', 86));
|
|
|
|
// WHEN
|
|
$client = Client::create(
|
|
id: $clientId,
|
|
name: $name,
|
|
identifier: $identifier,
|
|
secret: $secret,
|
|
);
|
|
|
|
// THEN
|
|
expect($client)->toBeInstanceOf(HasTimestampIdentifier::class)
|
|
->and($client->id())->toBe($clientId)
|
|
->and($client->name)->toBe($name)
|
|
->and($client->identifier)->toBe($identifier)
|
|
->and($client->secret)->toBe($secret)
|
|
->and($client->isActive)->toBeTrue()
|
|
->and($client->createdAt())->toBeInstanceOf(\DateTimeInterface::class)
|
|
->and($client->createdAt()->getTimestamp())->toBeLessThanOrEqual(time());
|
|
});
|
|
|
|
it('hydrates a client without stored createdAt', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
$clientId = new ClientId($uuid);
|
|
$name = new ClientName('Hydrated Client');
|
|
$identifier = new ClientIdentifier('arcadia_id_' . str_repeat('c', 32));
|
|
$secret = new ClientSecret('arcadia_sec_' . str_repeat('d', 86));
|
|
$updatedAt = new \DateTimeImmutable('2026-01-01 12:00:00');
|
|
|
|
// WHEN
|
|
$client = Client::hydrate(
|
|
id: $clientId,
|
|
name: $name,
|
|
identifier: $identifier,
|
|
secret: $secret,
|
|
isActive: false,
|
|
updatedAt: $updatedAt,
|
|
);
|
|
|
|
// THEN
|
|
expect($client->id())->toBe($clientId)
|
|
->and($client->isActive)->toBeFalse()
|
|
->and($client->updatedAt)->toBe($updatedAt)
|
|
->and($client->createdAt())->toBeInstanceOf(\DateTimeInterface::class)
|
|
->and($client->createdAt()->getTimestamp())->toBeLessThanOrEqual(time());
|
|
});
|
|
|
|
it('regenerates secret and updates updatedAt', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
$client = Client::create(
|
|
id: new ClientId($uuid),
|
|
name: new ClientName('Test Client'),
|
|
identifier: new ClientIdentifier('arcadia_id_' . str_repeat('e', 32)),
|
|
secret: new ClientSecret('arcadia_sec_' . str_repeat('f', 86)),
|
|
);
|
|
$newSecret = new ClientSecret('arcadia_sec_' . str_repeat('g', 86));
|
|
|
|
// WHEN
|
|
$client->regenerateSecret($newSecret);
|
|
|
|
// THEN
|
|
expect($client->secret)->toBe($newSecret);
|
|
});
|
|
|
|
it('sets active status and updates updatedAt', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
$client = Client::create(
|
|
id: new ClientId($uuid),
|
|
name: new ClientName('Test Client'),
|
|
identifier: new ClientIdentifier('arcadia_id_' . str_repeat('h', 32)),
|
|
secret: new ClientSecret('arcadia_sec_' . str_repeat('i', 86)),
|
|
);
|
|
|
|
// WHEN
|
|
$result = $client->setActive(false);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse()
|
|
->and($client->isActive)->toBeFalse();
|
|
});
|
|
});
|