ldc-cocotte/tests/Integration/WithApp/App/Actions/Ranges/DeleteRangeTest.php
Jérémy Munsch 86e7340c48
feat(products): add success and failure handling for product actions
- Implemented HandlesSuccess and HandlesFailure interfaces in CreateProduct and UpdateProduct actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

feat(ranges): add success and failure handling for range actions

- Implemented HandlesSuccess and HandlesFailure interfaces in CreateRange and UpdateRange actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

feat(slides): add success and failure handling for slide actions

- Implemented HandlesSuccess and HandlesFailure interfaces in CreateSlide and UpdateSlide actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

chore: introduce TracksPendingStoragePaths trait

- Created a new trait to manage tracking of uploaded and obsolete storage paths.
- This will streamline the handling of file cleanup across various actions.

test: add tests for action failure handling and cleanup

- Added tests to verify that uploaded files are cleaned up on action failure.
- Ensured that previous files are preserved during rollback scenarios.
2026-11-15 20:31:37 +01:00

117 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Contracts\ActionInput;
use App\Cqs\Actions\Ranges\DeleteRange;
use App\Cqs\Actions\Ranges\DeleteRangeInput;
use App\Lib\PublicS3Disk;
use App\Models\Range;
use App\Services\ActionExecutorService;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Locks\LaravelCacheLock;
use Psr\Log\NullLogger;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
uses(RefreshDatabase::class);
describe('DeleteRange action', function () {
test('it deletes an existing range from database', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$range = new Range();
$range->name = 'Gamme Temporaire';
$range->picture_alt = 'Alt';
$range->picture_title = 'Title';
$range->status = true;
$range->save();
// Range has no picture: storage must stay untouched.
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('fileExistsOnDisk');
$storage->shouldNotReceive('removeFromDisk');
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$input = new DeleteRangeInput(range: $range);
// WHEN
$result = $action($input);
// THEN
expect($result)->toBeTrue();
$this->assertDatabaseMissing('ranges', [
'id' => $range->id,
]);
});
test('it removes the picture from storage when the range has one', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$range = new Range();
$range->name = 'Gamme Avec Image';
$range->picture_alt = 'Alt';
$range->picture_title = 'Title';
$range->status = true;
$range->picture = new StorageFilePath('media/range.jpg');
$range->save();
$storage = \Mockery::mock(StorageService::class);
$storage->shouldReceive('fileExistsOnDisk')->once()->andReturnTrue();
$storage->shouldReceive('removeFromDisk')->once()->andReturnTrue();
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$executor = new ActionExecutorService(
new LaravelCacheLock(new Repository(new ArrayStore())),
new NullLogger(),
new LaravelDomainEventDispatcher(),
);
// WHEN
$result = $executor->execute($action, new DeleteRangeInput(range: $range));
// THEN
expect($result)->toBeTrue();
$this->assertDatabaseMissing('ranges', ['id' => $range->id]);
});
test('it preserves S3 picture when DeleteRange triggers failure hook before commit', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$range = new Range();
$range->name = 'Gamme Protégée';
$range->picture_alt = 'Alt';
$range->picture_title = 'Title';
$range->status = true;
$range->picture = new StorageFilePath('media/keep-range.jpg');
$range->save();
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('fileExistsOnDisk');
$storage->shouldNotReceive('removeFromDisk');
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$input = new DeleteRangeInput(range: $range);
// WHEN
$action($input);
$action->onFailure($input, new \RuntimeException('Simulated rollback'));
// THEN
expect(true)->toBeTrue();
});
test('it throws when an invalid input is passed', function () {
// GIVEN
$storage = \Mockery::mock(StorageService::class);
$action = new DeleteRange($storage, new PublicS3Disk('https://cdn.test'));
$invalidInput = new class () implements ActionInput
{
};
// WHEN & THEN
expect(fn () => $action($invalidInput))
->toThrow(\InvalidArgumentException::class);
});
});