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

315 lines
8.3 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\BasketProduct;
use Domain\Models\ValueObjects\DiscountRate;
use Domain\Models\ValueObjects\Entities\ProductId;
use Money\Currency;
use Money\Money;
use Shared\Helpers\UuidHelper;
describe('BasketProduct', function () {
function getMoney(int $amount): Money
{
return new Money($amount, new Currency('EUR'));
}
/**
* @param array{
* name?: string,
* qty?: int,
* unitPriceHt?: Money,
* unitDiscount?: Money,
* taxRate?: TaxRate,
* weightGrams?: int,
* slug?: string,
* priceKgTtc?: Money,
* } $overrides
*/
function getNewProduct(array $overrides = []): BasketProduct
{
return new BasketProduct(
id: new ProductId(UuidHelper::v7()),
name: $overrides['name'] ?? 'Product Name',
qty: $overrides['qty'] ?? 1,
unitPriceHt: $overrides['unitPriceHt'] ?? getMoney(100),
unitDiscount: $overrides['unitDiscount'] ?? getMoney(10),
taxRate: $overrides['taxRate'] ?? TaxRate::TVA_20,
weightGrams: $overrides['weightGrams'] ?? 500,
brand: ProductBrand::none,
incoId: null,
orderUnit: OrderUnit::PCE,
slug: $overrides['slug'] ?? 'product-slug',
picture: null,
description: null,
weightVariable: false,
discountRate: DiscountRate::fromRatio(0.1),
priceKgTtc: $overrides['priceKgTtc'] ?? getMoney(240),
);
}
// --- Tests d'instanciation et de validation (Invariants) ---
it('can instantiate', function () {
// GIVEN
$id = new ProductId(UuidHelper::v7());
$name = 'Product Name';
$qty = 2;
$unitPriceHt = getMoney(100);
$unitDiscount = getMoney(10);
$taxRate = TaxRate::TVA_20;
$weightGrams = 500;
$brand = ProductBrand::none;
$incoId = null;
$orderUnit = OrderUnit::PCE;
$slug = 'product-slug';
$picture = null;
$description = null;
$weightVariable = false;
$discountRate = DiscountRate::fromRatio(0.1);
$priceKgTtc = getMoney(240);
// WHEN
$vo = new BasketProduct(
id: $id,
name: $name,
qty: $qty,
unitPriceHt: $unitPriceHt,
unitDiscount: $unitDiscount,
taxRate: $taxRate,
weightGrams: $weightGrams,
brand: $brand,
incoId: $incoId,
orderUnit: $orderUnit,
slug: $slug,
picture: $picture,
description: $description,
weightVariable: $weightVariable,
discountRate: $discountRate,
priceKgTtc: $priceKgTtc
);
// THEN
expect($vo->id)->toBe($id)
->and($vo->name)->toBe($name)
->and($vo->qty)->toBe($qty);
});
it('throws exception if name is empty', function () {
// GIVEN
$name = ' ';
// WHEN
$action = fn () => getNewProduct([
'name' => $name,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if slug is empty', function () {
// GIVEN
$slug = ' ';
// WHEN
$action = fn () => getNewProduct([
'slug' => $slug,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if qty is zero or negative', function () {
// GIVEN
$qty = 0;
// WHEN
$action = fn () => getNewProduct([
'qty' => $qty,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if unitPriceHt is zero or negative', function () {
// GIVEN
$unitPriceHt = getMoney(0);
// WHEN
$action = fn () => getNewProduct([
'unitPriceHt' => $unitPriceHt,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if unitDiscount is negative', function () {
// GIVEN
$unitDiscount = getMoney(-1);
// WHEN
$action = fn () => getNewProduct([
'unitDiscount' => $unitDiscount,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
it('throws exception if priceKgTtc is zero or negative', function () {
// GIVEN
$priceKgTtc = getMoney(0);
// WHEN
$action = fn () => getNewProduct([
'priceKgTtc' => $priceKgTtc,
]);
// THEN
expect($action)->toThrow(\DomainException::class);
});
// --- Tests des méthodes de calcul et de logique métier ---
it('returns a clone of unitPriceHt', function () {
// GIVEN
$price = getMoney(100);
$product = getNewProduct(['unitPriceHt' => $price]);
// WHEN
$unitPriceHt = $product->unitPriceHt();
// THEN
expect($unitPriceHt)->toBe($price); // It returns the identical immutable reference
});
it('calculates unitTtc correctly', function () {
// GIVEN
$product = getNewProduct([
'unitPriceHt' => getMoney(1000), // ex: 10.00
'unitDiscount' => getMoney(100), // ex: 1.00
'taxRate' => TaxRate::TVA_20,
]);
// WHEN
// Discounted HT = 900
// Tax (20%) = 180
// unitTtc = 1080
$unitTtc = $product->unitTtc();
// THEN
expect($unitTtc->getAmount())->toBe('1080');
});
it('calculates discountTotal correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 3,
'unitDiscount' => getMoney(150),
]);
// WHEN
$discountTotal = $product->discountTotal();
// THEN
expect($discountTotal->getAmount())->toBe('450');
});
it('calculates priceTotalHt correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 4,
'unitPriceHt' => getMoney(1000),
]);
// WHEN
$priceTotalHt = $product->priceTotalHt();
// THEN
expect($priceTotalHt->getAmount())->toBe('4000');
});
it('calculates subtotalHt correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 2,
'unitPriceHt' => getMoney(1000),
'unitDiscount' => getMoney(200),
]);
// WHEN
// priceTotal = 2000, discountTotal = 400 => subtotalHt = 1600
$subtotalHt = $product->subtotalHt();
// THEN
expect($subtotalHt->getAmount())->toBe('1600');
});
it('calculates taxTotal correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 3,
'unitPriceHt' => getMoney(1000),
'unitDiscount' => getMoney(100),
'taxRate' => TaxRate::TVA_20,
]);
// WHEN
// (1000 - 100) * 3 = 2700
// 20% of 2700 = 540
$taxTotal = $product->taxTotal();
// THEN
expect($taxTotal->getAmount())->toBe('540');
});
it('calculates totalTtc correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 3,
'unitPriceHt' => getMoney(1000),
'unitDiscount' => getMoney(100),
'taxRate' => TaxRate::TVA_20,
]);
// WHEN
// subtotalHt = 2700
// taxTotal = 540
// totalTtc = 3240
$totalTtc = $product->totalTtc();
// THEN
expect($totalTtc->getAmount())->toBe('3240');
});
it('calculates weightTotal correctly', function () {
// GIVEN
$product = getNewProduct([
'qty' => 5,
'weightGrams' => 200,
]);
// WHEN
$weightTotal = $product->weightTotal();
// THEN
expect($weightTotal)->toBe(1000);
});
});