77 lines
2 KiB
PHP
77 lines
2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Shared\Helpers;
|
|
|
|
use Ramsey\Uuid\Lazy\LazyUuidFromString;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\DDD\Exceptions\InvalidUuidException;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
describe('uuid helper', function (): void {
|
|
it('check is a v7 uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
|
|
// WHEN
|
|
$version = UuidHelper::version($uuid);
|
|
|
|
// THEN
|
|
expect($version)->toBe(7);
|
|
});
|
|
|
|
it('validates a v7 uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
|
|
// WHEN / THEN
|
|
expect(UuidHelper::isV7($uuid))->toBeTrue();
|
|
});
|
|
|
|
it('rejects a non v7 uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid4();
|
|
|
|
// WHEN / THEN
|
|
expect(UuidHelper::isV7($uuid))->toBeFalse();
|
|
});
|
|
|
|
it('returns the timestamp from a v7 uuid', function (): void {
|
|
// GIVEN
|
|
$expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC'));
|
|
$uuid = Uuid::uuid7($expected);
|
|
|
|
// WHEN
|
|
$result = UuidHelper::toDateTime($uuid);
|
|
|
|
// THEN
|
|
expect($result->getTimezone()->getName())->toBe('UTC')
|
|
->and($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v'));
|
|
});
|
|
|
|
it('throws for a non-v7 uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid4();
|
|
|
|
// WHEN / THEN
|
|
expect(fn (): \DateTimeImmutable => UuidHelper::toDateTime($uuid))
|
|
->toThrow(InvalidUuidException::class);
|
|
});
|
|
|
|
it('does not throw for a valid uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7();
|
|
|
|
// WHEN / THEN
|
|
UuidHelper::validateUuid($uuid);
|
|
})->throwsNoExceptions();
|
|
|
|
it('throws for an invalid uuid', function (): void {
|
|
// GIVEN
|
|
$uuid = new LazyUuidFromString('invalid-uuid');
|
|
|
|
// WHEN / THEN
|
|
expect(fn () => UuidHelper::validateUuid($uuid))->toThrow(InvalidUuidException::class);
|
|
});
|
|
});
|