161 lines
4.7 KiB
PHP
161 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
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'));
|
|
}
|
|
|
|
function getNewProductPricing(array $overrides = []): ProductPricing
|
|
{
|
|
$data = array_merge([
|
|
'basePriceHt' => getPricingMoney(1000),
|
|
'basePriceTtc' => getPricingMoney(1200),
|
|
'discountAmountHt' => getPricingMoney(100),
|
|
'discountedPriceHt' => getPricingMoney(900),
|
|
'discountedPriceTtc' => getPricingMoney(1080),
|
|
'priceKgTtc' => getPricingMoney(2000),
|
|
'discountRate' => 0.1,
|
|
'hasDiscount' => false,
|
|
], $overrides);
|
|
|
|
return new ProductPricing(...$data);
|
|
}
|
|
|
|
it('can instantiate', function () {
|
|
// GIVEN
|
|
$data = [];
|
|
|
|
// WHEN
|
|
$vo = getNewProductPricing($data);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(ProductPricing::class);
|
|
});
|
|
|
|
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('throws exception if discountRate is negative', function () {
|
|
// GIVEN
|
|
$discountRate = -0.5;
|
|
|
|
// WHEN
|
|
$action = fn () => getNewProductPricing(['discountRate' => $discountRate]);
|
|
|
|
// 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),
|
|
discountRate: 0.1,
|
|
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');
|
|
});
|
|
});
|