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 (): void { // 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(); /** @var StorageService&\Mockery\MockInterface $storage */ $storage = \Mockery::spy(StorageService::class); $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(Range::query()->find($range->id))->toBeNull(); $storage->shouldNotHaveReceived('fileExistsOnDisk'); $storage->shouldNotHaveReceived('removeFromDisk'); }); 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 { }; /** @var \Closure(\App\Contracts\ActionInput): mixed $callable */ $callable = \Closure::fromCallable($action); // WHEN & THEN expect(fn () => $callable($invalidInput)) ->toThrow(\InvalidArgumentException::class); }); });