ldc-cocotte/tests/Unit/Domain/Models/Entities/OrderTest.php

444 lines
15 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
2026-06-14 21:32:07 +02:00
use Domain\Enums\OrderStatus;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\Entities\Basket;
2026-06-14 21:32:07 +02:00
use Domain\Models\Entities\Order;
use Domain\Models\Entities\Shop;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\BasketProduct;
2026-06-14 21:32:07 +02:00
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\DiscountRate;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\BasketId;
2026-06-14 21:32:07 +02:00
use Domain\Models\ValueObjects\Entities\OrderId;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\Entities\ShopId;
use Domain\Models\ValueObjects\Entities\ShopperId;
2026-06-14 21:32:07 +02:00
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderLine;
use Domain\Models\ValueObjects\OrderLineProduct;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\PickupTimeSlot;
use Domain\Models\ValueObjects\ProductWeight;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
2026-06-14 21:32:07 +02:00
use Money\Money;
use Shared\Helpers\UuidHelper;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
2026-06-14 21:32:07 +02:00
/**
* @param array{
* productName?: string,
* quantity?: int,
* unitPriceHt?: Money,
* unitDiscount?: Money,
* unitTax?: Money,
* totalHt?: Money,
* totalDiscount?: Money,
* totalTax?: Money,
* totalTtc?: Money,
* } $overrides
*/
2026-06-14 21:32:07 +02:00
function makeOrderLine(array $overrides = []): OrderLine
{
$product = new OrderLineProduct(
incoId: new IncoId(1),
originalId: new ProductId(UuidHelper::v7()),
2026-06-14 21:32:07 +02:00
name: $overrides['productName'] ?? 'Poulet Rôti',
weight: new ProductWeight(1600, false),
brand: ProductBrand::none,
orderUnit: OrderUnit::PCE,
price: Money::EUR(1000),
discountedPrice: Money::EUR(900),
priceKgTtc: Money::EUR(750),
taxRate: TaxRate::TVA_5_5,
);
return new OrderLine(
product: $product,
quantity: $overrides['quantity'] ?? 2,
unitPriceHt: $overrides['unitPriceHt'] ?? Money::EUR(1000),
unitDiscount: $overrides['unitDiscount'] ?? Money::EUR(100),
unitTax: $overrides['unitTax'] ?? Money::EUR(200),
totalHt: $overrides['totalHt'] ?? Money::EUR(750),
totalDiscount: $overrides['totalDiscount'] ?? Money::EUR(200),
totalTax: $overrides['totalTax'] ?? Money::EUR(99),
totalTtc: $overrides['totalTtc'] ?? Money::EUR(1899),
);
}
/**
* @param array{
* id?: OrderId,
* shopId?: ShopId,
* reference?: OrderReference,
* employee?: OrderEmployee,
* deliveryAddress?: DeliveryAddress,
* pickupTimeSlot?: PickupTimeSlot,
* status?: OrderStatus,
* total?: Money,
* lines?: array<int, OrderLine>,
* } $overrides
*/
2026-06-14 21:32:07 +02:00
function makeOrder(array $overrides = []): Order
{
return new Order(
id: $overrides['id'] ?? new OrderId(UuidHelper::v7()),
shopId: $overrides['shopId'] ?? new ShopId(UuidHelper::v7()),
2026-06-14 21:32:07 +02:00
reference: $overrides['reference'] ?? new OrderReference(
UuidHelper::v7()
2026-06-14 21:32:07 +02:00
),
employee: $overrides['employee'] ?? new OrderEmployee(
matricule: new Matricule('1234567'),
firstname: 'Jean',
lastname: 'Dupont',
facility: new ShopperEstablishmentCode('ETAB01'),
site: new ShopperSiteCode('SITEA'),
service: new ShopperServiceCode('101'),
2026-06-14 21:32:07 +02:00
),
deliveryAddress: $overrides['deliveryAddress'] ?? new DeliveryAddress(
name: 'Cocotte Lyon',
address: '12 rue de la Paix',
postcode: '69001',
city: 'Lyon',
),
pickupTimeSlot: $overrides['pickupTimeSlot'] ?? new PickupTimeSlot(
new \DateTimeImmutable('+1 day 10:00'),
new \DateInterval('PT10M'),
),
status: $overrides['status'] ?? OrderStatus::VALIDATED,
total: $overrides['total'] ?? Money::EUR(3798),
lines: $overrides['lines'] ?? [makeOrderLine()],
);
}
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
2026-06-14 21:32:07 +02:00
describe('Order - Instantiation', function (): void {
it('can instantiate a valid order', function (): void {
// GIVEN
$line = makeOrderLine();
// WHEN
$order = makeOrder(['lines' => [$line]]);
// THEN
expect($order->status)->toBe(OrderStatus::VALIDATED)
2026-06-14 21:32:07 +02:00
->and($order->getTotal()->getAmount())->toBe('3798')
->and($order->getLines())->toHaveCount(1);
});
it('throws if total is zero or negative', function (): void {
// GIVEN
$total = Money::EUR(0);
// WHEN
$action = fn () => makeOrder(['total' => $total]);
// THEN
expect($action)->toThrow(\DomainException::class, 'Total amount must be greater than zero');
});
it('throws if lines contain non-OrderLine instances', function (): void {
// GIVEN
$order = makeOrder();
2026-06-14 21:32:07 +02:00
$lines = ['not-an-order-line'];
// WHEN
$action = fn () => (new \ReflectionClass(Order::class))->newInstance(
$order->id,
$order->shopId,
$order->reference,
$order->employee,
$order->deliveryAddress,
$order->pickupTimeSlot,
$order->status,
Money::EUR(3798),
$lines,
);
2026-06-14 21:32:07 +02:00
// THEN
expect($action)->toThrow(\DomainException::class, 'All lines must be instances of OrderLine');
});
});
describe('Order - Cancellation', function (): void {
it('cancels a validated order', function (): void {
// GIVEN
$order = makeOrder(['status' => OrderStatus::VALIDATED]);
// WHEN
$order->cancel();
// THEN
expect($order->status)->toBe(OrderStatus::CANCELED);
});
it('cancels a waiting order', function (): void {
// GIVEN
$order = makeOrder(['status' => OrderStatus::WAITING]);
// WHEN
$order->cancel();
// THEN
expect($order->status)->toBe(OrderStatus::CANCELED);
});
it('is idempotent when cancelling an already cancelled order', function (): void {
// GIVEN
$order = makeOrder(['status' => OrderStatus::CANCELED]);
// WHEN
$order->cancel();
// THEN
expect($order->status)->toBe(OrderStatus::CANCELED);
});
});
describe('Order - Getters immutability', function (): void {
it('returns a clone of total', function (): void {
// GIVEN
$order = makeOrder(['total' => Money::EUR(5000)]);
// WHEN
$total1 = $order->getTotal();
$total2 = $order->getTotal();
// THEN
expect($total1)->toEqual($total2);
});
it('returns clones of lines', function (): void {
// GIVEN
$order = makeOrder();
// WHEN
$lines1 = $order->getLines();
$lines2 = $order->getLines();
// THEN
expect($lines1[0])->toEqual($lines2[0]);
});
});
describe('Order - id', function (): void {
it('returns id', function (): void {
// GIVEN
$order = makeOrder();
// WHEN
$id = $order->id();
// THEN
expect(strlen($id->value->toString()))->toBe(36);
2026-06-14 21:32:07 +02:00
});
});
describe('Order - fromBasket factory', function (): void {
it('creates an order from a basket with all lines mapped', function (): void {
// GIVEN
$basketId = new BasketId(UuidHelper::v7());
$basket = new Basket($basketId);
$basketProduct = new BasketProduct(
id: new ProductId(UuidHelper::v7()),
name: 'Poulet Rôti',
qty: 2,
unitPriceHt: Money::EUR(1000),
unitDiscount: Money::EUR(100),
taxRate: TaxRate::TVA_5_5,
weightGrams: 1200,
brand: ProductBrand::marie,
incoId: new IncoId(45),
orderUnit: OrderUnit::PCE,
slug: 'poulet-roti',
picture: new StorageFilePath('images/poulet.jpg'),
description: 'Un bon poulet',
weightVariable: false,
discountRate: DiscountRate::fromRatio(0.1),
priceKgTtc: Money::EUR(1500)
);
$basket->addOrUpdate($basketProduct);
$orderId = new OrderId(UuidHelper::v7());
$reference = new OrderReference(UuidHelper::v7());
$pickupTimeSlot = new PickupTimeSlot(
new \DateTimeImmutable('+1 day 10:00'),
new \DateInterval('PT10M')
);
$shopper = new Shopper(
id: new ShopperId(UuidHelper::v7()),
email: new Email('jean.dupont@example.com'),
matricule: new Matricule('1234567'),
nom: 'Dupont',
prenom: 'Jean',
startDate: CarbonImmutable::now(),
endDate: null,
etablissement: new ShopperEstablishmentCode('ETAB01'),
site: new ShopperSiteCode('SITEA'),
service: new ShopperServiceCode('101')
);
$shop = new Shop(
id: new ShopId(UuidHelper::v7()),
name: 'Cocotte Lyon',
address: '12 rue de la Paix',
postcode: '69001',
city: 'Lyon',
phone: '0478123456'
);
// WHEN
$order = Order::fromBasket($orderId, $reference, $basket, $pickupTimeSlot, $shopper, $shop);
// THEN
expect($order->id())->toBe($orderId)
->and($order->reference)->toBe($reference)
->and($order->status)->toBe(OrderStatus::VALIDATED)
->and($order->employee->firstname)->toBe('Jean')
->and($order->employee->lastname)->toBe('Dupont')
->and($order->deliveryAddress->name)->toBe('Cocotte Lyon')
->and($order->getLines())->toHaveCount(1)
->and($order->getLines()[0]->product->name)->toBe('Poulet Rôti')
->and($order->getLines()[0]->quantity)->toBe(2);
});
it('creates an order in PENDING status via pendingFromBasket and validates it to VALIDATED', function (): void {
// GIVEN
$basket = new Basket(new BasketId(UuidHelper::v7()), [
new BasketProduct(
id: new ProductId(UuidHelper::v7()),
name: 'Poulet Rôti',
qty: 1,
unitPriceHt: Money::EUR(1000),
unitDiscount: Money::EUR(0),
taxRate: TaxRate::TVA_5_5,
weightGrams: 1000,
brand: ProductBrand::loue,
incoId: new IncoId(123456),
orderUnit: OrderUnit::PCE,
slug: 'poulet-roti',
picture: null,
description: null,
weightVariable: false,
discountRate: DiscountRate::zero(),
priceKgTtc: Money::EUR(1000),
),
]);
$shopper = new Shopper(
id: new ShopperId(UuidHelper::v7()),
email: new Email('jean.dupont@example.com'),
matricule: new Matricule('1234567'),
nom: 'Dupont',
prenom: 'Jean',
startDate: CarbonImmutable::now(),
endDate: null,
etablissement: new ShopperEstablishmentCode('ETAB01'),
site: new ShopperSiteCode('SITEA'),
service: new ShopperServiceCode('101')
);
$shop = new Shop(
id: new ShopId(UuidHelper::v7()),
name: 'Cocotte Lyon',
address: '12 rue de la Paix',
postcode: '69001',
city: 'Lyon',
phone: '0478123456'
);
// WHEN
$order = Order::pendingFromBasket(
new OrderId(UuidHelper::v7()),
new OrderReference(UuidHelper::v7()),
$basket,
new PickupTimeSlot(new \DateTimeImmutable('+1 day 10:00'), new \DateInterval('PT10M')),
$shopper,
$shop
);
// THEN
expect($order->status)->toBe(OrderStatus::PENDING);
$order->validate();
expect($order->status)->toBe(OrderStatus::VALIDATED);
expect(fn () => $order->validate())->toThrow(\DomainException::class, 'Only pending orders can be validated.');
expect(fn () => $order->fail())->toThrow(\DomainException::class, 'Only pending orders can be marked as failed.');
$order->cancel();
expect($order->status)->toBe(OrderStatus::CANCELED);
expect(fn () => $order->validate())->toThrow(\DomainException::class, 'Only pending orders can be validated.');
});
it('marks a PENDING order as FAILED and forbids canceling PENDING or FAILED orders', function (): void {
// GIVEN
$pending = makeOrder(['status' => OrderStatus::PENDING]);
// WHEN / THEN
expect(fn () => $pending->cancel())
->toThrow(\DomainException::class, 'Cannot cancel a technical pending or failed order.');
$pending->fail();
expect($pending->status)->toBe(OrderStatus::FAILED);
$pending->fail(); // idempotent
expect($pending->status)->toBe(OrderStatus::FAILED);
expect(fn () => $pending->validate())
->toThrow(\DomainException::class, 'Only pending orders can be validated.');
expect(fn () => $pending->cancel())
->toThrow(\DomainException::class, 'Cannot cancel a technical pending or failed order.');
$waiting = makeOrder(['status' => OrderStatus::WAITING]);
expect(fn () => $waiting->validate())
->toThrow(\DomainException::class, 'Only pending orders can be validated.');
$waiting->cancel();
expect($waiting->status)->toBe(OrderStatus::CANCELED);
});
});
describe('Order - Domain Events', function (): void {
it('produces an AnOrderHasBeenPlaced domain event', function (): void {
// GIVEN
$order = makeOrder();
// WHEN
$event = $order->toPlacedEvent();
// THEN
expect($event->orderId)->toBe($order->id())
->and($event->reference)->toBe($order->reference)
->and($event->totalTtc->getAmount())->toBe($order->getTotal()->getAmount())
->and($event->byMatricule)->toBe($order->employee->matricule)
->and($event->employee->matricule)->toBe($order->employee->matricule)
->and($event->shop->name)->toBe($order->deliveryAddress->name)
->and($event->lines)->toHaveCount(count($order->getLines()))
->and($event->lines[0]->productName)->toBe($order->getLines()[0]->product->name);
});
});