refactor(application): update Create, Find, and List brand use cases with new contracts

This commit is contained in:
Jérémy Munsch 2026-09-20 17:17:25 +02:00
parent 1576848aa1
commit 3951d7fc95
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
12 changed files with 52 additions and 60 deletions

View file

@ -10,10 +10,11 @@ use Domain\Ports\Repositories\BrandRepository;
use Domain\Ports\Services\PictureStorage;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\DDD\UseCases\UseCase;
use Shared\DDD\UseCases\Transactional;
use Webmozart\Assert\Assert;
/** @implements UseCase<\Application\UseCases\Brand\Create\Input, \Application\UseCases\Brand\Create\Output> */
final readonly class CreateBrand implements UseCase
final readonly class CreateBrand implements UseCase, Transactional
{
public function __construct(
private BrandRepository $repository,

View file

@ -7,16 +7,16 @@ namespace Application\UseCases\Brand\Create;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandName;
use Shared\DDD\UseCases\Input as UseCaseInput;
use Shared\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\RawPicture;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\RawPicturePath;
use Shared\ValueObjects\Rcs;
final readonly class Input implements UseCaseInput
{
public function __construct(
public BrandCode $code,
public BrandName $name,
public RawPicture $logo,
public RawPicturePath $logo,
public Rcs $rcs,
public Email $contact,
) {

View file

@ -7,9 +7,9 @@ namespace Application\UseCases\Brand\Create;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandName;
use Shared\DDD\UseCases\Output as UseCaseOutput;
use Shared\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Rcs;
final readonly class Output implements UseCaseOutput
{

View file

@ -6,9 +6,9 @@ namespace Application\UseCases\Brand\Find\Output;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandName;
use Shared\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Rcs;
final readonly class BrandFound implements Output
{

View file

@ -6,7 +6,7 @@ namespace Application\UseCases\Brand\List;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Brand\BrandName;
use Shared\Models\ValueObjects\PicturePath;
use Domain\Models\ValueObjects\PicturePath;
final readonly class BrandListItem
{

View file

@ -4,14 +4,15 @@ declare(strict_types=1);
namespace Application\UseCases\Brand\List;
use Domain\Models\ValueObjects\Brand\BrandSortCriteria;
use Shared\DDD\UseCases\Input as UseCaseInput;
use Shared\Helpers\Pagination;
use Shared\ValueObjects\Pagination\Pagination;
final readonly class Input implements UseCaseInput
{
public function __construct(
public Pagination $pagination,
public ?SortCriteria $sort = null,
public ?BrandSortCriteria $sort = null,
) {
}
}

View file

@ -8,7 +8,7 @@ use Domain\Models\Entities\Brand;
use Domain\Ports\Repositories\BrandRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\DDD\UseCases\UseCase;
use Shared\Helpers\Paginated;
use Shared\ValueObjects\Pagination\Paginated;
use Webmozart\Assert\Assert;
/** @implements UseCase<\Application\UseCases\Brand\List\Input, \Application\UseCases\Brand\List\Output> */
@ -26,8 +26,7 @@ final readonly class ListBrands implements UseCase
$result = $this->repository->findAll(
pagination: $input->pagination,
sortField: $input->sort?->field,
sortDirection: $input->sort?->direction,
sortCriteria: $input->sort,
);
$items = array_map(

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Application\UseCases\Brand\List;
use Shared\DDD\UseCases\Output as UseCaseOutput;
use Shared\Helpers\Paginated;
use Shared\ValueObjects\Pagination\Paginated;
final readonly class Output implements UseCaseOutput
{

View file

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Application\UseCases\Brand\List;
use Domain\Enums\Brand\SortField;
final readonly class SortCriteria
{
public function __construct(
public SortField $field = SortField::Name,
public \SortDirection $direction = \SortDirection::Ascending,
) {
}
}

View file

@ -14,17 +14,17 @@ use Domain\Models\ValueObjects\Brand\BrandName;
use Domain\Ports\Repositories\BrandRepository;
use Domain\Ports\Services\PictureStorage;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\RawPicture;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Domain\Models\ValueObjects\RawPicturePath;
use Shared\ValueObjects\Rcs;
covers(CreateBrand::class);
covers(CreateBrand::class, Input::class, Output::class);
describe('create brand use case', function (): void {
it('creates a brand when the code is unique and valid', function (): void {
// GIVEN
$rawPicture = new RawPicture(content: 'binary-content', originalName: 'picture.jpg');
$rawPicture = new RawPicturePath(content: 'binary-content', originalName: 'picture.jpg');
$logoPath = new PicturePath('files/picture.jpg');
$input = new Input(
@ -51,13 +51,15 @@ describe('create brand use case', function (): void {
expect($result)->toBeInstanceOf(Output::class)
->and($result->code)->toBeInstanceOf(BrandCode::class);
});
});
describe('create brand use case validation rules', function (): void {
it('throws when the brand code already exists', function (): void {
// GIVEN
$input = new Input(
code: new BrandCode('AAA01'),
name: new BrandName('Name'),
logo: new RawPicture(content: 'binary-content', originalName: 'picture.jpg'),
logo: new RawPicturePath(content: 'binary-content', originalName: 'picture.jpg'),
rcs: new Rcs('RCS PARIS 111 222 333'),
contact: new Email('contact@example.com'),
);
@ -77,7 +79,9 @@ describe('create brand use case', function (): void {
// THEN
expect($action)->toThrow(BrandCodeAlreadyExistsException::class);
});
});
describe('create brand use case assertions', function (): void {
it('rejects an input of the wrong type', function (): void {
// GIVEN
$repository = \Mockery::mock(BrandRepository::class);

View file

@ -15,11 +15,11 @@ 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\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Rcs;
covers(FindBrand::class);
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 {
@ -49,7 +49,7 @@ describe('find brand use case', function (): void {
->and($result->contact)->toBe($brand->contact);
});
it('throws when the code is not found', function (): void {
it('returns BrandNotFound when the code is not found', function (): void {
// GIVEN
$code = new BrandCode('AAA01');
@ -64,7 +64,9 @@ describe('find brand use case', function (): void {
// 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);

View file

@ -8,26 +8,26 @@ use Application\UseCases\Brand\List\BrandListItem;
use Application\UseCases\Brand\List\Input;
use Application\UseCases\Brand\List\ListBrands;
use Application\UseCases\Brand\List\Output;
use Application\UseCases\Brand\List\SortCriteria;
use Domain\Enums\Brand\SortField;
use Domain\Models\ValueObjects\Brand\BrandSortField;
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\Models\ValueObjects\Brand\BrandSortCriteria;
use Domain\Ports\Repositories\BrandRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\Helpers\Paginated;
use Shared\Helpers\Pagination;
use Shared\Models\ValueObjects\Email;
use Shared\Models\ValueObjects\PicturePath;
use Shared\Models\ValueObjects\Rcs;
use Shared\ValueObjects\Email;
use Domain\Models\ValueObjects\PicturePath;
use Shared\ValueObjects\Pagination\Paginated;
use Shared\ValueObjects\Pagination\Pagination;
use Shared\ValueObjects\Rcs;
covers(ListBrands::class);
covers(ListBrands::class, Input::class, Output::class, BrandListItem::class);
describe('list brands use case', function (): void {
it('maps repository brands to list items, preserving pagination and sort', function (): void {
// GIVEN
$pagination = Pagination::firstPage(25);
$pagination = Pagination::firstPage();
$brand1 = Brand::hydrate(
BrandId::generate(),
new BrandCode('AAA01'),
@ -46,13 +46,13 @@ describe('list brands use case', function (): void {
);
$paginated = new Paginated($pagination, [$brand1, $brand2], 2);
$sort = new SortCriteria(field: SortField::CreatedAt, direction: \SortDirection::Descending);
$sort = new BrandSortCriteria(field: BrandSortField::CreatedAt, direction: \SortDirection::Descending);
$input = new Input(pagination: $pagination, sort: $sort);
$repository = \Mockery::mock(BrandRepository::class);
$repository->shouldReceive('findAll')
->once()
->with($pagination, SortField::CreatedAt, \SortDirection::Descending)
->with($pagination, BrandSortCriteria::class)
->andReturn($paginated);
$useCase = new ListBrands($repository);
@ -75,14 +75,14 @@ describe('list brands use case', function (): void {
it('returns an empty list without error when there are no brands', function (): void {
// GIVEN
$pagination = Pagination::firstPage(25);
$pagination = Pagination::firstPage();
$paginated = new Paginated($pagination, [], 0);
$input = new Input(pagination: $pagination);
$repository = \Mockery::mock(BrandRepository::class);
$repository->shouldReceive('findAll')
->once()
->with($pagination, null, null)
->with($pagination, null)
->andReturn($paginated);
$useCase = new ListBrands($repository);
@ -94,7 +94,8 @@ describe('list brands use case', function (): void {
expect($result->brands->getItems())->toBe([])
->and($result->brands->getTotal())->toBe(0);
});
});
describe('list brands use case assertions', function (): void {
it('rejects an input of the wrong type', function (): void {
// GIVEN
$repository = \Mockery::mock(BrandRepository::class);