shouldReceive('storeFileOntoDisk')->once()->andReturn(new StorageFilePath('media/range.jpg')); $action = new CreateRange( $storage, new PublicS3Disk('https://cdn.test'), new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()), ); $input = new CreateRangeInput( name: 'Gamme Décoration', description: '

Une superbe gamme

', picture: UploadedFile::fake()->image('range.jpg', 500, 500), pictureMetadata: new PictureMetadata(alt: 'Image alt range', title: 'Image title range'), status: true, ); // WHEN $range = $action($input); // THEN expect($range->name)->toBe('Gamme Décoration') ->and($range->slug)->toBe('gamme-decoration') ->and($range->description)->toBe('

Une superbe gamme

') ->and($range->picture_alt)->toBe('Image alt range') ->and($range->picture_title)->toBe('Image title range') ->and($range->status)->toBeTrue() ->and($range->exists)->toBeTrue(); $this->assertDatabaseHas('ranges', [ 'id' => $range->id, 'name' => 'Gamme Décoration', 'slug' => 'gamme-decoration', ]); }); test('it throws when an invalid input is passed', function () { // GIVEN $storage = \Mockery::mock(StorageService::class); $storage->shouldNotReceive('storeFileOntoDisk'); $action = new CreateRange( $storage, new PublicS3Disk('https://cdn.test'), new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()), ); $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); }); test('it cleans up uploaded S3 picture when SQL persistence rolls back', function () { /** @var \Tests\TestCase $this */ // GIVEN $existing = new Range(); $existing->name = 'Gamme Existante'; $existing->picture_alt = 'Alt'; $existing->picture_title = 'Title'; $existing->status = true; $existing->save(); $disk = new PublicS3Disk('https://cdn.test'); $storage = \Mockery::mock(StorageService::class); $storage->shouldReceive('storeFileOntoDisk') ->once() ->andReturn(new StorageFilePath('media/orphan-create-range.jpg')); $storage->shouldReceive('fileExistsOnDisk') ->once() ->with( \Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-create-range.jpg'), $disk->disk, ) ->andReturnTrue(); $storage->shouldReceive('removeFromDisk') ->once() ->with( \Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-create-range.jpg'), $disk->disk, ) ->andReturnTrue(); $action = new CreateRange( $storage, $disk, new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()), ); $input = new CreateRangeInput( name: 'Gamme Existante', description: 'Duplicate trigger', picture: UploadedFile::fake()->image('range.jpg', 500, 500), pictureMetadata: new PictureMetadata(alt: 'Alt valide', title: 'Titre valide'), 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); }); });