ldc-boutique-cnc-sable/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php
2026-10-27 09:58:12 +01:00

337 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\OrderLine;
use Domain\Models\ValueObjects\OrderLineProduct;
use Domain\Models\ValueObjects\ProductWeight;
use Money\Money;
use Ramsey\Uuid\Uuid;
describe('OrderLine', function (): void {
function getNewOrderLineProduct(array $overrides = []): OrderLineProduct
{
$data = array_merge([
'incoId' => new IncoId(1),
'originalId' => new ProductId(Uuid::uuid7()),
'name' => 'poulet blanc',
'weight' => new ProductWeight(200, false),
'brand' => ProductBrand::loue,
'orderUnit' => OrderUnit::KGM,
'price' => Money::EUR(100),
'discountedPrice' => Money::EUR(100),
'priceKgTtc' => Money::EUR(500),
'taxRate' => TaxRate::TVA_20,
'picture' => null,
], $overrides);
return new OrderLineProduct(...$data);
}
it('can instantiate correctly', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 2;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(10);
$unitTax = Money::EUR(20);
$totalHt = Money::EUR(200);
$totalDiscount = Money::EUR(20);
$totalTax = Money::EUR(40);
$totalTtc = Money::EUR(220);
// WHEN
$line = new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($line->product)->toBe($product)
->and($line->quantity)->toBe(2)
->and($line->unitPriceHt->getAmount())->toBe('100')
->and($line->unitDiscount->getAmount())->toBe('10')
->and($line->unitTax->getAmount())->toBe('20')
->and($line->unitTtc->getAmount())->toBe('110')
->and($line->totalHt->getAmount())->toBe('200')
->and($line->totalDiscount->getAmount())->toBe('20')
->and($line->totalTax->getAmount())->toBe('40')
->and($line->totalTtc->getAmount())->toBe('220');
});
it('throws exception if quantity <= 0', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 0;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(0);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Quantity must be greater than 0');
});
it('throws exception if unit price is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(-100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(0);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Unit price must be non-negative');
});
it('throws exception if unit discount is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(-10);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(0);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Unit discount must be non-negative');
});
it('throws exception if unit tax is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(-10);
$unitTtc = Money::EUR(100);
$totalHt = Money::EUR(0);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Unit tax must be non-negative');
});
it('throws exception if unit ttc is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(200);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(0);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Unit TTC must be non-negative');
});
it('throws exception if total ht is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(-100);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Total HT must be non-negative');
});
it('throws exception if total discount is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(100);
$totalDiscount = Money::EUR(-10);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Total discount must be non-negative');
});
it('throws exception if total tax is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(100);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(-10);
$totalTtc = Money::EUR(0);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Total tax must be non-negative');
});
it('throws exception if total ttc is negative', function (): void {
// GIVEN
$product = getNewOrderLineProduct();
$quantity = 1;
$unitPriceHt = Money::EUR(100);
$unitDiscount = Money::EUR(0);
$unitTax = Money::EUR(0);
$totalHt = Money::EUR(100);
$totalDiscount = Money::EUR(0);
$totalTax = Money::EUR(0);
$totalTtc = Money::EUR(-100);
// WHEN
$action = fn () => new OrderLine(
$product,
$quantity,
$unitPriceHt,
$unitDiscount,
$unitTax,
$totalHt,
$totalDiscount,
$totalTax,
$totalTtc
);
// THEN
expect($action)->toThrow(\DomainException::class, 'Total TTC must be non-negative');
});
});