ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Ranges/UpdateRangeTest.php

220 lines
8.4 KiB
PHP
Raw Permalink Normal View History

feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Actions\Ranges;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
use App\Contracts\ActionInput;
use App\Cqs\Actions\Ranges\UpdateRange;
use App\Cqs\Actions\Ranges\UpdateRangeInput;
use App\Lib\PublicS3Disk;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
use App\Models\Range;
use App\Services\ActionExecutorService;
use App\Services\ImageOptimizerService;
use App\Services\PublicImageStorageService;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
use Domain\Models\ValueObjects\PictureMetadata;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
use Illuminate\Http\UploadedFile;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Locks\LaravelCacheLock;
use Psr\Log\NullLogger;
use Shared\Filesystem\TemporaryFileFactory;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
describe('UpdateRange action', function () {
test('it updates an existing range in database', function () {
/** @var \Tests\TestCase $this */
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
// GIVEN
$range = new Range();
$range->name = 'Gamme Initiale';
$range->description = 'Ancien contenu';
$range->picture_alt = 'Ancien alt';
$range->picture_title = 'Ancien title';
$range->status = true;
$range->save();
// Storage is a strict double: no network/S3 side effect, and the upload is asserted.
$storage = \Mockery::mock(StorageService::class);
$storage->shouldReceive('storeFileOntoDisk')->once()->andReturn(new StorageFilePath('media/range.jpg'));
$action = new UpdateRange(
new PublicImageStorageService(
$storage,
new PublicS3Disk('https://cdn.test'),
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
),
);
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
$input = new UpdateRangeInput(
range: $range,
name: 'Gamme Renommée',
description: 'Nouveau contenu',
picture: UploadedFile::fake()->image('new-range.jpg', 500, 500),
pictureMetadata: new PictureMetadata(alt: 'Nouveau alt', title: 'Nouveau title'),
status: false,
);
// WHEN
$updatedRange = $action($input);
// THEN
expect($updatedRange->id)->toBe($range->id)
->and($updatedRange->name)->toBe('Gamme Renommée')
->and($updatedRange->slug)->toBe('gamme-renommee')
->and($updatedRange->description)->toBe('Nouveau contenu')
->and($updatedRange->picture_alt)->toBe('Nouveau alt')
->and($updatedRange->picture_title)->toBe('Nouveau title')
->and($updatedRange->status)->toBeFalse();
$this->assertDatabaseHas('ranges', [
'id' => $range->id,
'name' => 'Gamme Renommée',
'status' => 0,
]);
});
test('it deletes previous S3 picture when replacing range image', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$oldPicture = new StorageFilePath('media/old-range.jpg');
$newPicture = new StorageFilePath('media/new-range.jpg');
$range = new Range();
$range->name = 'Gamme Avec Image';
$range->description = 'Contenu';
$range->picture = $oldPicture;
$range->picture_alt = 'Ancien alt';
$range->picture_title = 'Ancien title';
$range->status = true;
$range->save();
$disk = new PublicS3Disk('https://cdn.test');
$storage = \Mockery::mock(StorageService::class);
$storage->shouldReceive('storeFileOntoDisk')->once()->andReturn($newPicture);
$storage->shouldReceive('fileExistsOnDisk')
->once()
->with(\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/old-range.jpg'), $disk->disk)
->andReturnTrue();
$storage->shouldReceive('removeFromDisk')
->once()
->with(\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/old-range.jpg'), $disk->disk)
->andReturnTrue();
$action = new UpdateRange(
new PublicImageStorageService(
$storage,
$disk,
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
),
);
$input = new UpdateRangeInput(
range: $range,
name: 'Gamme Avec Nouvelle Image',
description: 'Contenu',
picture: UploadedFile::fake()->image('new-range.jpg', 500, 500),
pictureMetadata: new PictureMetadata(alt: 'Nouveau alt', title: 'Nouveau title'),
status: true,
);
$executor = new ActionExecutorService(
new LaravelCacheLock(new Repository(new ArrayStore())),
new NullLogger(),
new LaravelDomainEventDispatcher(),
);
// WHEN
$updatedRange = $executor->execute($action, $input);
// THEN
expect($updatedRange->picture?->value)->toBe('media/new-range.jpg');
});
test('it removes uploaded picture and preserves previous picture on rollback', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$oldPicture = new StorageFilePath('media/old-range.jpg');
$newPicture = new StorageFilePath('media/orphan-new-range.jpg');
$range = new Range();
$range->name = 'Gamme Stable';
$range->description = 'Contenu';
$range->picture = $oldPicture;
$range->picture_alt = 'Ancien alt';
$range->picture_title = 'Ancien title';
$range->status = true;
$range->save();
$duplicate = new Range();
$duplicate->name = 'Nom Déjà Pris';
$duplicate->picture_alt = 'Alt';
$duplicate->picture_title = 'Title';
$duplicate->status = true;
$duplicate->save();
$disk = new PublicS3Disk('https://cdn.test');
$storage = \Mockery::mock(StorageService::class);
$storage->shouldReceive('storeFileOntoDisk')->once()->andReturn($newPicture);
$storage->shouldReceive('fileExistsOnDisk')
->once()
->with(
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-new-range.jpg'),
$disk->disk,
)
->andReturnTrue();
$storage->shouldReceive('removeFromDisk')
->once()
->with(
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-new-range.jpg'),
$disk->disk,
)
->andReturnTrue();
$action = new UpdateRange(
new PublicImageStorageService(
$storage,
$disk,
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
),
);
$input = new UpdateRangeInput(
range: $range,
name: 'Nom Déjà Pris',
description: 'Contenu',
picture: UploadedFile::fake()->image('new-range.jpg', 500, 500),
pictureMetadata: new PictureMetadata(alt: 'Nouveau alt', title: 'Nouveau title'),
status: true,
);
$executor = new ActionExecutorService(
new LaravelCacheLock(new Repository(new ArrayStore())),
new NullLogger(),
new LaravelDomainEventDispatcher(),
);
// WHEN & THEN
expect(fn () => $executor->execute($action, $input))->toThrow(\Exception::class);
});
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
test('it throws when an invalid input is passed', function () {
// GIVEN
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('storeFileOntoDisk');
$action = new UpdateRange(
new PublicImageStorageService(
$storage,
new PublicS3Disk('https://cdn.test'),
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
),
);
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
$invalidInput = new class () implements ActionInput
{
};
/** @var \Closure(\App\Contracts\ActionInput): mixed $callable */
$callable = \Closure::fromCallable($action);
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
// WHEN & THEN
expect(fn () => $callable($invalidInput))
->toThrow(\InvalidArgumentException::class);
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
});
});