new Input( code: new CouponCode('COU01'), name: new CouponName('Coupon name'), amount: new MoneyAmount(new Money(Currency::EUR, 100)), picture: new CouponPicture( new PicturePath('files/product.jpg'), new PicturePath('files/thumbnail.jpg'), new PicturePath('files/mobileThumbnail.jpg'), ), diffusion: new CouponDiffusion( new Period( new DateTime('2026-01-01'), new DateTime('2026-02-01'), ), ), validityInformations: new CouponValidityInformationArray([ new CouponValidityInformation( 'Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE, ), ]), legalInformations: new CouponLegalInformationArray([ new CouponLegalInformation( 'Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE, ), ]), processingCode: new CouponProcessingCodeArray([ new CouponProcessingCode('5425003944130', Country::FRANCE), ]), brandCode: new BrandCode('TST89'), createdBy: ActorId::generate(), products: new CouponProductArray(), quantity: new CouponQuantity(1000), ); it('creates a coupon when the brand exists and the code is unique', function () use ($makeInput): void { // GIVEN $input = $makeInput(); $brandRepository = \Mockery::mock(BrandRepository::class); $brandRepository->shouldReceive('existsByCode')->once()->with($input->brandCode)->andReturn(true); $couponRepository = \Mockery::mock(CouponRepository::class); $couponRepository->shouldReceive('existsByCode')->once()->with($input->code)->andReturn(false); $couponRepository->shouldReceive('save')->once()->with(\Mockery::type(Coupon::class)); $useCase = new CreateCoupon($couponRepository, $brandRepository); // WHEN $result = $useCase->execute($input); // THEN expect($result)->toBeInstanceOf(Output::class) ->and($result->id)->toBeInstanceOf(CouponId::class) ->and($result->code)->toEqual($input->code) ->and($result->name)->toEqual($input->name) ->and($result->amount)->toEqual($input->amount) ->and($result->picture)->toEqual($input->picture) ->and($result->diffusion)->toEqual($input->diffusion) ->and($result->validityInformations)->toEqual($input->validityInformations) ->and($result->legalInformations)->toEqual($input->legalInformations) ->and($result->processingCode)->toEqual($input->processingCode) ->and($result->brandCode)->toEqual($input->brandCode) ->and($result->createdBy)->toEqual($input->createdBy) ->and($result->products)->toEqual($input->products) ->and($result->movements)->toBeInstanceOf(CouponMovementArray::class) ->and($result->quantity)->toEqual($input->quantity) ->and($result->availabilityCalendar)->toBeNull() ->and($result->createdAt)->toBeInstanceOf(\DateTimeInterface::class); }); it('throws when the brand does not exist', function () use ($makeInput): void { // GIVEN $input = $makeInput(); $brandRepository = \Mockery::mock(BrandRepository::class); $brandRepository->shouldReceive('existsByCode')->once()->with($input->brandCode)->andReturn(false); $couponRepository = \Mockery::mock(CouponRepository::class); $couponRepository->shouldNotReceive('existsByCode'); $couponRepository->shouldNotReceive('save'); $useCase = new CreateCoupon($couponRepository, $brandRepository); // WHEN $action = fn (): Output => $useCase->execute($input); // THEN expect($action)->toThrow(\DomainException::class, 'Brand code "TST89" was not found.'); }); it('throws when the coupon code already exists', function () use ($makeInput): void { // GIVEN $input = $makeInput(); $brandRepository = \Mockery::mock(BrandRepository::class); $brandRepository->shouldReceive('existsByCode')->once()->with($input->brandCode)->andReturn(true); $couponRepository = \Mockery::mock(CouponRepository::class); $couponRepository->shouldReceive('existsByCode')->once()->with($input->code)->andReturn(true); $couponRepository->shouldNotReceive('save'); $useCase = new CreateCoupon($couponRepository, $brandRepository); // WHEN $action = fn (): Output => $useCase->execute($input); // THEN expect($action)->toThrow(\DomainException::class, 'Coupon code "COU01" already exists.'); }); });