ldc-mesbr-app/tests/Unit/Shared/DDD/Models/Uuidv7EntityIdTest.php

62 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\DDD\Models;
use Ramsey\Uuid\Uuid;
use Shared\DDD\Models\Uuidv7EntityId;
covers(Uuidv7EntityId::class);
describe('uuidv7 entity id', function (): void {
it('should create a uuidv7 entity id', function (): void {
// GIVEN / WHEN
$uuid = Uuid::uuid7();
$id = new DummyUuidv7EntityId($uuid);
// THEN
expect($id->value)->toBe((string) $uuid);
});
it('can be casted to string', function (): void {
// GIVEN / WHEN
$uuid = Uuid::uuid7();
$id = new DummyUuidv7EntityId($uuid);
// THEN
expect((string) $id)->toBe((string) $uuid);
});
it('can be compared for equality', function (): void {
// GIVEN / WHEN
$uuid = Uuid::uuid7();
$id1 = new DummyUuidv7EntityId($uuid);
$id2 = new DummyUuidv7EntityId($uuid);
// THEN
expect($id1->equals($id2))->toBeTrue();
});
it('can be converted to a datetime', function (): void {
// GIVEN / WHEN
$uuid = Uuid::uuid7();
$id = new DummyUuidv7EntityId($uuid);
// THEN
expect($id->toDateTime())->toBeInstanceOf(\DateTimeImmutable::class);
});
});
describe('uuidv7 entity id validation rules', function (): void {
it('should throw an exception when non v7 UUID is used', function (): void {
// GIVEN
$uuid = Uuid::uuid4();
// THEN
$this->expectException(\DomainException::class);
// WHEN
new DummyUuidv7EntityId($uuid);
});
});