ldc-cocotte/tests/Unit/Domain/Models/ValueObjects/OrderLineTest.php

350 lines
11 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 {
/**
* @param array{
* incoId?: IncoId,
* originalId?: ProductId,
* name?: string,
* weight?: ProductWeight,
* brand?: ProductBrand,
* orderUnit?: OrderUnit,
* price?: Money,
* discountedPrice?: Money,
* priceKgTtc?: Money,
* taxRate?: TaxRate,
* picture?: \Uwil\Storage\Lib\ValueObjects\StorageFilePath|null,
* } $overrides
*/
function getNewOrderLineProduct(array $overrides = []): OrderLineProduct
{
return new OrderLineProduct(
incoId: $overrides['incoId'] ?? new IncoId(1),
originalId: $overrides['originalId'] ?? new ProductId(Uuid::uuid7()),
name: $overrides['name'] ?? 'poulet blanc',
weight: $overrides['weight'] ?? new ProductWeight(200, false),
brand: $overrides['brand'] ?? ProductBrand::loue,
orderUnit: $overrides['orderUnit'] ?? OrderUnit::KGM,
price: $overrides['price'] ?? Money::EUR(100),
discountedPrice: $overrides['discountedPrice'] ?? Money::EUR(100),
priceKgTtc: $overrides['priceKgTtc'] ?? Money::EUR(500),
taxRate: $overrides['taxRate'] ?? TaxRate::TVA_20,
picture: $overrides['picture'] ?? null,
);
}
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');
});
});