refactor(orders): simplify OrderReference to wrap UuidInterface directly and remove obsolete UuidConverter

This commit is contained in:
Jérémy Munsch 2026-10-03 10:51:42 +02:00
parent bae633e312
commit 5492ffe217
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
23 changed files with 87 additions and 234 deletions

View file

@ -6,6 +6,7 @@ namespace App\Casts;
use Domain\Models\ValueObjects\OrderReference;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Ramsey\Uuid\Uuid;
/**
* @implements CastsAttributes<OrderReference,OrderReference>
@ -19,17 +20,16 @@ class OrderReferenceCast implements CastsAttributes
throw new \UnexpectedValueException('OrderReference cannot be null.');
}
return new OrderReference($value);
return new OrderReference(Uuid::fromString($value));
}
/** @param array<string, mixed> $attributes */
public function set($model, string $key, $value, array $attributes): string
{
if (! $value instanceof OrderReference) {
throw new \UnexpectedValueException('The given value is not an instance of OrderReference.');
}
return $value->value;
return $value->value->toString();
}
}

View file

@ -143,7 +143,7 @@ class TunnelController extends Controller
return back()->with('message', $e->getMessage());
}
return to_route('fo.sales.confirmation.show-confirmed', $orderReference->value);
return to_route('fo.sales.confirmation.show-confirmed', (string) $orderReference);
}
// * -- CONFIRMATION -- *

View file

@ -17,7 +17,6 @@ use Domain\Ports\Services\CustomerAuthenticationService;
use Domain\Ports\Services\EmployeeImportStorageService;
use Domain\Ports\Services\EmployeeStatisticsService;
use Domain\Ports\Services\TransactionManager;
use Domain\Ports\Services\UuidConverter;
use Illuminate\Support\ServiceProvider;
use Infrastructure\Adapters\Directories\EloquentCodeDirectory;
use Infrastructure\Adapters\Directories\EloquentShopDirectory;
@ -33,7 +32,6 @@ use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Infrastructure\Adapters\Services\EloquentEmployeeImportStorageService;
use Infrastructure\Adapters\Services\EloquentEmployeeStatisticsService;
use Infrastructure\Adapters\Services\LaravelTransactionManager;
use Infrastructure\Adapters\Services\UuidConverterImpl;
use Shared\DDD\DomainEventDispatcher;
class DomainServiceProvider extends ServiceProvider
@ -113,11 +111,6 @@ class DomainServiceProvider extends ServiceProvider
EmployeeImportStorageService::class,
EloquentEmployeeImportStorageService::class
);
$this->app->bind(
UuidConverter::class,
UuidConverterImpl::class
);
}
/**

View file

@ -4,37 +4,22 @@ declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV7;
use Ramsey\Uuid\UuidInterface;
use Shared\Helpers\UuidHelper;
final readonly class OrderReference implements \Stringable
{
public function __construct(
public string $value
public UuidInterface $value
) {
$value = trim($value);
if (empty($value)) {
throw new \DomainException('Order reference cannot be empty');
}
if (! preg_match('/^[1-9A-HJ-NP-Za-km-z]{21,22}$/', $value)) {
throw new \DomainException('Order reference must be a valid base-58 string of 21 or 22 characters');
}
try {
$uuid = Uuid::fromBase58($value);
if (get_class($uuid) !== UuidV7::class) {
throw new \DomainException('Order reference must be generated from a UUID v7');
}
} catch (\InvalidArgumentException $e) {
throw new \DomainException('Order reference must be a valid base-58 string');
if (! UuidHelper::isV7($value)) {
throw new \DomainException('value must be a valid UUID v7');
}
}
#[\Override]
public function __toString(): string
{
return $this->value;
return $this->value->toString();
}
}

View file

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace Domain\Ports\Services;
use Ramsey\Uuid\UuidInterface;
interface UuidConverter
{
public function fromBase58(string $uuid): UuidInterface;
public function toBase58(UuidInterface $uuid): string;
}

View file

@ -19,13 +19,11 @@ use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\PickupTimeSlot;
use Domain\Models\ValueObjects\ProductWeight;
use Domain\Ports\Repositories\OrderRepository;
use Domain\Ports\Services\UuidConverter;
use Ramsey\Uuid\Uuid;
class OrderService
{
public function __construct(
private readonly UuidConverter $uuidConverter,
private readonly OrderRepository $orderRepository,
) {
}
@ -102,11 +100,7 @@ class OrderService
private function newReference(): OrderReference
{
return new OrderReference(
$this->uuidConverter->toBase58(
Uuid::uuid7()
)
);
return new OrderReference(Uuid::uuid7());
}
public function cancelOrder(OrderId $id): void

View file

@ -58,7 +58,7 @@ class RegateService
'',
$pickupDay->format('d/m/Y'),
(string) $line->quantity,
$reference->value,
(string) $reference,
]
);
}

View file

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Infrastructure\Adapters\Services;
use Domain\Ports\Services\UuidConverter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\Uuid as SymphonyUuid;
class UuidConverterImpl implements UuidConverter
{
public function fromBase58(string $uuid): UuidInterface
{
return Uuid::fromString(SymphonyUuid::fromBase58($uuid)->toString());
}
public function toBase58(UuidInterface $uuid): string
{
return SymphonyUuid::fromString($uuid->toString())->toBase58();
}
}

View file

@ -43,7 +43,7 @@ describe('Bo\OrdersController', function (): void {
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(2500, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000101'), 'Alice', 'Smith', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -70,7 +70,7 @@ describe('Bo\OrdersController', function (): void {
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(2500, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule((string) (1000100 + $i)), 'Alice', 'Smith', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -93,12 +93,13 @@ describe('Bo\OrdersController', function (): void {
it('can filter index by search term', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$ref = \Symfony\Component\Uid\Uuid::v7()->toBase58();
$shop = Shop::factory()->create(['name' => 'UniqueShopSearch']);
$refUuid = Uuid::uuid7();
$ref = $refUuid->toString();
$shop = Shop::factory()->create(['name' => 'UniqueShopSearch']);
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference($ref),
'reference' => new OrderReference($refUuid),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1500, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000102'), 'Bob', 'Jones', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -125,7 +126,7 @@ describe('Bo\OrdersController', function (): void {
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(3000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000103'), 'Charlie', 'Brown', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -172,7 +173,7 @@ describe('Bo\OrdersController', function (): void {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(4000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000999'), 'Jean', 'Dupont', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -196,7 +197,7 @@ describe('Bo\OrdersController', function (): void {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(2000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000104'), 'David', 'Clark', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -235,7 +236,7 @@ describe('Bo\OrdersController', function (): void {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::CANCELED->value,
'total' => new Money(2000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000105'), 'Eve', 'Stone', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),

View file

@ -115,12 +115,13 @@ describe('OrderController', function () {
'updated_at' => CarbonImmutable::now(),
]);
$shop = Shop::factory()->create();
$ref = \Symfony\Component\Uid\Uuid::v7()->toBase58();
$shop = Shop::factory()->create();
$refUuid = Uuid::uuid7();
$ref = $refUuid->toString();
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference($ref),
'reference' => new OrderReference($refUuid),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(5000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('8888001'), 'Jean', 'Dupont', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -196,12 +197,13 @@ describe('OrderController', function () {
'updated_at' => CarbonImmutable::now(),
]);
$shop = Shop::factory()->create();
$ref = \Symfony\Component\Uid\Uuid::v7()->toBase58();
$order = Order::forceCreate([
$shop = Shop::factory()->create();
$refUuid = Uuid::uuid7();
$ref = $refUuid->toString();
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'shop_id' => $shop->id,
'reference' => new OrderReference($ref),
'reference' => new OrderReference($refUuid),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(5000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('8888002'), 'Paul', 'Martin', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),

View file

@ -488,7 +488,7 @@ describe('TunnelController', function (): void {
]);
Gate::before(fn () => true);
$orderReference = new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$orderReference = new OrderReference(Uuid::uuid7());
$this->mock(Executor::class, function ($mock) use ($orderReference): void {
$mock->shouldReceive('execute')
->once()
@ -572,7 +572,7 @@ describe('TunnelController', function (): void {
$shop->default = true;
$shop->save();
$orderReference = new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$orderReference = new OrderReference(Uuid::uuid7());
$order = new Order();
$order->reference = $orderReference;
$order->total = Money::EUR(3000);

View file

@ -21,7 +21,7 @@ use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Money\Money;
use Symfony\Component\Uid\Uuid;
use Ramsey\Uuid\Uuid;
describe('GetTunnelOrderConfirmedQuery', function (): void {
it('returns order confirmed output with step, orderDto, shopDto and mapUrl', function (): void {
@ -34,7 +34,7 @@ describe('GetTunnelOrderConfirmedQuery', function (): void {
$shop->default = true;
$shop->save();
$reference = new OrderReference(Uuid::v7()->toBase58());
$reference = new OrderReference(Uuid::uuid7());
$order = new Order();
$order->reference = $reference;
@ -69,7 +69,7 @@ describe('GetTunnelOrderConfirmedQuery', function (): void {
expect($output)->toBeInstanceOf(GetTunnelOrderConfirmedQueryOutput::class)
->and($output->step)->toBe(OrderTunnelStep::CONFIRMATION)
->and($output->order)->toBeInstanceOf(OrderDto::class)
->and((string) $output->order->reference)->toBe($reference->value)
->and((string) $output->order->reference)->toBe((string) $reference)
->and($output->shop)->toBeInstanceOf(ShopDto::class)
->and($output->shop->name)->toBe('Boutique Retrait')
->and($output->mapUrl)->toBeString();
@ -77,7 +77,7 @@ describe('GetTunnelOrderConfirmedQuery', function (): void {
it('throws UnexpectedValueException when order has no shop', function (): void {
// GIVEN
$reference = new OrderReference(Uuid::v7()->toBase58());
$reference = new OrderReference(Uuid::uuid7());
$order = new Order();
$order->reference = $reference;

View file

@ -51,7 +51,7 @@ describe('OrderService', function () {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000301'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -91,7 +91,7 @@ describe('OrderService', function () {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000999'), 'Other', 'Person', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -117,7 +117,7 @@ describe('OrderService', function () {
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000999'), 'Other', 'Person', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),

View file

@ -76,7 +76,7 @@ describe('EloquentOrderDirectory', function () {
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000201'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -85,7 +85,7 @@ describe('EloquentOrderDirectory', function () {
Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(1000, new Currency('EUR')),
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -106,7 +106,7 @@ describe('EloquentOrderDirectory', function () {
$directory = new EloquentOrderDirectory();
$order = Order::forceCreate([
'id' => Uuid::uuid7()->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::VALIDATED->value,
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -157,7 +157,7 @@ describe('EloquentOrderDirectory', function () {
$orderId = Uuid::uuid7()->toString();
Order::forceCreate([
'id' => $orderId,
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::VALIDATED->value,
'employee' => new OrderEmployee(new Matricule('1000202'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -179,7 +179,7 @@ describe('EloquentOrderDirectory', function () {
$pastDate = CarbonImmutable::now()->subDays(5);
Order::forceCreate([
'id' => Uuid::uuid7($pastDate)->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::VALIDATED->value,
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -187,7 +187,7 @@ describe('EloquentOrderDirectory', function () {
]);
Order::forceCreate([
'id' => Uuid::uuid7($pastDate)->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::WAITING->value,
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -210,7 +210,7 @@ describe('EloquentOrderDirectory', function () {
$pastDate = CarbonImmutable::now()->subDays(5);
Order::forceCreate([
'id' => Uuid::uuid7($pastDate)->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::CANCELED->value,
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -252,7 +252,7 @@ describe('EloquentOrderDirectory', function () {
$pastDate = CarbonImmutable::now()->subDays(5);
Order::forceCreate([
'id' => Uuid::uuid7($pastDate)->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::VALIDATED->value,
'employee' => new OrderEmployee(new Matricule('1000203'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),
@ -275,7 +275,7 @@ describe('EloquentOrderDirectory', function () {
$pastDate = CarbonImmutable::now()->subDays(5);
$order = Order::forceCreate([
'id' => Uuid::uuid7($pastDate)->toString(),
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
'reference' => new OrderReference(Uuid::uuid7()),
'total' => new Money(1000, new Currency('EUR')),
'status' => OrderStatus::VALIDATED->value,
'employee' => new OrderEmployee(new Matricule('1000999'), 'John', 'Doe', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')),

View file

@ -26,7 +26,7 @@ describe('FinalizeOrder', function (): void {
// GIVEN
$input = new FinalizeOrderInput();
$orderReference = new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$orderReference = new OrderReference(Uuid::uuid7());
$builder = \Mockery::mock(OrderBuilderCompleted::class);
$order = new Order(
id: new OrderId(Uuid::uuid7()),

View file

@ -5,19 +5,22 @@ declare(strict_types=1);
use App\Casts\OrderReferenceCast;
use Domain\Models\ValueObjects\OrderReference;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;
it('gets a reference', function () {
// GIVEN
$cast = new OrderReferenceCast();
$model = \Mockery::mock(Model::class);
$value = '1Cbx1Nwz16buAGRpU3FX8F';
$uuid = Uuid::uuid7();
$value = $uuid->toString();
// WHEN
$reference = $cast->get($model, 'reference', $value, []);
// THEN
expect($reference)->toBeInstanceOf(OrderReference::class)
->and($reference->value)->toBe($value);
->and((string) $reference)->toBe($value)
->and($reference->value->equals($uuid))->toBeTrue();
});
it('throws exception when getting invalid reference', function () {
@ -46,7 +49,7 @@ it('throws exception when setting invalid reference type', function () {
// GIVEN
$cast = new OrderReferenceCast();
$model = \Mockery::mock(Model::class);
$value = '1Cbx1Nwz16buAGRpU3FX8F';
$value = Uuid::uuid7()->toString();
// WHEN
$cast->set($model, 'reference', $value, []);
@ -61,12 +64,12 @@ it('sets a reference', function () {
// GIVEN
$cast = new OrderReferenceCast();
$model = \Mockery::mock(Model::class);
$value = '1Cbx1Nwz16buAGRpU3FX8F';
$reference = new OrderReference($value);
$uuid = Uuid::uuid7();
$reference = new OrderReference($uuid);
// WHEN
$value = $cast->set($model, 'reference', $reference, []);
// THEN
expect($value)->toBe($value);
expect($value)->toBe($uuid->toString());
});

View file

@ -25,7 +25,7 @@ describe('AnOrderHasBeenPlaced', function () {
// GIVEN
$uuid = Uuid::uuid7();
$orderId = new OrderId($uuid);
$reference = new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$reference = new OrderReference(Uuid::uuid7());
$totalTtc = new Money(1000, new Currency('EUR'));
$matricule = new Matricule('1234567');
$employee = new OrderEmployee($matricule, 'John', 'Doe', new ShopperEstablishmentCode('ETAB01'), new ShopperSiteCode('SITEA'), new ShopperServiceCode('101'));
@ -47,7 +47,7 @@ describe('AnOrderHasBeenPlaced', function () {
// GIVEN
$uuid = Uuid::uuid7();
$orderId = new OrderId($uuid);
$reference = new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$reference = new OrderReference(Uuid::uuid7());
$totalTtc = new Money(1000, new Currency('EUR'));
$matricule = new Matricule('1234567');
$employee = new OrderEmployee($matricule, 'John', 'Doe', new ShopperEstablishmentCode('ETAB01'), new ShopperSiteCode('SITEA'), new ShopperServiceCode('101'));

View file

@ -26,7 +26,6 @@ use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\UuidV7;
function makeOrderLine(array $overrides = []): OrderLine
{
@ -62,7 +61,7 @@ function makeOrder(array $overrides = []): Order
id: $overrides['id'] ?? new OrderId(Uuid::uuid7()),
shopId: $overrides['shopId'] ?? new ShopId(Uuid::uuid7()),
reference: $overrides['reference'] ?? new OrderReference(
UuidV7::v7()->toBase58()
Uuid::uuid7()
),
employee: $overrides['employee'] ?? new OrderEmployee(
matricule: new Matricule('1234567'),

View file

@ -5,66 +5,36 @@ declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects;
use Domain\Models\ValueObjects\OrderReference;
use Symfony\Component\Uid\Uuid;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
describe('OrderReference', function () {
function getNewOrderReference(string $value): OrderReference
function getNewOrderReference(UuidInterface $value): OrderReference
{
return new OrderReference($value);
}
it('can instantiate', function () {
it('can instantiate with a valid UUID v7', function () {
// GIVEN
$uuidBase58 = Uuid::v7()->toBase58();
$uuidV7 = Uuid::uuid7();
// WHEN
$vo = getNewOrderReference($uuidBase58);
$vo = getNewOrderReference($uuidV7);
// THEN
expect($vo)->toBeInstanceOf(OrderReference::class);
expect((string) $vo)->toBe($uuidBase58);
});
it('throws exception if order reference is empty', function () {
// GIVEN
$value = ' ';
// WHEN
$action = fn () => getNewOrderReference($value);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if order reference is not a valid base58 string', function () {
// GIVEN
$value = 'invalid_base58_string!!!!';
// WHEN
$action = fn () => getNewOrderReference($value);
// THEN
expect($action)->toThrow(\DomainException::class);
expect((string) $vo)->toBe($uuidV7->toString());
expect($vo->value)->toBe($uuidV7);
});
it('throws exception if provided UUID is not v7', function () {
// GIVEN
$uuidV4Base58 = Uuid::v4()->toBase58();
$uuidV4 = Uuid::uuid4();
// WHEN
$action = fn () => getNewOrderReference($uuidV4Base58);
$action = fn () => getNewOrderReference($uuidV4);
// THEN
expect($action)->toThrow(\DomainException::class);
expect($action)->toThrow(\DomainException::class, 'value must be a valid UUID v7');
});
});
it('throws exception if base58 string is syntactically valid but invalid length when decoded', function (): void {
// GIVEN
$value = 'zzzzzzzzzzzzzzzzzzzzzz'; // 22 valid chars, but decodes to out-of-bounds UUID
// WHEN
$action = fn () => new OrderReference($value);
// THEN
expect($action)->toThrow(\DomainException::class, 'Order reference must be a valid base-58 string');
});

View file

@ -33,23 +33,21 @@ use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Ports\Repositories\OrderRepository;
use Domain\Ports\Services\UuidConverter;
use Domain\Services\OrderService;
use Mockery;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\UuidV7;
use Shared\Helpers\UuidHelper;
describe('Order Cancellation', function () {
it('cancels a validated order successfully', function () {
// GIVEN
$orderId = new OrderId(Uuid::uuid7());
$validBase58 = UuidV7::v7()->toBase58();
$order = new Order(
id: $orderId,
shopId: new ShopId(Uuid::uuid7()),
reference: new OrderReference($validBase58),
reference: new OrderReference(Uuid::uuid7()),
employee: new OrderEmployee(
new Matricule('1234567'),
'Jean',
@ -102,9 +100,7 @@ describe('Order Cancellation', function () {
$repository->shouldReceive('find')->with($orderId)->andReturn($order);
$repository->shouldReceive('persist')->with($order)->once();
$uuidConverter = Mockery::mock(UuidConverter::class);
$service = new OrderService($uuidConverter, $repository);
$service = new OrderService($repository);
// WHEN
$service->cancelOrder($orderId);
@ -121,9 +117,7 @@ describe('Order Cancellation', function () {
$repository = Mockery::mock(OrderRepository::class);
$repository->shouldReceive('find')->with($orderId)->andReturn(null);
$uuidConverter = Mockery::mock(UuidConverter::class);
$service = new OrderService($uuidConverter, $repository);
$service = new OrderService($repository);
// WHEN
$action = fn () => $service->cancelOrder($orderId);
@ -186,14 +180,10 @@ describe('Order Creation', function () {
phone: '0478123456'
);
$validBase58 = UuidV7::v7()->toBase58();
$uuidConverter = Mockery::mock(UuidConverter::class);
$uuidConverter->shouldReceive('toBase58')->andReturn($validBase58);
/** @var OrderRepository|\Mockery\MockInterface $repository */
$repository = Mockery::mock(OrderRepository::class);
$service = new OrderService($uuidConverter, $repository);
$service = new OrderService($repository);
// WHEN
$order = $service->createOrderFromBasket($basket, $pickupTimeSlot, $shopper, $shop);
@ -201,7 +191,7 @@ describe('Order Creation', function () {
// THEN
expect($order)->toBeInstanceOf(Order::class);
expect($order->status)->toBe(OrderStatus::VALIDATED);
expect($order->reference->value)->toBe($validBase58);
expect(UuidHelper::isV7($order->reference->value))->toBeTrue();
// * Employee
$employee = $order->employee;
@ -292,10 +282,9 @@ describe('Order Creation', function () {
city: 'C'
);
$uuidConverter = Mockery::mock(UuidConverter::class);
$repository = Mockery::mock(OrderRepository::class);
$service = new OrderService($uuidConverter, $repository);
$service = new OrderService($repository);
// WHEN
$action = fn () => $service->createOrderFromBasket($basket, $pickupTimeSlot, $shopper, $shop);

View file

@ -39,7 +39,6 @@ use Domain\Ports\Services\EmployeeStatisticsService;
use Domain\Ports\Services\OrderBuilderSessionManager;
use Domain\Ports\Services\SessionService;
use Domain\Ports\Services\TransactionManager;
use Domain\Ports\Services\UuidConverter;
use Domain\Services\BasketService;
use Domain\Services\OrderService;
use Domain\Services\OrderTunnelService;
@ -107,7 +106,6 @@ function createOrderTunnelService(
?OrderRepository $orderRepository = null,
?TransactionManager $transactionManager = null,
?DomainEventDispatcher $eventDispatcher = null,
?UuidConverter $uuidConverter = null,
): OrderTunnelService {
if ($basketRepository === null) {
$basketRepository = mock(BasketRepository::class);
@ -144,7 +142,6 @@ function createOrderTunnelService(
$transactionManager ??= mock(TransactionManager::class);
$eventDispatcher ??= mock(DomainEventDispatcher::class);
$uuidConverter ??= mock(UuidConverter::class);
$sessionService = \Mockery::mock(SessionService::class);
$sessionService->shouldReceive('get')->andReturn(new SessionId(Uuid::uuid7()))->byDefault();
@ -155,7 +152,7 @@ function createOrderTunnelService(
$basketService = new BasketService($basketRepository, $productRepository, $sessionService);
$shopperService = new ShopperService($shopRepository, $shopperRepository);
$pickupService = new PickupSchedulingService($pickupExceptionRepository);
$orderService = new OrderService($uuidConverter, $orderRepository, $employeeStatisticsService);
$orderService = new OrderService($orderRepository);
$orderLimitPolicy = new CustomerOrderLimitPolicy(500000, 500000);
@ -580,9 +577,6 @@ describe('OrderTunnelService', function (): void {
$builderRepo = mock(OrderBuilderSessionManager::class);
$builderRepo->shouldReceive('clear')->once();
$uuidConverter = mock(UuidConverter::class);
$uuidConverter->shouldReceive('toBase58')->andReturn(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$service = createOrderTunnelService(
basketRepository: $basketRepo,
shopRepository: $shopRepo,
@ -590,8 +584,7 @@ describe('OrderTunnelService', function (): void {
orderBuilderSessionManager: $builderRepo,
orderRepository: $orderRepo,
transactionManager: $transactionManager,
eventDispatcher: $eventDispatcher,
uuidConverter: $uuidConverter
eventDispatcher: $eventDispatcher
);
// WHEN
@ -685,9 +678,6 @@ describe('OrderTunnelService', function (): void {
$builderRepo = mock(OrderBuilderSessionManager::class);
$builderRepo->shouldReceive('clear')->once();
$uuidConverter = mock(UuidConverter::class);
$uuidConverter->shouldReceive('toBase58')->andReturn(\Symfony\Component\Uid\Uuid::v7()->toBase58());
$basketRepo = mock(BasketRepository::class);
$basketRepo->shouldReceive('find')->once()->andReturn(createTestBasket(1));
$basketRepo->shouldReceive('persist')->once();
@ -723,8 +713,7 @@ describe('OrderTunnelService', function (): void {
orderBuilderSessionManager: $builderRepo,
orderRepository: $orderRepo,
transactionManager: $transactionManager,
eventDispatcher: $eventDispatcher,
uuidConverter: $uuidConverter
eventDispatcher: $eventDispatcher
);
// WHEN

View file

@ -15,8 +15,8 @@ use Domain\Ports\Services\CsvGenerator;
use Domain\Services\RegateService;
use Mockery;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Shared\DDD\DomainEventDispatcher;
use Symfony\Component\Uid\Uuid;
describe('RegateService', function (): void {
@ -26,8 +26,9 @@ describe('RegateService', function (): void {
$dispatcher = Mockery::mock(DomainEventDispatcher::class);
$service = new RegateService($csvGenerator, $dispatcher);
$orderReferenceValue = Uuid::v7()->toBase58();
$reference = new OrderReference($orderReferenceValue);
$uuid = Uuid::uuid7();
$reference = new OrderReference($uuid);
$orderReferenceValue = (string) $reference;
$employeeMatricule = new Matricule('1234567');
$pickupDay = CarbonImmutable::create(2025, 1, 1);

View file

@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
use Infrastructure\Adapters\Services\UuidConverterImpl;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Uid\Uuid as SymphonyUuid;
it('converts from base58 to uuid', function () {
describe('UuidConverterImpl', function () {
// GIVEN
$converter = new UuidConverterImpl();
$uuidStr = '123e4567-e89b-12d3-a456-426614174000';
$base58 = SymphonyUuid::fromString($uuidStr)->toBase58();
// WHEN
$result = $converter->fromBase58($base58);
// THEN
expect($result->toString())->toBe($uuidStr);
});
it('converts from uuid to base58', function () {
// GIVEN
$converter = new UuidConverterImpl();
$uuidStr = '123e4567-e89b-12d3-a456-426614174000';
$uuid = Uuid::fromString($uuidStr);
$expectedBase58 = SymphonyUuid::fromString($uuidStr)->toBase58();
// WHEN
$result = $converter->toBase58($uuid);
// THEN
expect($result)->toBe($expectedBase58);
});
});