From 3951d7fc956dd8d104dd6c432fc49732111fa2de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Sun, 20 Sep 2026 17:17:25 +0200 Subject: [PATCH] refactor(application): update Create, Find, and List brand use cases with new contracts --- .../UseCases/Brand/Create/CreateBrand.php | 3 +- .../UseCases/Brand/Create/Input.php | 8 ++--- .../UseCases/Brand/Create/Output.php | 6 ++-- .../UseCases/Brand/Find/Output/BrandFound.php | 6 ++-- .../UseCases/Brand/List/BrandListItem.php | 2 +- src/Application/UseCases/Brand/List/Input.php | 5 ++-- .../UseCases/Brand/List/ListBrands.php | 5 ++-- .../UseCases/Brand/List/Output.php | 2 +- .../UseCases/Brand/List/SortCriteria.php | 16 ---------- .../UseCases/Brand/Create/CreateBrandTest.php | 18 +++++++----- .../UseCases/Brand/Find/FindBrandTest.php | 12 ++++---- .../UseCases/Brand/List/ListBrandsTest.php | 29 ++++++++++--------- 12 files changed, 52 insertions(+), 60 deletions(-) delete mode 100644 src/Application/UseCases/Brand/List/SortCriteria.php diff --git a/src/Application/UseCases/Brand/Create/CreateBrand.php b/src/Application/UseCases/Brand/Create/CreateBrand.php index 9579557..7eb070f 100644 --- a/src/Application/UseCases/Brand/Create/CreateBrand.php +++ b/src/Application/UseCases/Brand/Create/CreateBrand.php @@ -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, diff --git a/src/Application/UseCases/Brand/Create/Input.php b/src/Application/UseCases/Brand/Create/Input.php index adc51b1..6e7e9a5 100644 --- a/src/Application/UseCases/Brand/Create/Input.php +++ b/src/Application/UseCases/Brand/Create/Input.php @@ -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, ) { diff --git a/src/Application/UseCases/Brand/Create/Output.php b/src/Application/UseCases/Brand/Create/Output.php index 0862b8c..fbf416f 100644 --- a/src/Application/UseCases/Brand/Create/Output.php +++ b/src/Application/UseCases/Brand/Create/Output.php @@ -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 { diff --git a/src/Application/UseCases/Brand/Find/Output/BrandFound.php b/src/Application/UseCases/Brand/Find/Output/BrandFound.php index 3057328..2540207 100644 --- a/src/Application/UseCases/Brand/Find/Output/BrandFound.php +++ b/src/Application/UseCases/Brand/Find/Output/BrandFound.php @@ -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 { diff --git a/src/Application/UseCases/Brand/List/BrandListItem.php b/src/Application/UseCases/Brand/List/BrandListItem.php index f9b2e20..13dfc38 100644 --- a/src/Application/UseCases/Brand/List/BrandListItem.php +++ b/src/Application/UseCases/Brand/List/BrandListItem.php @@ -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 { diff --git a/src/Application/UseCases/Brand/List/Input.php b/src/Application/UseCases/Brand/List/Input.php index e782a8c..ff304ae 100644 --- a/src/Application/UseCases/Brand/List/Input.php +++ b/src/Application/UseCases/Brand/List/Input.php @@ -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, ) { } } diff --git a/src/Application/UseCases/Brand/List/ListBrands.php b/src/Application/UseCases/Brand/List/ListBrands.php index 1783380..58f4f45 100644 --- a/src/Application/UseCases/Brand/List/ListBrands.php +++ b/src/Application/UseCases/Brand/List/ListBrands.php @@ -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( diff --git a/src/Application/UseCases/Brand/List/Output.php b/src/Application/UseCases/Brand/List/Output.php index 4b3873e..940283a 100644 --- a/src/Application/UseCases/Brand/List/Output.php +++ b/src/Application/UseCases/Brand/List/Output.php @@ -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 { diff --git a/src/Application/UseCases/Brand/List/SortCriteria.php b/src/Application/UseCases/Brand/List/SortCriteria.php deleted file mode 100644 index f6724b8..0000000 --- a/src/Application/UseCases/Brand/List/SortCriteria.php +++ /dev/null @@ -1,16 +0,0 @@ -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); diff --git a/tests/Unit/Application/UseCases/Brand/Find/FindBrandTest.php b/tests/Unit/Application/UseCases/Brand/Find/FindBrandTest.php index 6d55ed8..2a251a5 100644 --- a/tests/Unit/Application/UseCases/Brand/Find/FindBrandTest.php +++ b/tests/Unit/Application/UseCases/Brand/Find/FindBrandTest.php @@ -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); diff --git a/tests/Unit/Application/UseCases/Brand/List/ListBrandsTest.php b/tests/Unit/Application/UseCases/Brand/List/ListBrandsTest.php index dd31702..b804a5c 100644 --- a/tests/Unit/Application/UseCases/Brand/List/ListBrandsTest.php +++ b/tests/Unit/Application/UseCases/Brand/List/ListBrandsTest.php @@ -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);