2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
|
|
2026-10-03 11:30:52 +02:00
|
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Domain\Models\ValueObjects\ProductPricing;
|
|
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
|
|
|
|
|
|
|
|
|
describe('ProductPricing', function () {
|
|
|
|
|
|
|
|
|
|
function getPricingMoney(int $amount): Money
|
|
|
|
|
{
|
|
|
|
|
return new Money($amount, new Currency('EUR'));
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 16:13:14 +01:00
|
|
|
/**
|
|
|
|
|
* @param array{
|
|
|
|
|
* basePriceHt?: Money,
|
|
|
|
|
* basePriceTtc?: Money,
|
|
|
|
|
* discountAmountHt?: Money,
|
|
|
|
|
* discountedPriceHt?: Money,
|
|
|
|
|
* discountedPriceTtc?: Money,
|
|
|
|
|
* priceKgTtc?: Money,
|
|
|
|
|
* discountRate?: DiscountRate,
|
|
|
|
|
* hasDiscount?: bool,
|
|
|
|
|
* } $overrides
|
|
|
|
|
*/
|
2026-06-14 21:32:07 +02:00
|
|
|
function getNewProductPricing(array $overrides = []): ProductPricing
|
|
|
|
|
{
|
2026-11-16 16:13:14 +01:00
|
|
|
return new ProductPricing(
|
|
|
|
|
basePriceHt: $overrides['basePriceHt'] ?? getPricingMoney(1000),
|
|
|
|
|
basePriceTtc: $overrides['basePriceTtc'] ?? getPricingMoney(1200),
|
|
|
|
|
discountAmountHt: $overrides['discountAmountHt'] ?? getPricingMoney(100),
|
|
|
|
|
discountedPriceHt: $overrides['discountedPriceHt'] ?? getPricingMoney(900),
|
|
|
|
|
discountedPriceTtc: $overrides['discountedPriceTtc'] ?? getPricingMoney(1080),
|
|
|
|
|
priceKgTtc: $overrides['priceKgTtc'] ?? getPricingMoney(2000),
|
|
|
|
|
discountRate: $overrides['discountRate'] ?? DiscountRate::fromRatio(0.1),
|
|
|
|
|
hasDiscount: $overrides['hasDiscount'] ?? false,
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it('can instantiate', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$vo = getNewProductPricing($data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($vo->basePriceHt()->getAmount())->toBe('1000')
|
|
|
|
|
->and($vo->hasDiscount)->toBeFalse();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if basePriceHt is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$basePriceHt = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['basePriceHt' => $basePriceHt]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if basePriceTtc is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$basePriceTtc = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['basePriceTtc' => $basePriceTtc]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if discountAmountHt is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$discountAmountHt = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['discountAmountHt' => $discountAmountHt]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if discountedPriceHt is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$discountedPriceHt = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['discountedPriceHt' => $discountedPriceHt]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if discountedPriceTtc is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$discountedPriceTtc = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['discountedPriceTtc' => $discountedPriceTtc]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if priceKgTtc is negative', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$priceKgTtc = getPricingMoney(-1);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => getNewProductPricing(['priceKgTtc' => $priceKgTtc]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns clones of money objects', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$price = getPricingMoney(1000);
|
|
|
|
|
$vo = getNewProductPricing(['basePriceHt' => $price]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $vo->basePriceHt();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe($price);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('ProductPricing methods', function (): void {
|
|
|
|
|
it('returns clones of all other money properties', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$pricing = new ProductPricing(
|
|
|
|
|
basePriceHt: Money::EUR(100),
|
|
|
|
|
basePriceTtc: Money::EUR(120),
|
|
|
|
|
discountAmountHt: Money::EUR(10),
|
|
|
|
|
discountedPriceHt: Money::EUR(90),
|
|
|
|
|
discountedPriceTtc: Money::EUR(108),
|
|
|
|
|
priceKgTtc: Money::EUR(150),
|
2026-10-03 11:30:52 +02:00
|
|
|
discountRate: DiscountRate::fromRatio(0.1),
|
2026-06-14 21:32:07 +02:00
|
|
|
hasDiscount: true
|
|
|
|
|
);
|
|
|
|
|
// WHEN
|
|
|
|
|
$ttc = $pricing->basePriceTtc();
|
|
|
|
|
$discHt = $pricing->discountAmountHt();
|
|
|
|
|
$discPriceHt = $pricing->discountedPriceHt();
|
|
|
|
|
$discPriceTtc = $pricing->discountedPriceTtc();
|
|
|
|
|
$priceKg = $pricing->priceKgTtc();
|
|
|
|
|
// THEN
|
|
|
|
|
expect($ttc->getAmount())->toBe('120')
|
|
|
|
|
->and($discHt->getAmount())->toBe('10')
|
|
|
|
|
->and($discPriceHt->getAmount())->toBe('90')
|
|
|
|
|
->and($discPriceTtc->getAmount())->toBe('108')
|
|
|
|
|
->and($priceKg->getAmount())->toBe('150');
|
|
|
|
|
});
|
|
|
|
|
});
|