Refactor OrderReferenceCast to utilize base58 encoding for order references instead of UUID. This change improves compatibility with the new OrderReference implementation.
106 lines
3.1 KiB
PHP
106 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Ramsey\Uuid\UuidInterface;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
describe('OrderReference', function () {
|
|
function getNewOrderReference(UuidInterface $value): OrderReference
|
|
{
|
|
return new OrderReference($value);
|
|
}
|
|
|
|
it('can instantiate with a valid UUID v7', function () {
|
|
// GIVEN
|
|
$uuidV7 = Uuid::uuid7();
|
|
|
|
// WHEN
|
|
$vo = getNewOrderReference($uuidV7);
|
|
|
|
// THEN
|
|
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();
|
|
});
|
|
|
|
it('throws exception if provided UUID is not v7', function () {
|
|
// GIVEN
|
|
$uuidV4 = Uuid::uuid4();
|
|
|
|
// WHEN
|
|
$action = fn () => getNewOrderReference($uuidV4);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'value must be a valid UUID v7');
|
|
});
|
|
|
|
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');
|
|
});
|
|
});
|