From 5a59a92546a17aabcd68527fa7cbfd656762501c Mon Sep 17 00:00:00 2001 From: Corentin Huet Date: Fri, 2 Oct 2026 17:00:14 +0200 Subject: [PATCH] refactor: :recycle: move face value validation to MoneyAmount --- .../Models/ValueObjects/MoneyAmount.php | 9 +++ .../RequestedCoupon/RequestedCoupon.php | 12 ---- .../Models/ValueObjects/MoneyAmountTest.php | 35 +++++++++- .../RequestedCoupon/RequestedCouponTest.php | 64 ------------------- 4 files changed, 42 insertions(+), 78 deletions(-) diff --git a/src/Domain/Models/ValueObjects/MoneyAmount.php b/src/Domain/Models/ValueObjects/MoneyAmount.php index cb0b753..31f307d 100644 --- a/src/Domain/Models/ValueObjects/MoneyAmount.php +++ b/src/Domain/Models/ValueObjects/MoneyAmount.php @@ -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] diff --git a/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php b/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php index 8fc0c3c..c5bfbab 100644 --- a/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php +++ b/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php @@ -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 * @param array $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.'); } diff --git a/tests/Unit/Domain/Models/ValueObjects/MoneyAmountTest.php b/tests/Unit/Domain/Models/ValueObjects/MoneyAmountTest.php index dbd4190..84ea7e2 100644 --- a/tests/Unit/Domain/Models/ValueObjects/MoneyAmountTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/MoneyAmountTest.php @@ -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€'); + }); }); diff --git a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php index 111a145..f55da32 100644 --- a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php @@ -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(