diff --git a/src/Application/UseCases/Campaign/Find/FindCampaign.php b/src/Application/UseCases/Campaign/Find/FindCampaign.php new file mode 100644 index 0000000..cbd6297 --- /dev/null +++ b/src/Application/UseCases/Campaign/Find/FindCampaign.php @@ -0,0 +1,59 @@ + */ +final readonly class FindCampaign implements UseCase +{ + public function __construct( + private CampaignRepository $repository, + ) { + } + + #[\Override] + public function execute(BaseInput $input): Output + { + Assert::isInstanceOf($input, Input::class); + + $campaign = $this->repository->findByCode($input->code); + + if (! $campaign instanceof Campaign) { + return new CampaignNotFound(); + } + + $allCampaignCoupons = $campaign->coupons->value; + + $couponsToDisplay = array_slice( + $allCampaignCoupons, + $input->couponsPagination->from(), + $input->couponsPagination->pageSize(), + ); + + return new CampaignFound( + name: $campaign->name, + code: $campaign->code, + brandCode: $campaign->brandCode, + publication: $campaign->publication, + printAllAtOnce: $campaign->printAllAtOnce, + coupons: new Paginated( + pagination: $input->couponsPagination, + items: $couponsToDisplay, + total: count($allCampaignCoupons), + ), + createdAt: $campaign->createdAt(), + updatedAt: $campaign->updatedAt(), + ); + } +} diff --git a/src/Application/UseCases/Campaign/Find/Input.php b/src/Application/UseCases/Campaign/Find/Input.php new file mode 100644 index 0000000..b3f30b9 --- /dev/null +++ b/src/Application/UseCases/Campaign/Find/Input.php @@ -0,0 +1,18 @@ + $coupons */ + public function __construct( + public CampaignName $name, + public CampaignCode $code, + public BrandCode $brandCode, + public bool $publication, + public bool $printAllAtOnce, + public Paginated $coupons, + public \DateTimeInterface $createdAt, + public ?\DateTimeInterface $updatedAt, + ) { + } +} diff --git a/src/Application/UseCases/Campaign/Find/Outputs/CampaignNotFound.php b/src/Application/UseCases/Campaign/Find/Outputs/CampaignNotFound.php new file mode 100644 index 0000000..70df467 --- /dev/null +++ b/src/Application/UseCases/Campaign/Find/Outputs/CampaignNotFound.php @@ -0,0 +1,9 @@ +shouldReceive('findByCode')->once()->with($code)->andReturn($brand); - - $useCase = new FindBrand($repository); - - // WHEN - $result = $useCase->execute(new Input(code: $code)); - - // THEN - expect($result)->toBeInstanceOf(BrandFound::class) - ->and($result->code)->toBe($brand->code) - ->and($result->rcs)->toBe($brand->rcs) - ->and($result->contact)->toBe($brand->contact); - }); - - it('returns BrandNotFound when the code is not found', function (): void { - // GIVEN - $code = new BrandCode('AAA01'); - - $repository = \Mockery::mock(BrandRepository::class); - $repository->shouldReceive('findByCode')->once()->with($code)->andReturn(null); - - $useCase = new FindBrand($repository); - - // WHEN - $result = $useCase->execute(new Input(code: $code)); - - // WHEN / THEN - expect($result)->toBeInstanceOf(BrandNotFound::class); - }); -}); - -describe('find brand use case assertions', function (): void { - it('rejects an input of the wrong type', function (): void { - // GIVEN - $repository = \Mockery::mock(BrandRepository::class); - $useCase = new FindBrand($repository); - $invalidInput = new class () implements BaseInput - { - }; - - // WHEN - $action = fn (): Output => $useCase->execute($invalidInput); - - // THEN - expect($action)->toThrow(\InvalidArgumentException::class); - }); -}); diff --git a/tests/Unit/Application/UseCases/Campaign/Find/FindCampaignTest.php b/tests/Unit/Application/UseCases/Campaign/Find/FindCampaignTest.php new file mode 100644 index 0000000..15e03ab --- /dev/null +++ b/tests/Unit/Application/UseCases/Campaign/Find/FindCampaignTest.php @@ -0,0 +1,90 @@ + CampaignFactory::hydrated( + published: true, + name: 'Campaign A', + printAllAtOnce: false, + couponCodes: $couponCodes, + printRules: new PrintRules(maxPerCustomer: 1), + ); + + it('finds a campaign by its code with paginated coupons', function () use ($makeCampaign): void { + // GIVEN + $campaign = $makeCampaign(); + $pagination = Pagination::firstPage(); + $repository = \Mockery::mock(CampaignRepository::class); + $repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign); + + $useCase = new FindCampaign($repository); + + // WHEN + $result = $useCase->execute(new Input(code: $campaign->code, couponsPagination: $pagination)); + + // THEN + expect($result)->toBeInstanceOf(CampaignFound::class) + ->and($result->name)->toBe($campaign->name) + ->and($result->code)->toBe($campaign->code) + ->and($result->brandCode)->toBe($campaign->brandCode) + ->and($result->publication)->toBe($campaign->publication) + ->and($result->printAllAtOnce)->toBe($campaign->printAllAtOnce) + ->and($result->createdAt)->toEqual($campaign->createdAt()) + ->and($result->updatedAt)->toEqual($campaign->updatedAt()) + ->and($result->coupons->getItems())->toEqual($campaign->coupons->value) + ->and($result->coupons->getTotal())->toBe(1) + ->and($result->coupons->getPagination()->equals($pagination))->toBeTrue(); + }); + + it('returns CampaignNotFound when the code is not found', function (): void { + // GIVEN + $pagination = Pagination::firstPage(); + $code = CampaignCode::generate(); + + $repository = \Mockery::mock(CampaignRepository::class); + $repository->shouldReceive('findByCode')->once()->with($code)->andReturn(null); + + $useCase = new FindCampaign($repository); + + // WHEN + $result = $useCase->execute(new Input(code: $code, couponsPagination: $pagination)); + + // WHEN / THEN + expect($result)->toBeInstanceOf(CampaignNotFound::class); + }); +}); + +describe('find campaign use case assertions', function (): void { + it('rejects an input of the wrong type', function (): void { + // GIVEN + $repository = \Mockery::mock(CampaignRepository::class); + $useCase = new FindCampaign($repository); + $invalidInput = new class () implements BaseInput + { + }; + + // WHEN + $action = fn (): Output => $useCase->execute($invalidInput); + + // THEN + expect($action)->toThrow(\InvalidArgumentException::class); + }); +});