ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Slides/CreateSlideTest.php

82 lines
2.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\CreateSlide;
use App\Cqs\Actions\Slides\CreateSlideInput;
use App\Lib\PublicS3Disk;
use App\Models\Slide;
use App\Services\ImageOptimizerService;
use Domain\Models\ValueObjects\DatePeriod;
use Psr\Log\NullLogger;
use Shared\Filesystem\TemporaryFileFactory;
use Uwil\Storage\Contracts\StorageService;
describe('CreateSlide action', function (): void {
it('creates and persists a slide in database', function (): void {
// GIVEN
// Strict double: no picture in this input, so storage must never be touched.
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('storeFileOntoDisk');
$action = new CreateSlide(
$storage,
new PublicS3Disk('https://cdn.test'),
new ImageOptimizerService(new TemporaryFileFactory(), new NullLogger()),
);
$start = new \DateTimeImmutable('2026-06-01 10:00:00');
$end = new \DateTimeImmutable('2026-06-30 20:00:00');
$period = new DatePeriod($start, $end);
$input = new CreateSlideInput(
title: 'Nouvelle Bannière',
text: '<p>Contenu promo</p>',
link: 'https://example.com/promo',
linkLabel: 'En profiter',
linkNewTab: true,
connected: false,
period: $period,
);
// WHEN
$slide = $action($input);
// THEN
expect($slide)->toBeInstanceOf(Slide::class)
->and($slide->id)->toBeGreaterThan(0)
->and($slide->title)->toBe('Nouvelle Bannière')
->and($slide->text)->toBe('<p>Contenu promo</p>')
->and($slide->link)->toBe('https://example.com/promo')
->and($slide->link_label)->toBe('En profiter')
->and($slide->link_new_tab)->toBeTrue()
->and($slide->connected)->toBeFalse();
$fresh = Slide::query()->find($slide->id);
expect($fresh)->not->toBeNull()
->and($fresh?->title)->toBe('Nouvelle Bannière');
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$storage = \Mockery::mock(StorageService::class);
$storage->shouldNotReceive('storeFileOntoDisk');
$action = new CreateSlide(
$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);
});
});