From de9bb5d2c6ce6fa076fc3b4c7a5d5eb84462521f Mon Sep 17 00:00:00 2001 From: Corentin Huet Date: Wed, 30 Sep 2026 12:18:56 +0200 Subject: [PATCH] feat: :sparkles: add campaign request creation use case --- .../Create/CreateCampaignRequest.php | 55 +++++ .../UseCases/CampaignRequest/Create/Input.php | 24 +++ .../CampaignRequest/Create/Output.php | 29 +++ .../Models/Entities/CampaignRequest.php | 26 ++- src/Domain/Models/ValueObjects/Period.php | 3 + .../RequestedCoupon/RequestedCoupon.php | 39 +++- .../CampaignRequestRepository.php | 2 +- .../Create/CreateCampaignRequestTest.php | 106 ++++++++++ .../Find/FindCampaignRequestTest.php | 5 +- .../List/ListCampaignsRequestTest.php | 10 +- .../Reject/RejectCampaignRequestTest.php | 5 +- .../Models/Entities/CampaignRequestTest.php | 5 +- .../Domain/Models/Entities/CouponTest.php | 5 +- .../RequestedCoupon/PeriodTest.php | 19 ++ .../RequestedCoupon/RequestedCouponTest.php | 200 ++++++++++++++---- 15 files changed, 479 insertions(+), 54 deletions(-) create mode 100644 src/Application/UseCases/CampaignRequest/Create/CreateCampaignRequest.php create mode 100644 src/Application/UseCases/CampaignRequest/Create/Input.php create mode 100644 src/Application/UseCases/CampaignRequest/Create/Output.php create mode 100644 tests/Unit/Application/UseCases/CampaignRequest/Create/CreateCampaignRequestTest.php diff --git a/src/Application/UseCases/CampaignRequest/Create/CreateCampaignRequest.php b/src/Application/UseCases/CampaignRequest/Create/CreateCampaignRequest.php new file mode 100644 index 0000000..9dc1b30 --- /dev/null +++ b/src/Application/UseCases/CampaignRequest/Create/CreateCampaignRequest.php @@ -0,0 +1,55 @@ + */ +final readonly class CreateCampaignRequest implements UseCase, Transactional +{ + public function __construct( + private CampaignRequestRepository $campaignRequestRepository, + private BrandRepository $brandRepository + ) { + } + + #[\Override] + public function execute(BaseInput $input): Output + { + Assert::isInstanceOf($input, Input::class); + + if (!$this->brandRepository->existsByCode($input->brandCode)) { + throw new BrandNotFoundException($input->brandCode); + } + + $campaignRequest = CampaignRequest::create( + brandCode: $input->brandCode, + name: $input->name, + applicant: $input->applicant, + campaignType: $input->campaignType, + requestedCoupon: $input->requestedCoupon, + ); + + $this->campaignRequestRepository->save($campaignRequest); + + return new Output( + id: $campaignRequest->id(), + brandCode: $campaignRequest->brandCode, + name: $campaignRequest->name, + applicant: $campaignRequest->applicant, + campaignType: $campaignRequest->campaignType, + requestedCoupon: $campaignRequest->requestedCoupon, + status: $campaignRequest->status, + createdAt: $campaignRequest->createdAt(), + ); + } +} diff --git a/src/Application/UseCases/CampaignRequest/Create/Input.php b/src/Application/UseCases/CampaignRequest/Create/Input.php new file mode 100644 index 0000000..bffc422 --- /dev/null +++ b/src/Application/UseCases/CampaignRequest/Create/Input.php @@ -0,0 +1,24 @@ +updatedAt; } + public static function create( + BrandCode $brandCode, + CampaignName $name, + Applicant $applicant, + CampaignRequestType $campaignType, + RequestedCoupon $requestedCoupon, + ?\DateTimeInterface $updatedAt = null, + ): self { + return new self( + id: CampaignRequestId::generate(), + brandCode: $brandCode, + name: $name, + applicant: $applicant, + campaignType: $campaignType, + requestedCoupon: $requestedCoupon, + status: CampaignRequestStatus::CREATED, + rejectionReason: null, + updatedAt: $updatedAt, + ); + } public static function hydrate( CampaignRequestId $id, diff --git a/src/Domain/Models/ValueObjects/Period.php b/src/Domain/Models/ValueObjects/Period.php index da672a4..c189079 100644 --- a/src/Domain/Models/ValueObjects/Period.php +++ b/src/Domain/Models/ValueObjects/Period.php @@ -12,5 +12,8 @@ final readonly class Period public DateTime $start, public DateTime $end, ) { + if ($this->end < $this->start) { + throw new \DomainException('Period end date cannot be before its start date.'); + } } } diff --git a/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php b/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php index 683cd5c..c85f37d 100644 --- a/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php +++ b/src/Domain/Models/ValueObjects/RequestedCoupon/RequestedCoupon.php @@ -15,9 +15,16 @@ use Domain\Models\ValueObjects\Period; use Domain\Models\ValueObjects\PrintRules; use Domain\Models\ValueObjects\ProcessingCode; use Domain\Models\ValueObjects\WarningThresholdQuantity; +use Shared\Enums\Country; final readonly class RequestedCoupon { + private const int DEFAULT_WARNING_THRESHOLD_QUANTITY = 100; + private const int MIN_FACE_VALUE_IN_CENTS = 1; + private const int MAX_FACE_VALUE_IN_CENTS = 300; + + public WarningThresholdQuantity $warningThresholdQuantity; + /** * @param array $couponValidityInformation * @param array $products @@ -34,12 +41,42 @@ final readonly class RequestedCoupon public array $products = [], public ?PrintRules $printRules = null, public ?GS1 $gs1 = null, - public ?WarningThresholdQuantity $warningThresholdQuantity = null, + ?WarningThresholdQuantity $warningThresholdQuantity = 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.'); + } + + if ($this->gs1 instanceof GS1 && !$this->hasValidityInformationFor(Country::BENELUX)) { + throw new \DomainException('A GS1 coupon requires validity information for Benelux.'); + } + + $this->warningThresholdQuantity = $warningThresholdQuantity ?? new WarningThresholdQuantity(self::DEFAULT_WARNING_THRESHOLD_QUANTITY); + } + + private function hasValidityInformationFor(Country $country): bool + { + foreach ($this->couponValidityInformation as $validityInformation) { + if ($validityInformation->forLocale === $country) { + return true; + } + } + + return false; } } diff --git a/src/Domain/Ports/Repositories/CampaignRequestRepository.php b/src/Domain/Ports/Repositories/CampaignRequestRepository.php index f8c6cbe..909202a 100644 --- a/src/Domain/Ports/Repositories/CampaignRequestRepository.php +++ b/src/Domain/Ports/Repositories/CampaignRequestRepository.php @@ -13,7 +13,7 @@ use Shared\ValueObjects\Pagination\Paginated; use Shared\ValueObjects\Pagination\Pagination; /** - * @extends \Shared\DDD\Ports\Repository<\Domain\Models\ValueObjects\Brand\BrandId, \Domain\Models\Entities\Brand> + * @extends \Shared\DDD\Ports\Repository<\Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId, \Domain\Models\Entities\CampaignRequest> */ interface CampaignRequestRepository extends Repository { diff --git a/tests/Unit/Application/UseCases/CampaignRequest/Create/CreateCampaignRequestTest.php b/tests/Unit/Application/UseCases/CampaignRequest/Create/CreateCampaignRequestTest.php new file mode 100644 index 0000000..af5ea21 --- /dev/null +++ b/tests/Unit/Application/UseCases/CampaignRequest/Create/CreateCampaignRequestTest.php @@ -0,0 +1,106 @@ +input = new Input( + brandCode: new BrandCode('AAA01'), + name: new CampaignName('Campaign name'), + applicant: new Applicant('John', 'Doe', new Email('john.doe@example.com')), + campaignType: CampaignRequestType::CLUB, + requestedCoupon: new RequestedCoupon( + name: new CouponName('Coupon A'), + quantity: new RequestedCouponQuantity(100), + diffusion: new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')), + moneyAmount: new MoneyAmount(new Money(Currency::EUR, 300)), + processingCode: new ProcessingCode('PC1'), + couponPicture: new CouponPicture( + new PicturePath('coupons/product.jpg'), + new PicturePath('coupons/thumbnail.jpg'), + new PicturePath('coupons/inactive_thumbnail.jpg') + ), + couponValidity: new CouponValidity(30, new DateTime('2026-02-01')), + couponValidityInformation: [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + ], + products: [new IncoCode(35784), new IncoCode(15522)], + printRules: new PrintRules(maxPerCustomer: 3), + warningThresholdQuantity: new WarningThresholdQuantity(10), + ), + ); +}); + +describe('create campaign request use case', function (): void { + it('creates a campaign request when the brand exists', function (): void { + // GIVEN + $brandRepository = \Mockery::mock(BrandRepository::class); + $brandRepository->shouldReceive('existsByCode')->once()->with($this->input->brandCode)->andReturn(true); + + $campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class); + $campaignRequestRepository->shouldReceive('save')->once()->with(\Mockery::type(CampaignRequest::class)); + + $useCase = new CreateCampaignRequest($campaignRequestRepository, $brandRepository); + + // WHEN + $result = $useCase->execute($this->input); + + // THEN + expect($result)->toBeInstanceOf(Output::class) + ->and($result->status)->toBe(CampaignRequestStatus::CREATED) + ->and($result->brandCode)->toBe($this->input->brandCode); + }); +}); + +describe('create campaign request use case validation rules', function (): void { + it('throws when the brand does not exist', function (): void { + // GIVEN + $brandRepository = \Mockery::mock(BrandRepository::class); + $brandRepository->shouldReceive('existsByCode')->once()->with($this->input->brandCode)->andReturn(false); + + $campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class); + $campaignRequestRepository->shouldNotReceive('save'); + + $useCase = new CreateCampaignRequest($campaignRequestRepository, $brandRepository); + + // WHEN + $action = fn (): Output => $useCase->execute($this->input); + + // THEN + expect($action)->toThrow(BrandNotFoundException::class); + }); +}); diff --git a/tests/Unit/Application/UseCases/CampaignRequest/Find/FindCampaignRequestTest.php b/tests/Unit/Application/UseCases/CampaignRequest/Find/FindCampaignRequestTest.php index 606acd6..715a98a 100644 --- a/tests/Unit/Application/UseCases/CampaignRequest/Find/FindCampaignRequestTest.php +++ b/tests/Unit/Application/UseCases/CampaignRequest/Find/FindCampaignRequestTest.php @@ -62,7 +62,10 @@ describe('find campaign request use case', function (): void { new PicturePath('coupons/inactive_thumbnail.jpg') ), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new IncoCode(35784), new IncoCode(15522)], new PrintRules(maxPerCustomer: 3), new GS1('01234567890128', BarcodeFormat::EAN13), diff --git a/tests/Unit/Application/UseCases/CampaignRequest/List/ListCampaignsRequestTest.php b/tests/Unit/Application/UseCases/CampaignRequest/List/ListCampaignsRequestTest.php index dd4f0fe..119c54f 100644 --- a/tests/Unit/Application/UseCases/CampaignRequest/List/ListCampaignsRequestTest.php +++ b/tests/Unit/Application/UseCases/CampaignRequest/List/ListCampaignsRequestTest.php @@ -55,7 +55,10 @@ describe('list campaigns request use case', function (): void { new PicturePath('coupons/inactive_thumbnail.jpg') ), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new IncoCode(35784), new IncoCode(15522)], new PrintRules(maxPerCustomer: 3), new GS1('01234567890128', BarcodeFormat::EAN13), @@ -74,7 +77,10 @@ describe('list campaigns request use case', function (): void { new PicturePath('coupons/inactive_thumbnail.jpg') ), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new IncoCode(24514), new IncoCode(46259)], new PrintRules(maxPerCustomer: 3), new GS1('01234567890128', BarcodeFormat::EAN13), diff --git a/tests/Unit/Application/UseCases/CampaignRequest/Reject/RejectCampaignRequestTest.php b/tests/Unit/Application/UseCases/CampaignRequest/Reject/RejectCampaignRequestTest.php index 85f1bfa..780085d 100644 --- a/tests/Unit/Application/UseCases/CampaignRequest/Reject/RejectCampaignRequestTest.php +++ b/tests/Unit/Application/UseCases/CampaignRequest/Reject/RejectCampaignRequestTest.php @@ -56,7 +56,10 @@ describe('reject campaign request use case', function (): void { new PicturePath('coupons/inactive_thumbnail.jpg') ), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new IncoCode(35784), new IncoCode(15522)], new PrintRules(maxPerCustomer: 3), new GS1('01234567890128', BarcodeFormat::EAN13), diff --git a/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php b/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php index 8b9e532..2181331 100644 --- a/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php +++ b/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php @@ -55,7 +55,10 @@ describe('campaign request', function (): void { new PicturePath('coupons/inactive_thumbnail.jpg') ), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new IncoCode(35784), new IncoCode(15522)], new PrintRules(maxPerCustomer: 3), new GS1('01234567890128', BarcodeFormat::EAN13), diff --git a/tests/Unit/Domain/Models/Entities/CouponTest.php b/tests/Unit/Domain/Models/Entities/CouponTest.php index 623ebc4..277f90a 100644 --- a/tests/Unit/Domain/Models/Entities/CouponTest.php +++ b/tests/Unit/Domain/Models/Entities/CouponTest.php @@ -424,7 +424,10 @@ describe('coupon', function (): void { ), new CouponDiffusion([new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01'))]), new CouponValidity(30, new DateTime('2026-02-01')), - [new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], + [ + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE), + new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX) + ], [new CouponLegalInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)], new ProcessingCode('5425003944130'), new BrandCode('AAA01'), diff --git a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/PeriodTest.php b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/PeriodTest.php index 0602074..31f88ae 100644 --- a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/PeriodTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/PeriodTest.php @@ -23,4 +23,23 @@ describe('period', function (): void { ->and($period->start)->toBe($start) ->and($period->end)->toBe($end); }); + + it('accepts a period whose end equals its start', function (): void { + // GIVEN + $start = new DateTime('2026-01-01'); + $end = new DateTime('2026-01-01'); + + // WHEN + $period = new Period($start, $end); + + // THEN + expect($period->start)->toBe($start) + ->and($period->end)->toBe($end); + }); + + it('throws when the end is before the start', function (): void { + // GIVEN / WHEN / THEN + expect(fn () => new Period(new DateTime('2026-02-01'), new DateTime('2026-01-01'))) + ->toThrow(\DomainException::class, 'Period end date cannot be before its start date.'); + }); }); diff --git a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php index 325ab50..c467cb1 100644 --- a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php +++ b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php @@ -29,27 +29,33 @@ covers(RequestedCoupon::class); describe('requested coupon', function (): void { beforeEach(function (): void { - $this->name = new CouponName('Coupon A'); - $this->quantity = new RequestedCouponQuantity(100); - $this->diffusion = new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')); - $this->moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300)); - $this->processingCode = new ProcessingCode('PC1'); - $this->couponPicture = new CouponPicture( + $this->name = new CouponName('Coupon A'); + $this->quantity = new RequestedCouponQuantity(100); + $this->diffusion = new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')); + $this->moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300)); + $this->processingCode = new ProcessingCode('PC1'); + $this->couponPicture = new CouponPicture( new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg'), new PicturePath('coupons/inactive_thumbnail.jpg') ); - $this->couponValidity = new CouponValidity(30, new DateTime('2026-02-01')); - $this->couponValidityInformation = [ - new CouponValidityInformation( - 'Lorem ipsum dolor sit amet consectetur adipiscing elit.', - Country::FRANCE - ), + $this->couponValidity = new CouponValidity(30, new DateTime('2026-02-01')); + $this->franceValidityInformation = new CouponValidityInformation( + 'Lorem ipsum dolor sit amet consectetur adipiscing elit.', + Country::FRANCE + ); + $this->beneluxValidityInformation = new CouponValidityInformation( + 'Lorem ipsum dolor sit amet consectetur adipiscing elit.', + Country::BENELUX + ); + $this->couponValidityInformation = [ + $this->franceValidityInformation, + $this->beneluxValidityInformation, ]; - $this->products = [new IncoCode(35784), new IncoCode(15522)]; - $this->printRules = new PrintRules(maxPerCustomer: 3); - $this->gs1 = new GS1('01234567890128', BarcodeFormat::EAN13); - $this->warningThresholdQuantity = new WarningThresholdQuantity(10); + $this->products = [new IncoCode(35784), new IncoCode(15522)]; + $this->printRules = new PrintRules(maxPerCustomer: 3); + $this->gs1 = new GS1('01234567890128', BarcodeFormat::EAN13); + $this->warningThresholdQuantity = new WarningThresholdQuantity(10); }); afterEach(function (): void { @@ -96,7 +102,7 @@ describe('requested coupon', function (): void { diffusion: $this->diffusion, moneyAmount: $this->moneyAmount, processingCode: $this->processingCode, - couponPicture:$this->couponPicture, + couponPicture: $this->couponPicture, couponValidity: $this->couponValidity, couponValidityInformation: $this->couponValidityInformation, printRules: $this->printRules, @@ -119,7 +125,7 @@ describe('requested coupon', function (): void { diffusion: $this->diffusion, moneyAmount: $this->moneyAmount, processingCode: $this->processingCode, - couponPicture:$this->couponPicture, + couponPicture: $this->couponPicture, couponValidity: $this->couponValidity, couponValidityInformation: $this->couponValidityInformation, products: $this->products, @@ -142,7 +148,7 @@ describe('requested coupon', function (): void { diffusion: $this->diffusion, moneyAmount: $this->moneyAmount, processingCode: $this->processingCode, - couponPicture:$this->couponPicture, + couponPicture: $this->couponPicture, couponValidity: $this->couponValidity, couponValidityInformation: $this->couponValidityInformation, products: $this->products, @@ -157,7 +163,7 @@ describe('requested coupon', function (): void { ->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity); }); - it('hydrates a requested coupon without warning threshold quantity', function (): void { + it('defaults the warning threshold quantity to 100 when not provided', function (): void { // GIVEN / WHEN $coupon = new RequestedCoupon( name: $this->name, @@ -165,7 +171,7 @@ describe('requested coupon', function (): void { diffusion: $this->diffusion, moneyAmount: $this->moneyAmount, processingCode: $this->processingCode, - couponPicture:$this->couponPicture, + couponPicture: $this->couponPicture, couponValidity: $this->couponValidity, couponValidityInformation: $this->couponValidityInformation, products: $this->products, @@ -174,7 +180,7 @@ describe('requested coupon', function (): void { ); // THEN - expect($coupon->warningThresholdQuantity)->toBeNull() + expect($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100)) ->and($coupon->products)->toBe($this->products) ->and($coupon->printRules)->toBe($this->printRules) ->and($coupon->gs1)->toBe($this->gs1); @@ -188,7 +194,7 @@ describe('requested coupon', function (): void { diffusion: $this->diffusion, moneyAmount: $this->moneyAmount, processingCode: $this->processingCode, - couponPicture:$this->couponPicture, + couponPicture: $this->couponPicture, couponValidity: $this->couponValidity, couponValidityInformation: $this->couponValidityInformation, ); @@ -197,28 +203,21 @@ describe('requested coupon', function (): void { expect($coupon->products)->toBe([]) ->and($coupon->printRules)->toBeNull() ->and($coupon->gs1)->toBeNull() - ->and($coupon->warningThresholdQuantity)->toBeNull(); + ->and($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100)); }); - it('accepts an empty validity information list', function (): void { - // GIVEN / WHEN - $coupon = new RequestedCoupon( - $this->name, - $this->quantity, - $this->diffusion, - $this->moneyAmount, - $this->processingCode, - $this->couponPicture, - $this->couponValidity, - [], - $this->products, - $this->printRules, - $this->gs1, - $this->warningThresholdQuantity - ); - - // THEN - expect($coupon->couponValidityInformation)->toBe([]); + it('throws when there is no validity information for france', function (): void { + // GIVEN / WHEN / THEN + expect(fn () => new RequestedCoupon( + name: $this->name, + quantity: $this->quantity, + diffusion: $this->diffusion, + moneyAmount: $this->moneyAmount, + processingCode: $this->processingCode, + couponPicture: $this->couponPicture, + couponValidity: $this->couponValidity, + couponValidityInformation: [], + ))->toThrow(\DomainException::class, 'A coupon requires validity information for France.'); }); it('throws when validity information contains an invalid item', function (): void { @@ -238,4 +237,119 @@ describe('requested coupon', function (): void { $this->warningThresholdQuantity ))->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, + couponPicture: $this->couponPicture, + 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, + couponPicture: $this->couponPicture, + 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, + couponPicture: $this->couponPicture, + 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, + couponPicture: $this->couponPicture, + 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( + name: $this->name, + quantity: $this->quantity, + diffusion: $this->diffusion, + moneyAmount: $this->moneyAmount, + processingCode: $this->processingCode, + couponPicture: $this->couponPicture, + couponValidity: $this->couponValidity, + couponValidityInformation: [$this->franceValidityInformation], + gs1: $this->gs1, + ))->toThrow(\DomainException::class, 'A GS1 coupon requires validity information for Benelux.'); + }); + + it('throws when a gs1 coupon has only benelux validity information', function (): void { + // GIVEN / WHEN / THEN + expect(fn () => new RequestedCoupon( + name: $this->name, + quantity: $this->quantity, + diffusion: $this->diffusion, + moneyAmount: $this->moneyAmount, + processingCode: $this->processingCode, + couponPicture: $this->couponPicture, + couponValidity: $this->couponValidity, + couponValidityInformation: [$this->beneluxValidityInformation], + gs1: $this->gs1, + ))->toThrow(\DomainException::class, 'A coupon requires validity information for France.'); + }); + + it('accepts a coupon without gs1 and without benelux validity information', function (): void { + // GIVEN / WHEN + $coupon = new RequestedCoupon( + name: $this->name, + quantity: $this->quantity, + diffusion: $this->diffusion, + moneyAmount: $this->moneyAmount, + processingCode: $this->processingCode, + couponPicture: $this->couponPicture, + couponValidity: $this->couponValidity, + couponValidityInformation: [$this->franceValidityInformation], + ); + + // THEN + expect($coupon->gs1)->toBeNull(); + }); });