refactor: ♻️ move face value validation to MoneyAmount

This commit is contained in:
Corentin Huet 2026-10-02 17:00:14 +02:00
parent a2d211814b
commit 5a59a92546
4 changed files with 42 additions and 78 deletions

View file

@ -12,9 +12,18 @@ use Shared\ValueObjects\Money;
*/
final readonly class MoneyAmount implements \Stringable, Equatable
{
private const int MIN_FACE_VALUE_IN_CENTS = 1;
private const int MAX_FACE_VALUE_IN_CENTS = 300;
public function __construct(
public Money $value,
) {
if ($this->value->value < self::MIN_FACE_VALUE_IN_CENTS || $this->value->value > self::MAX_FACE_VALUE_IN_CENTS) {
throw new \DomainException(sprintf(
'Money amount must be between 0.01€ and 3.00€, %s given.',
$this->value,
));
}
}
#[\Override]

View file

@ -17,9 +17,6 @@ use Shared\Enums\Country;
final readonly class RequestedCoupon
{
private const int MIN_FACE_VALUE_IN_CENTS = 1;
private const int MAX_FACE_VALUE_IN_CENTS = 300;
/**
* @param array<CouponValidityInformation> $couponValidityInformation
* @param array<IncoCode> $products
@ -36,21 +33,12 @@ final readonly class RequestedCoupon
public ?PrintRules $printRules = null,
public ?GS1 $gs1 = null,
) {
$money = $this->moneyAmount->value;
foreach ($this->couponValidityInformation as $validityInformation) {
if (!$validityInformation instanceof CouponValidityInformation) {
throw new \DomainException('Coupon validity information must be CouponValidityInformation instances.');
}
}
if ($money->value < self::MIN_FACE_VALUE_IN_CENTS || $money->value > self::MAX_FACE_VALUE_IN_CENTS) {
throw new \DomainException(sprintf(
'Coupon face value must be between 0.01€ and 3.00€, %s given.',
$money,
));
}
if (!$this->hasValidityInformationFor(Country::FRANCE)) {
throw new \DomainException('A coupon requires validity information for France.');
}

View file

@ -12,8 +12,11 @@ covers(MoneyAmount::class);
describe('money amount', function (): void {
beforeEach(function (): void {
$this->money = new Money(Currency::EUR, 300);
$this->secondMoney = new Money(Currency::EUR, 500);
$this->money = new Money(Currency::EUR, 300);
$this->secondMoney = new Money(Currency::EUR, 200);
$this->minimumMoney = new Money(Currency::EUR, 1);
$this->zeroMoney = new Money(Currency::EUR, 0);
$this->aboveLimitMoney = new Money(Currency::EUR, 301);
});
it('wraps a Money value', function (): void {
@ -54,4 +57,32 @@ describe('money amount', function (): void {
// THEN
expect((string) $amount)->toBe((string) $amount->value);
});
it('accepts the minimum amount of 0.01€', function (): void {
// GIVEN / WHEN
$amount = new MoneyAmount($this->minimumMoney);
// THEN
expect($amount->value)->toBe($this->minimumMoney);
});
it('accepts the maximum amount of 3.00€', function (): void {
// GIVEN / WHEN
$amount = new MoneyAmount($this->money);
// THEN
expect($amount->value)->toBe($this->money);
});
it('throws when the amount is zero', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new MoneyAmount($this->zeroMoney))
->toThrow(\DomainException::class, 'Money amount must be between 0.01€ and 3.00€');
});
it('throws when the amount is above 3.00€', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new MoneyAmount($this->aboveLimitMoney))
->toThrow(\DomainException::class, 'Money amount must be between 0.01€ and 3.00€');
});
});

View file

@ -188,70 +188,6 @@ describe('requested coupon', function (): void {
))->toThrow(\DomainException::class, 'Coupon validity information must be CouponValidityInformation instances.');
});
it('accepts the minimum face value of 0.01€', function (): void {
// GIVEN
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 1));
// WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->moneyAmount)->toBe($moneyAmount);
});
it('accepts the maximum face value of 3.00€', function (): void {
// GIVEN
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300));
// WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->moneyAmount)->toBe($moneyAmount);
});
it('throws when the face value is zero', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 0)),
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
});
it('throws when the face value is above 3.00€', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 301)),
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
});
it('throws when a gs1 coupon has no benelux validity information', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(