2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
|
|
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
2026-10-03 10:51:42 +02:00
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
use Ramsey\Uuid\UuidInterface;
|
2026-10-27 16:09:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
describe('OrderReference', function () {
|
2026-10-03 10:51:42 +02:00
|
|
|
function getNewOrderReference(UuidInterface $value): OrderReference
|
2026-06-14 21:32:07 +02:00
|
|
|
{
|
|
|
|
|
return new OrderReference($value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 10:51:42 +02:00
|
|
|
it('can instantiate with a valid UUID v7', function () {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-10-03 10:51:42 +02:00
|
|
|
$uuidV7 = Uuid::uuid7();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-03 10:51:42 +02:00
|
|
|
$vo = getNewOrderReference($uuidV7);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-10-27 16:09:39 +01:00
|
|
|
expect($vo)->toBeInstanceOf(OrderReference::class)
|
|
|
|
|
->and((string) $vo)->toBe(UuidHelper::toBase58($uuidV7))
|
|
|
|
|
->and($vo->value)->toBe($uuidV7);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can instantiate from a valid base-58 UUID v7 string', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$uuidV7 = Uuid::uuid7();
|
|
|
|
|
$base58 = UuidHelper::toBase58($uuidV7);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$vo = OrderReference::fromBase58($base58);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($vo)->toBeInstanceOf(OrderReference::class)
|
|
|
|
|
->and((string) $vo)->toBe($base58)
|
|
|
|
|
->and($vo->value->equals($uuidV7))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can instantiate from a valid UUID v7 string via fromString', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$uuidV7 = Uuid::uuid7();
|
|
|
|
|
$uuidString = $uuidV7->toString();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$vo = OrderReference::fromString($uuidString);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($vo)->toBeInstanceOf(OrderReference::class)
|
|
|
|
|
->and((string) $vo)->toBe(UuidHelper::toBase58($uuidV7))
|
|
|
|
|
->and($vo->value->equals($uuidV7))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can instantiate from a valid base-58 string via fromString', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$uuidV7 = Uuid::uuid7();
|
|
|
|
|
$base58 = UuidHelper::toBase58($uuidV7);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$vo = OrderReference::fromString($base58);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($vo)->toBeInstanceOf(OrderReference::class)
|
|
|
|
|
->and((string) $vo)->toBe($base58)
|
|
|
|
|
->and($vo->value->equals($uuidV7))->toBeTrue();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if provided UUID is not v7', function () {
|
|
|
|
|
// GIVEN
|
2026-10-03 10:51:42 +02:00
|
|
|
$uuidV4 = Uuid::uuid4();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-03 10:51:42 +02:00
|
|
|
$action = fn () => getNewOrderReference($uuidV4);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-10-03 10:51:42 +02:00
|
|
|
expect($action)->toThrow(\DomainException::class, 'value must be a valid UUID v7');
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
2026-10-27 16:09:39 +01:00
|
|
|
|
|
|
|
|
it('throws exception if base-58 string is invalid', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$invalidBase58 = 'invalid-base58-string!';
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => OrderReference::fromBase58($invalidBase58);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Order reference must be a valid base-58 string');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if base-58 string is not from a UUID v7', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$uuidV4 = Uuid::uuid4();
|
|
|
|
|
$base58 = UuidHelper::toBase58($uuidV4);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => OrderReference::fromBase58($base58);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'value must be a valid UUID v7');
|
|
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|