2026-09-29 16:59:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Ranges;
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
use App\Contracts\ActionInput;
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Ranges\UpdateRange;
|
|
|
|
|
use App\Cqs\Actions\Ranges\UpdateRangeInput;
|
2026-11-14 16:06:12 +01:00
|
|
|
use App\Lib\PublicS3Disk;
|
2026-09-29 16:59:07 +02:00
|
|
|
use App\Models\Range;
|
2026-11-15 20:31:37 +01:00
|
|
|
use App\Services\ActionExecutorService;
|
2026-11-14 16:06:12 +01:00
|
|
|
use App\Services\ImageOptimizerService;
|
2026-11-15 17:13:39 +01:00
|
|
|
use App\Services\PublicImageStorageService;
|
2026-09-29 16:59:07 +02:00
|
|
|
use Domain\Models\ValueObjects\PictureMetadata;
|
2026-11-15 20:31:37 +01:00
|
|
|
use Illuminate\Cache\ArrayStore;
|
|
|
|
|
use Illuminate\Cache\Repository;
|
2026-09-29 16:59:07 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2026-11-15 20:31:37 +01:00
|
|
|
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
|
|
|
|
|
use Infrastructure\Adapters\Locks\LaravelCacheLock;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use Shared\Filesystem\TemporaryFileFactory;
|
|
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
2026-09-29 16:59:07 +02:00
|
|
|
|
|
|
|
|
describe('UpdateRange action', function () {
|
|
|
|
|
test('it updates an existing range in database', function () {
|
2026-10-02 10:53:21 +02:00
|
|
|
/** @var \Tests\TestCase $this */
|
2026-09-29 16:59:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$range = new Range();
|
|
|
|
|
$range->name = 'Gamme Initiale';
|
|
|
|
|
$range->description = 'Ancien contenu';
|
|
|
|
|
$range->picture_alt = 'Ancien alt';
|
|
|
|
|
$range->picture_title = 'Ancien title';
|
|
|
|
|
$range->status = true;
|
|
|
|
|
$range->save();
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
// 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(
|
2026-11-15 17:13:39 +01:00
|
|
|
new PublicImageStorageService(
|
|
|
|
|
$storage,
|
|
|
|
|
new PublicS3Disk('https://cdn.test'),
|
|
|
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
|
|
|
),
|
2026-11-14 16:06:12 +01:00
|
|
|
);
|
2026-09-29 16:59:07 +02:00
|
|
|
$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,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 17:13:39 +01:00
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 20:31:37 +01:00
|
|
|
$executor = new ActionExecutorService(
|
|
|
|
|
new LaravelCacheLock(new Repository(new ArrayStore())),
|
|
|
|
|
new NullLogger(),
|
|
|
|
|
new LaravelDomainEventDispatcher(),
|
|
|
|
|
);
|
|
|
|
|
|
2026-11-15 17:13:39 +01:00
|
|
|
// WHEN
|
2026-11-15 20:31:37 +01:00
|
|
|
$updatedRange = $executor->execute($action, $input);
|
2026-11-15 17:13:39 +01:00
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($updatedRange->picture?->value)->toBe('media/new-range.jpg');
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-15 20:31:37 +01:00
|
|
|
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()
|
2026-11-16 16:29:17 +01:00
|
|
|
->with(
|
|
|
|
|
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-new-range.jpg'),
|
|
|
|
|
$disk->disk,
|
|
|
|
|
)
|
2026-11-15 20:31:37 +01:00
|
|
|
->andReturnTrue();
|
|
|
|
|
$storage->shouldReceive('removeFromDisk')
|
|
|
|
|
->once()
|
2026-11-16 16:29:17 +01:00
|
|
|
->with(
|
|
|
|
|
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'media/orphan-new-range.jpg'),
|
|
|
|
|
$disk->disk,
|
|
|
|
|
)
|
2026-11-15 20:31:37 +01:00
|
|
|
->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);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
test('it throws when an invalid input is passed', function () {
|
|
|
|
|
// GIVEN
|
2026-11-14 16:06:12 +01:00
|
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
|
|
|
$storage->shouldNotReceive('storeFileOntoDisk');
|
|
|
|
|
$action = new UpdateRange(
|
2026-11-15 17:13:39 +01:00
|
|
|
new PublicImageStorageService(
|
|
|
|
|
$storage,
|
|
|
|
|
new PublicS3Disk('https://cdn.test'),
|
|
|
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
|
|
|
),
|
2026-11-14 16:06:12 +01:00
|
|
|
);
|
2026-09-29 16:59:07 +02:00
|
|
|
$invalidInput = new class () implements ActionInput
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-16 16:13:14 +01:00
|
|
|
/** @var \Closure(\App\Contracts\ActionInput): mixed $callable */
|
|
|
|
|
$callable = \Closure::fromCallable($action);
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
// WHEN & THEN
|
2026-11-16 16:13:14 +01:00
|
|
|
expect(fn () => $callable($invalidInput))
|
2026-10-02 10:53:21 +02:00
|
|
|
->toThrow(\InvalidArgumentException::class);
|
2026-09-29 16:59:07 +02:00
|
|
|
});
|
|
|
|
|
});
|