new ProductId(Uuid::uuid7()), 'name' => 'Product Name', 'qty' => 1, '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), ], $overrides); return new BasketProduct(...$data); } // --- Tests d'instanciation et de validation (Invariants) --- it('can instantiate', function () { // GIVEN $id = new ProductId(Uuid::uuid7()); $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)->toBeInstanceOf(BasketProduct::class); }); 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); }); });