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()), ), ); $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); }); 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()), ), ); $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); }); });