- 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.
98 lines
3.5 KiB
PHP
98 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Cqs\Actions\Slides\DeleteSlide;
|
|
use App\Cqs\Actions\Slides\DeleteSlideInput;
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Models\Slide;
|
|
use App\Services\ActionExecutorService;
|
|
use Illuminate\Cache\ArrayStore;
|
|
use Illuminate\Cache\Repository;
|
|
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;
|
|
|
|
describe('DeleteSlide action', function (): void {
|
|
it('deletes an existing slide from database without picture', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$slide = new Slide();
|
|
$slide->title = 'Slide à supprimer';
|
|
$slide->text = '<p>Contenu</p>';
|
|
$slide->saveOrFail();
|
|
|
|
// Slide has no picture: storage must stay untouched.
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('fileExistsOnDisk');
|
|
$storage->shouldNotReceive('removeFromDisk');
|
|
$action = new DeleteSlide($storage, new PublicS3Disk('https://cdn.test'));
|
|
$input = new DeleteSlideInput(slide: $slide);
|
|
$executor = new ActionExecutorService(
|
|
new LaravelCacheLock(new Repository(new ArrayStore())),
|
|
new NullLogger(),
|
|
new LaravelDomainEventDispatcher(),
|
|
);
|
|
|
|
// WHEN
|
|
$result = $executor->execute($action, $input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
$this->assertDatabaseMissing('slides', [
|
|
'id' => $slide->id,
|
|
]);
|
|
expect(Slide::query()->find($slide->id))->toBeNull();
|
|
});
|
|
|
|
it('removes the picture from storage when the slide has one', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$slide = new Slide();
|
|
$slide->title = 'Slide avec image';
|
|
$slide->text = '<p>Contenu</p>';
|
|
$slide->picture = new StorageFilePath('slides/slide.jpg');
|
|
$slide->saveOrFail();
|
|
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldReceive('fileExistsOnDisk')->once()->andReturnTrue();
|
|
$storage->shouldReceive('removeFromDisk')->once()->andReturnTrue();
|
|
$action = new DeleteSlide($storage, new PublicS3Disk('https://cdn.test'));
|
|
$input = new DeleteSlideInput(slide: $slide);
|
|
$executor = new ActionExecutorService(
|
|
new LaravelCacheLock(new Repository(new ArrayStore())),
|
|
new NullLogger(),
|
|
new LaravelDomainEventDispatcher(),
|
|
);
|
|
|
|
// WHEN
|
|
$result = $executor->execute($action, $input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
$this->assertDatabaseMissing('slides', [
|
|
'id' => $slide->id,
|
|
]);
|
|
expect(Slide::query()->find($slide->id))->toBeNull();
|
|
});
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('fileExistsOnDisk');
|
|
$storage->shouldNotReceive('removeFromDisk');
|
|
$action = new DeleteSlide($storage, new PublicS3Disk('https://cdn.test'));
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN & THEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $action($invalidInput)
|
|
)->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|