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

101 lines
4.4 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?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\OrderLineProduct;
use Domain\Models\ValueObjects\ProductWeight;
use Money\Money;
use Ramsey\Uuid\Uuid;
describe('OrderLineProduct', function (): void {
it('can instantiate correctly', function (): void {
// GIVEN
$incoId = new IncoId(123);
$productId = new ProductId(Uuid::uuid7());
$weight = new ProductWeight(500, false);
$product = new OrderLineProduct(
$incoId,
$productId,
'Product Name',
$weight,
ProductBrand::marie,
OrderUnit::KGM,
Money::EUR(1000),
Money::EUR(900),
Money::EUR(2000),
TaxRate::TVA_5_5,
'pic.jpg'
// WHEN
);
// THEN
expect($product->incoId)->toBe($incoId)
->and($product->originalId)->toBe($productId)
->and($product->name)->toBe('Product Name')
->and($product->weight)->toBe($weight)
->and($product->brand)->toBe(ProductBrand::marie)
->and($product->orderUnit)->toBe(OrderUnit::KGM)
->and($product->price->getAmount())->toBe('1000')
->and($product->discountedPrice->getAmount())->toBe('900')
->and($product->priceKgTtc->getAmount())->toBe('2000')
->and($product->taxRate)->toBe(TaxRate::TVA_5_5)
->and($product->picture)->toBe('pic.jpg');
});
it('throws exception if name is empty', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), '', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(100), Money::EUR(100), Money::EUR(100), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Product name cannot be empty');
});
it('throws exception if price is negative', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), 'Name', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(-100), Money::EUR(100), Money::EUR(100), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Product price cannot be negative');
});
it('throws exception if discounted price is negative', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), 'Name', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(100), Money::EUR(-100), Money::EUR(100), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Product discounted price cannot be negative');
});
it('throws exception if price per kg ttc is negative', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), 'Name', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(100), Money::EUR(100), Money::EUR(-100), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Price per kg TTC cannot be negative');
});
it('throws exception if discounted price is greater than price', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), 'Name', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(100), Money::EUR(150), Money::EUR(100), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Product discounted price cannot be greater than original price');
});
it('throws exception if price per kg ttc is zero', function (): void {
// GIVEN
// WHEN
$action = fn () => new OrderLineProduct(new IncoId(1), new ProductId(Uuid::uuid7()), 'Name', new ProductWeight(1, false), ProductBrand::marie, OrderUnit::KGM, Money::EUR(100), Money::EUR(100), Money::EUR(0), TaxRate::TVA_5_5);
// THEN
expect($action)->toThrow(\DomainException::class, 'Price per kg TTC has to be positive');
});
});