feat: ✨ Campaign Details - #51 - US2.4

This commit is contained in:
Noah Lancien 2026-10-22 12:23:37 +02:00
parent 38c1d43d2a
commit f50cd625b0
7 changed files with 214 additions and 84 deletions

View file

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Campaign\Find;
use Application\UseCases\Campaign\Find\Outputs\CampaignFound;
use Application\UseCases\Campaign\Find\Outputs\CampaignNotFound;
use Domain\Models\Entities\Campaign;
use Domain\Ports\Repositories\CampaignRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\DDD\UseCases\Output;
use Shared\DDD\UseCases\UseCase;
use Shared\ValueObjects\Pagination\Paginated;
use Webmozart\Assert\Assert;
/** @implements UseCase<\Application\UseCases\Campaign\Find\Input, \Shared\DDD\UseCases\Output> */
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(),
);
}
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Campaign\Find;
use Domain\Models\ValueObjects\Campaign\CampaignCode;
use Shared\DDD\UseCases\Input as UseCaseInput;
use Shared\ValueObjects\Pagination\Pagination;
final readonly class Input implements UseCaseInput
{
public function __construct(
public CampaignCode $code,
public Pagination $couponsPagination,
) {
}
}

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Campaign\Find\Outputs;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Campaign\CampaignCode;
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
use Domain\Models\ValueObjects\Campaign\CampaignName;
use Shared\ValueObjects\Pagination\Paginated;
final readonly class CampaignFound implements Output
{
/** @param Paginated<CampaignCoupon> $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,
) {
}
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Campaign\Find\Outputs;
final readonly class CampaignNotFound implements Output
{
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Campaign\Find\Outputs;
use Shared\DDD\UseCases\Output as UseCaseOutput;
interface Output extends UseCaseOutput
{
}

View file

@ -1,84 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Application\UseCases\Brand\Find;
use Application\UseCases\Brand\Find\FindBrand;
use Application\UseCases\Brand\Find\Input;
use Application\UseCases\Brand\Find\Output\BrandFound;
use Application\UseCases\Brand\Find\Output\BrandNotFound;
use Application\UseCases\Brand\Find\Output\Output;
use Domain\Models\Entities\Brand;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandId;
use Domain\Models\ValueObjects\Brand\BrandName;
use Domain\Ports\Repositories\BrandRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Rcs;
covers(FindBrand::class, Input::class, BrandFound::class, BrandNotFound::class);
describe('find brand use case', function (): void {
it('finds a brand by its code', function (): void {
// GIVEN
$code = new BrandCode('AAA01');
$brand = Brand::hydrate(
BrandId::generate(),
$code,
new BrandName('Name'),
new PicturePath('files/picture.jpg'),
new Rcs('RCS PARIS 111 222 333'),
new Email('contact@example.com'),
);
$repository = \Mockery::mock(BrandRepository::class);
$repository->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);
});
});

View file

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Application\UseCases\Campaign\Find;
use Application\UseCases\Campaign\Find\FindCampaign;
use Application\UseCases\Campaign\Find\Input;
use Application\UseCases\Campaign\Find\Outputs\CampaignFound;
use Application\UseCases\Campaign\Find\Outputs\CampaignNotFound;
use Application\UseCases\Campaign\Find\Outputs\Output;
use Domain\Models\Entities\Campaign;
use Domain\Models\ValueObjects\Campaign\CampaignCode;
use Domain\Ports\Repositories\CampaignRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Domain\Models\ValueObjects\PrintRules;
use Shared\ValueObjects\Pagination\Pagination;
use Tests\Fixtures\Campaign\CampaignFactory;
covers(FindCampaign::class, Input::class, CampaignFound::class, CampaignNotFound::class);
describe('find campaign use case', function (): void {
$makeCampaign = static fn (array $couponCodes = ['COU01']): Campaign => 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);
});
});