ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Ranges/CreateRangeTest.php

136 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Actions\Ranges;
use App\Contracts\ActionInput;
use App\Cqs\Actions\Ranges\CreateRange;
use App\Cqs\Actions\Ranges\CreateRangeInput;
use App\Lib\PublicS3Disk;
use App\Models\Range;
use App\Services\ActionExecutorService;
use App\Services\ImageOptimizerService;
use Domain\Models\ValueObjects\PictureMetadata;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;
use Illuminate\Http\UploadedFile;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Locks\LaravelCacheLock;
use Psr\Log\NullLogger;
use Shared\Filesystem\TemporaryFileFactory;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
describe('CreateRange action', function () {
test('it creates and persists a range in database', function () {
/** @var \Tests\TestCase $this */
// GIVEN
// Strict double: no S3 side effect, upload asserted.
$storage = \Mockery::mock(StorageService::class);
$storage->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: '<p>Une superbe gamme</p>',
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('<p>Une superbe gamme</p>')
->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);
});
});