211 lines
7.8 KiB
PHP
211 lines
7.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Slides;
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Cqs\Actions\Slides\UpdateSlide;
|
|
use App\Cqs\Actions\Slides\UpdateSlideInput;
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Models\Slide;
|
|
use App\Services\ActionExecutorService;
|
|
use App\Services\ImageOptimizerService;
|
|
use App\Services\PublicImageStorageService;
|
|
use Domain\Models\ValueObjects\DatePeriod;
|
|
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('UpdateSlide action', function (): void {
|
|
it('updates an existing slide without new picture', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$slide = new Slide();
|
|
$slide->title = 'Ancien Titre';
|
|
$slide->text = '<p>Ancien texte</p>';
|
|
$slide->saveOrFail();
|
|
|
|
// Strict double: no picture in this input, so storeFileOntoDisk must never be touched.
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('storeFileOntoDisk');
|
|
$action = new UpdateSlide(
|
|
new PublicImageStorageService(
|
|
$storage,
|
|
new PublicS3Disk('https://cdn.test'),
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
),
|
|
);
|
|
$start = new \DateTimeImmutable('2026-07-01 10:00:00');
|
|
$end = new \DateTimeImmutable('2026-07-31 20:00:00');
|
|
$period = new DatePeriod($start, $end);
|
|
|
|
$input = new UpdateSlideInput(
|
|
slide: $slide,
|
|
title: 'Titre Mis à Jour',
|
|
text: '<p>Texte mis à jour</p>',
|
|
link: 'https://example.com/updated',
|
|
linkLabel: 'Lien mis à jour',
|
|
linkNewTab: true,
|
|
connected: true,
|
|
period: $period,
|
|
);
|
|
|
|
// WHEN
|
|
$updatedSlide = $action($input);
|
|
|
|
// THEN
|
|
expect($updatedSlide->id)->toBe($slide->id)
|
|
->and($updatedSlide->title)->toBe('Titre Mis à Jour')
|
|
->and($updatedSlide->text)->toBe('<p>Texte mis à jour</p>')
|
|
->and($updatedSlide->link)->toBe('https://example.com/updated')
|
|
->and($updatedSlide->link_label)->toBe('Lien mis à jour')
|
|
->and($updatedSlide->link_new_tab)->toBeTrue()
|
|
->and($updatedSlide->connected)->toBeTrue();
|
|
|
|
$fresh = Slide::query()->find($slide->id);
|
|
expect($fresh)->not->toBeNull()
|
|
->and($fresh?->title)->toBe('Titre Mis à Jour')
|
|
->and($fresh?->text)->toBe('<p>Texte mis à jour</p>');
|
|
|
|
$this->assertDatabaseHas('slides', [
|
|
'id' => $slide->id,
|
|
'title' => 'Titre Mis à Jour',
|
|
]);
|
|
});
|
|
|
|
it('updates an existing slide with a new uploaded picture', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$slide = new Slide();
|
|
$slide->title = 'Ancien Titre';
|
|
$slide->text = '<p>Ancien texte</p>';
|
|
$slide->saveOrFail();
|
|
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldReceive('storeFileOntoDisk')->once()->andReturn(new StorageFilePath('slides/new-slide.jpg'));
|
|
$action = new UpdateSlide(
|
|
new PublicImageStorageService(
|
|
$storage,
|
|
new PublicS3Disk('https://cdn.test'),
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
),
|
|
);
|
|
|
|
$input = new UpdateSlideInput(
|
|
slide: $slide,
|
|
title: 'Titre Avec Image',
|
|
text: '<p>Texte avec image</p>',
|
|
picture: UploadedFile::fake()->image('new-slide.jpg', 600, 400),
|
|
pictureAlt: 'Nouvel alt',
|
|
pictureTitle: 'Nouveau titre',
|
|
);
|
|
|
|
// WHEN
|
|
$updatedSlide = $action($input);
|
|
|
|
// THEN
|
|
expect($updatedSlide->id)->toBe($slide->id)
|
|
->and($updatedSlide->title)->toBe('Titre Avec Image')
|
|
->and($updatedSlide->picture_alt)->toBe('Nouvel alt')
|
|
->and($updatedSlide->picture_title)->toBe('Nouveau titre')
|
|
->and($updatedSlide->picture)->toBeInstanceOf(StorageFilePath::class)
|
|
->and($updatedSlide->picture?->value)->toBe('slides/new-slide.jpg');
|
|
|
|
$fresh = Slide::query()->find($slide->id);
|
|
expect($fresh)->not->toBeNull()
|
|
->and($fresh?->title)->toBe('Titre Avec Image')
|
|
->and($fresh?->picture?->value)->toBe('slides/new-slide.jpg');
|
|
|
|
$this->assertDatabaseHas('slides', [
|
|
'id' => $slide->id,
|
|
'title' => 'Titre Avec Image',
|
|
'picture_alt' => 'Nouvel alt',
|
|
'picture_title' => 'Nouveau titre',
|
|
]);
|
|
});
|
|
|
|
it('deletes previous S3 picture when replacing slide image', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$oldPicture = new StorageFilePath('slides/old-slide.jpg');
|
|
$newPicture = new StorageFilePath('slides/new-slide.jpg');
|
|
|
|
$slide = new Slide();
|
|
$slide->title = 'Ancien Titre';
|
|
$slide->text = '<p>Ancien texte</p>';
|
|
$slide->picture = $oldPicture;
|
|
$slide->saveOrFail();
|
|
|
|
$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 === 'slides/old-slide.jpg'), $disk->disk)
|
|
->andReturnTrue();
|
|
$storage->shouldReceive('removeFromDisk')
|
|
->once()
|
|
->with(\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'slides/old-slide.jpg'), $disk->disk)
|
|
->andReturnTrue();
|
|
|
|
$action = new UpdateSlide(
|
|
new PublicImageStorageService(
|
|
$storage,
|
|
$disk,
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
),
|
|
);
|
|
|
|
$input = new UpdateSlideInput(
|
|
slide: $slide,
|
|
title: 'Titre Mis à Jour',
|
|
text: '<p>Texte</p>',
|
|
picture: UploadedFile::fake()->image('new-slide.jpg', 600, 400),
|
|
pictureAlt: 'Nouvel alt',
|
|
pictureTitle: 'Nouveau titre',
|
|
);
|
|
|
|
$executor = new ActionExecutorService(
|
|
new LaravelCacheLock(new Repository(new ArrayStore())),
|
|
new NullLogger(),
|
|
new LaravelDomainEventDispatcher(),
|
|
);
|
|
|
|
// WHEN
|
|
$updatedSlide = $executor->execute($action, $input);
|
|
|
|
// THEN
|
|
expect($updatedSlide->picture?->value)->toBe('slides/new-slide.jpg');
|
|
});
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('storeFileOntoDisk');
|
|
$action = new UpdateSlide(
|
|
new PublicImageStorageService(
|
|
$storage,
|
|
new PublicS3Disk('https://cdn.test'),
|
|
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
|
|
),
|
|
);
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $action($invalidInput)
|
|
)
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|