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

542 lines
18 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\Entities;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Models\Entities\Product;
use Domain\Models\ValueObjects\DiscountValue;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\ProductWeight;
use Money\Money;
use Shared\Helpers\UuidHelper;
function makeProduct(
?Money $price = null,
TaxRate $taxRate = TaxRate::TVA_20,
?ProductWeight $weight = null,
InvoicingUnit $invoicingUnit = InvoicingUnit::PCE,
SellUnit $sellUnit = SellUnit::PCE,
?DiscountValue $discount = null,
bool $status = true,
?CarbonImmutable $orderableStart = null,
?CarbonImmutable $orderableEnd = null,
bool $hasInco = true
): Product {
return new Product(
id: new ProductId(UuidHelper::v7()),
price: $price ?? Money::EUR(1000),
tax_rate: $taxRate,
product_weight: $weight,
invoicing_unit: $invoicingUnit,
sell_unit: $sellUnit,
validDiscount: $discount,
status: $status,
orderable_start: $orderableStart,
orderable_end: $orderableEnd,
hasInco: $hasInco
);
}
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();
});
describe('Product', function (): void {
it('can instantiate correctly', function (): void {
// GIVEN
$id = new ProductId(UuidHelper::v7());
$price = Money::EUR(1000);
$taxRate = TaxRate::TVA_5_5;
$weight = new ProductWeight(500, false);
$invoicingUnit = InvoicingUnit::KGM;
$sellUnit = SellUnit::KGM;
$discount = DiscountValue::fromPercentage(10);
$status = true;
$orderableStart = CarbonImmutable::now()->subDay();
$orderableEnd = CarbonImmutable::now()->addDay();
$hasInco = true;
// WHEN
$entity = new Product(
$id,
$price,
$taxRate,
$weight,
$invoicingUnit,
$sellUnit,
$discount,
$status,
$orderableStart,
$orderableEnd,
$hasInco
);
// THEN
expect($entity->id())->toBe($id)
->and($entity->price)->toBe($price)
->and($entity->tax_rate)->toBe($taxRate)
->and($entity->product_weight)->toBe($weight)
->and($entity->invoicing_unit)->toBe($invoicingUnit)
->and($entity->sell_unit)->toBe($sellUnit)
->and($entity->validDiscount)->toBe($discount)
->and($entity->status)->toBe($status)
->and($entity->orderable_start)->toBe($orderableStart)
->and($entity->orderable_end)->toBe($orderableEnd)
->and($entity->hasInco)->toBe($hasInco);
});
describe('isPublished', function (): void {
it('returns true when status is active', function (): void {
// GIVEN
$product = makeProduct(status: true);
// WHEN
$result = $product->isPublished();
// THEN
expect($result)->toBeTrue();
});
it('returns false when status is inactive', function (): void {
// GIVEN
$product = makeProduct(status: false);
// WHEN
$result = $product->isPublished();
// THEN
expect($result)->toBeFalse();
});
});
describe('hasInco', function (): void {
it('reflects the hasInco property', function (): void {
// GIVEN
$withInco = makeProduct(hasInco: true);
$withoutInco = makeProduct(hasInco: false);
// WHEN & THEN
expect($withInco->hasInco())->toBeTrue()
->and($withoutInco->hasInco())->toBeFalse();
});
});
describe('hasDiscount', function (): void {
it('returns true when validDiscount exists', function (): void {
// GIVEN
$product = makeProduct(discount: DiscountValue::fromPercentage(15));
// WHEN
$result = $product->hasDiscount();
// THEN
expect($result)->toBeTrue();
});
it('returns false when validDiscount is null', function (): void {
// GIVEN
$product = makeProduct(discount: null);
// WHEN
$result = $product->hasDiscount();
// THEN
expect($result)->toBeFalse();
});
});
describe('canBeOrderOn', function (): void {
it('returns true when orderable dates are not defined and product is active with inco', function (): void {
// GIVEN
$product = makeProduct(orderableStart: null, orderableEnd: null, status: true, hasInco: true);
$now = CarbonImmutable::now();
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeTrue();
});
it('returns true when date is between start and end', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: $now->subDay(),
orderableEnd: $now->addDay(),
hasInco: true
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeTrue();
});
it('returns false when date is before start', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: $now->addDay(),
orderableEnd: $now->addDays(3),
hasInco: true
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeFalse();
});
it('returns false when date is after end', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: $now->subDays(3),
orderableEnd: $now->subDay(),
hasInco: true
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeFalse();
});
it('returns true when date equals orderable_start', function (): void {
// GIVEN
$now = CarbonImmutable::parse('2026-11-15 10:00:00');
$product = makeProduct(
status: true,
orderableStart: $now,
orderableEnd: $now->addDay(),
hasInco: true
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeTrue();
});
it('returns false when date equals orderable_end', function (): void {
// GIVEN
$now = CarbonImmutable::parse('2026-11-15 10:00:00');
$product = makeProduct(
status: true,
orderableStart: $now->subDay(),
orderableEnd: $now,
hasInco: true
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeFalse();
});
it('returns false when product has no inco', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: null,
orderableEnd: null,
hasInco: false
);
// WHEN
$result = $product->canBeOrderOn($now);
// THEN
expect($result)->toBeFalse();
});
});
describe('isOrderable', function (): void {
it('returns true when published and within orderable range', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: $now->subDay(),
orderableEnd: $now->addDay(),
hasInco: true
);
// WHEN
$result = $product->isOrderable($now);
// THEN
expect($result)->toBeTrue();
});
it('returns false when not published even if within date range', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: false,
orderableStart: $now->subDay(),
orderableEnd: $now->addDay(),
hasInco: true
);
// WHEN
$result = $product->isOrderable($now);
// THEN
expect($result)->toBeFalse();
});
it('returns false when published but outside date range', function (): void {
// GIVEN
$now = CarbonImmutable::now();
$product = makeProduct(
status: true,
orderableStart: $now->addDay(),
orderableEnd: $now->addDays(2),
hasInco: true
);
// WHEN
$result = $product->isOrderable($now);
// THEN
expect($result)->toBeFalse();
});
});
describe('pricing calculations', function (): void {
it('calculates standard pricing without discounts', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(1000), // 10.00 EUR HT
taxRate: TaxRate::TVA_20,
invoicingUnit: InvoicingUnit::PCE,
sellUnit: SellUnit::PCE
);
// WHEN
$pricing = $product->pricing();
// THEN
expect($pricing->basePriceHt())->toEqual(Money::EUR(1000))
->and($pricing->basePriceTtc())->toEqual(Money::EUR(1200))
->and($pricing->discountAmountHt())->toEqual(Money::EUR(0))
->and($pricing->discountedPriceHt())->toEqual(Money::EUR(1000))
->and($pricing->discountedPriceTtc())->toEqual(Money::EUR(1200))
->and($pricing->hasDiscount)->toBeFalse()
->and($pricing->discountRate->ratio())->toBe(0.0);
});
it('calculates weighted base price HT when invoiced by KGM and sold by PCE', function (): void {
// GIVEN
// 20€/kg HT, 500g weight => 10€ HT
$product = makeProduct(
price: Money::EUR(2000),
taxRate: TaxRate::TVA_20,
weight: new ProductWeight(500, false),
invoicingUnit: InvoicingUnit::KGM,
sellUnit: SellUnit::PCE
);
// WHEN
$basePriceHt = $product->basePriceHt();
// THEN
expect($basePriceHt)->toEqual(Money::EUR(1000));
});
it('applies percentage discount properly', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(1000), // 10.00 EUR HT
taxRate: TaxRate::TVA_20,
discount: DiscountValue::fromPercentage(20) // 20% discount
);
// WHEN
$pricing = $product->pricing();
// THEN
expect($pricing->hasDiscount)->toBeTrue()
->and($pricing->discountAmountHt())->toEqual(Money::EUR(200))
->and($pricing->discountedPriceHt())->toEqual(Money::EUR(800))
->and($pricing->discountedPriceTtc())->toEqual(Money::EUR(960))
->and($pricing->discountRate->percentage())->toBe(20.0);
});
it('applies fixed discount properly and caps at base price HT', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(1000), // 10.00 EUR HT
taxRate: TaxRate::TVA_20,
discount: DiscountValue::fromAmount(Money::EUR(1500)) // 15.00 EUR discount > 10.00 EUR
);
// WHEN
$pricing = $product->pricing();
// THEN
expect($pricing->discountAmountHt())->toEqual(Money::EUR(1000))
->and($pricing->discountedPriceHt())->toEqual(Money::EUR(0))
->and($pricing->discountedPriceTtc())->toEqual(Money::EUR(0));
});
it('calculates virtual price per KG TTC for PCE product with weight', function (): void {
// GIVEN
// 10€ HT, 20% VAT => 12€ TTC. Weight: 500g (0.5kg) => 24€/kg TTC
$product = makeProduct(
price: Money::EUR(1000),
taxRate: TaxRate::TVA_20,
weight: new ProductWeight(500, false),
invoicingUnit: InvoicingUnit::PCE,
sellUnit: SellUnit::PCE
);
// WHEN
$priceKgTtc = $product->priceKgTtc();
// THEN
expect($priceKgTtc)->toEqual(Money::EUR(2400));
});
it('returns standard price as price per KG TTC if the product status is false', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(1000),
taxRate: TaxRate::TVA_20,
weight: new ProductWeight(250, false),
invoicingUnit: InvoicingUnit::PCE,
sellUnit: SellUnit::PCE,
status: false
);
// WHEN
$result = $product->pricing();
// THEN
expect($result->priceKgTtc())->toEqual(Money::EUR(1200));
});
it('returns a 0.0 discount rate if base price HT is 0', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(0),
discount: DiscountValue::fromAmount(Money::EUR(0))
);
// WHEN
$result = $product->pricing();
// THEN
expect($result->discountRate->basisPoints())->toBe(0);
});
it('calculates the price per KG TTC for a product invoiced by KGM and sold by PCE', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(865), // 8.65 €/kg
taxRate: TaxRate::TVA_5_5,
weight: new ProductWeight(360, false), // 360g
invoicingUnit: InvoicingUnit::KGM,
sellUnit: SellUnit::PCE,
status: true
);
// WHEN
$result = $product->pricing();
// THEN
expect($result->basePriceHt())->toEqual(Money::EUR(312))
->and($result->basePriceTtc())->toEqual(Money::EUR(330))
->and($result->priceKgTtc())->toEqual(Money::EUR(913));
});
it('calculates the price per KG TTC for a product invoiced by KGM with percentage discount', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(865), // 8.65 €/kg
taxRate: TaxRate::TVA_5_5,
weight: new ProductWeight(360, false), // 360g
invoicingUnit: InvoicingUnit::KGM,
sellUnit: SellUnit::PCE,
discount: DiscountValue::fromPercentage(10),
status: true
);
// WHEN
$result = $product->pricing();
// THEN
expect($result->discountedPriceHt())->toEqual(Money::EUR(280))
->and($result->discountedPriceTtc())->toEqual(Money::EUR(296))
->and($result->priceKgTtc())->toEqual(Money::EUR(821));
});
it('calculates the price per KG TTC for a product invoiced by KGM with fixed amount discount', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(865), // 8.65 €/kg
taxRate: TaxRate::TVA_5_5,
weight: new ProductWeight(360, false),
invoicingUnit: InvoicingUnit::KGM,
sellUnit: SellUnit::PCE,
discount: DiscountValue::fromAmount(Money::EUR(50)), // 50 cts discount on piece
status: true
);
// WHEN
$result = $product->pricing();
// THEN
// 50 cts off a 360 g piece => 139 cts off per kg: (865 - 139) HT x 1.055
expect($result->discountedPriceTtc())->toEqual(Money::EUR(277))
->and($result->priceKgTtc())->toEqual(Money::EUR(766));
});
it('caps the discount at the piece price when a fixed discount exceeds the piece price for KGM', function (): void {
// GIVEN
$product = makeProduct(
price: Money::EUR(865), // 8.65 €/kg => 3.12 € HT for 360g
taxRate: TaxRate::TVA_5_5,
weight: new ProductWeight(360, false),
invoicingUnit: InvoicingUnit::KGM,
sellUnit: SellUnit::PCE,
discount: DiscountValue::fromAmount(Money::EUR(500)), // 5.00 € discount > 3.12 €
status: true
);
// WHEN
$result = $product->pricing();
// THEN
expect($result->discountAmountHt())->toEqual(Money::EUR(312))
->and($result->discountedPriceHt())->toEqual(Money::EUR(0))
->and($result->discountedPriceTtc())->toEqual(Money::EUR(0))
->and($result->priceKgTtc())->toEqual(Money::EUR(0))
->and($result->discountRate->basisPoints())->toBe(10000);
});
});
});