refactor(slides): implement CQS actions, query, DTO, and DatePeriod VO for slides

This commit is contained in:
Jérémy Munsch 2026-09-29 15:29:43 +02:00
parent 1fd2a3a1d8
commit e2e2a357f7
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
26 changed files with 1166 additions and 70 deletions

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Models\Slide;
use Illuminate\Support\Carbon;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\Slides\CreateSlideInput, \App\Models\Slide>
*/
final class CreateSlide implements Action
{
/**
* @param \App\Actions\Slides\CreateSlideInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): Slide
{
Assert::isInstanceOf($input, CreateSlideInput::class);
$slide = new Slide();
$slide->title = $input->title;
$slide->text = $input->text;
if ($input->picture !== null) {
$slide->picture = $input->picture;
}
$slide->picture_alt = $input->pictureAlt;
$slide->picture_title = $input->pictureTitle;
$slide->link = $input->link;
$slide->link_label = $input->linkLabel;
$slide->link_new_tab = $input->linkNewTab;
$slide->connected = $input->connected;
$slide->date_start = $input->period->start !== null ? Carbon::instance($input->period->start) : null;
$slide->date_end = $input->period->end !== null ? Carbon::instance($input->period->end) : null;
$slide->saveOrFail();
return $slide;
}
}

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\ActionInput;
use Domain\Models\ValueObjects\DatePeriod;
use Illuminate\Http\UploadedFile;
use Webmozart\Assert\Assert;
final readonly class CreateSlideInput implements ActionInput
{
public function __construct(
public string $title,
public ?string $text = null,
public UploadedFile | string | null $picture = null,
public ?string $pictureAlt = null,
public ?string $pictureTitle = null,
public ?string $link = null,
public ?string $linkLabel = null,
public bool $linkNewTab = false,
public bool $connected = false,
public DatePeriod $period = new DatePeriod(),
) {
Assert::stringNotEmpty(trim($this->title));
Assert::maxLength($this->title, 75);
Assert::true(
$this->picture !== null || ($this->text !== null && trim($this->text) !== ''),
'A slide must have either a picture or text.'
);
if ($this->pictureAlt !== null) {
Assert::maxLength($this->pictureAlt, 75);
}
if ($this->pictureTitle !== null) {
Assert::maxLength($this->pictureTitle, 75);
}
if ($this->link !== null) {
Assert::maxLength($this->link, 255);
}
if ($this->linkLabel !== null) {
Assert::maxLength($this->linkLabel, 255);
}
}
}

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\Slides\DeleteSlideInput, bool>
*/
final class DeleteSlide implements Action
{
/**
* @param \App\Actions\Slides\DeleteSlideInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): bool
{
Assert::isInstanceOf($input, DeleteSlideInput::class);
return (bool) $input->slide->deleteOrFail();
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\ActionInput;
use App\Models\Slide;
final readonly class DeleteSlideInput implements ActionInput
{
public function __construct(
public Slide $slide,
) {
}
}

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Models\Slide;
use Illuminate\Support\Carbon;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Action<\App\Actions\Slides\UpdateSlideInput, \App\Models\Slide>
*/
final class UpdateSlide implements Action
{
/**
* @param \App\Actions\Slides\UpdateSlideInput $input
*/
#[\Override]
public function __invoke(ActionInput $input): Slide
{
Assert::isInstanceOf($input, UpdateSlideInput::class);
$slide = $input->slide;
$slide->title = $input->title;
$slide->text = $input->text;
if ($input->picture !== null) {
$slide->picture = $input->picture;
}
$slide->picture_alt = $input->pictureAlt;
$slide->picture_title = $input->pictureTitle;
$slide->link = $input->link;
$slide->link_label = $input->linkLabel;
$slide->link_new_tab = $input->linkNewTab;
$slide->connected = $input->connected;
$slide->date_start = $input->period->start !== null ? Carbon::instance($input->period->start) : null;
$slide->date_end = $input->period->end !== null ? Carbon::instance($input->period->end) : null;
$slide->saveOrFail();
return $slide;
}
}

View file

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace App\Actions\Slides;
use App\Contracts\ActionInput;
use App\Models\Slide;
use Domain\Models\ValueObjects\DatePeriod;
use Illuminate\Http\UploadedFile;
use Webmozart\Assert\Assert;
final readonly class UpdateSlideInput implements ActionInput
{
public function __construct(
public Slide $slide,
public string $title,
public ?string $text = null,
public UploadedFile | string | null $picture = null,
public ?string $pictureAlt = null,
public ?string $pictureTitle = null,
public ?string $link = null,
public ?string $linkLabel = null,
public bool $linkNewTab = false,
public bool $connected = false,
public DatePeriod $period = new DatePeriod(),
) {
Assert::stringNotEmpty(trim($this->title));
Assert::maxLength($this->title, 75);
Assert::true(
$this->picture !== null || ! empty($this->slide->picture) || ($this->text !== null && trim($this->text) !== ''),
'A slide must have either a picture or text.'
);
if ($this->pictureAlt !== null) {
Assert::maxLength($this->pictureAlt, 75);
}
if ($this->pictureTitle !== null) {
Assert::maxLength($this->pictureTitle, 75);
}
if ($this->link !== null) {
Assert::maxLength($this->link, 255);
}
if ($this->linkLabel !== null) {
Assert::maxLength($this->linkLabel, 255);
}
}
}

102
app/Dto/SlideDto.php Normal file
View file

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Dto;
use App\Models\Slide;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Carbon;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class SlideDto implements Arrayable, UrlRoutable
{
public function __construct(
public int $id,
public string $title,
public ?string $text,
public ?string $picture,
public ?string $picture_alt,
public ?string $picture_title,
public ?string $link,
public ?string $link_label,
public bool $link_new_tab,
public bool $connected,
public ?Carbon $date_start,
public ?Carbon $date_end,
public int $order,
public bool $status,
) {
}
public static function fromModel(Slide $slide): self
{
return new self(
id: $slide->id,
title: $slide->title,
text: $slide->text,
picture: is_string($slide->picture) ? $slide->picture : null,
picture_alt: $slide->picture_alt,
picture_title: $slide->picture_title,
link: $slide->link,
link_label: $slide->link_label,
link_new_tab: $slide->link_new_tab,
connected: $slide->connected,
date_start: $slide->date_start,
date_end: $slide->date_end,
order: $slide->order,
status: $slide->status,
);
}
#[\Override]
public function getRouteKey(): mixed
{
return $this->id;
}
#[\Override]
public function getRouteKeyName(): string
{
return 'id';
}
#[\Override]
public function resolveRouteBinding($value, $field = null): mixed
{
return null;
}
#[\Override]
public function resolveChildRouteBinding($childType, $value, $field): mixed
{
return null;
}
/**
* @return array<string, mixed>
*/
#[\Override]
public function toArray(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'text' => $this->text,
'picture' => $this->picture,
'picture_alt' => $this->picture_alt,
'picture_title' => $this->picture_title,
'link' => $this->link,
'link_label' => $this->link_label,
'link_new_tab' => $this->link_new_tab,
'connected' => $this->connected,
'date_start' => $this->date_start,
'date_end' => $this->date_end,
'order' => $this->order,
'status' => $this->status,
];
}
}

View file

@ -4,16 +4,25 @@ declare(strict_types=1);
namespace App\Http\Controllers\Bo;
use App\Actions\Slides\CreateSlide;
use App\Actions\Slides\CreateSlideInput;
use App\Actions\Slides\DeleteSlide;
use App\Actions\Slides\DeleteSlideInput;
use App\Actions\Slides\UpdateSlide;
use App\Actions\Slides\UpdateSlideInput;
use App\Contracts\ActionExecutor;
use App\Dto\SlideDto;
use App\Http\Controllers\Controller;
use App\Http\Requests\Bo\Slides\StoreSlideRequest;
use App\Http\Requests\Bo\Slides\UpdateSlideRequest;
use App\Models\Slide;
use App\Services\DataTableService;
use App\Queries\Slides\GetPaginatedSlidesQuery;
use App\Traits\Controllers\ChangesModelOrder;
use App\Traits\Controllers\TogglesModelStatus;
use Domain\Models\ValueObjects\DatePeriod;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
class SlidesController extends Controller
@ -21,15 +30,16 @@ class SlidesController extends Controller
use ChangesModelOrder;
use TogglesModelStatus;
public function index(DataTableService $dataTable): View
public function __construct(
private readonly ActionExecutor $executor,
) {
}
public function index(GetPaginatedSlidesQuery $query): View
{
$this->authorize('viewAny', Slide::class);
$query = Slide::query();
$dataTable->sortQuery(Slide::class, $query);
$slides = $dataTable->paginateQuery($query);
$slides = $query->execute();
return view('bo.slides.index', compact('slides'));
}
@ -41,73 +51,102 @@ class SlidesController extends Controller
return view('bo.slides.create');
}
public function store(StoreSlideRequest $request): RedirectResponse
public function store(StoreSlideRequest $request, CreateSlide $action): RedirectResponse
{
$this->authorize('create', Slide::class);
return DB::transaction(function () use ($request) {
$slide = new Slide();
$slide->fill($request->only(
$slide->getFillable()
));
if ($slide->saveOrFail()) {
return to_route('bo.slides.edit', $slide)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
}
$period = new DatePeriod(
start: $request->filled('date_start') ? new \DateTimeImmutable($request->string('date_start')->value()) : null,
end: $request->filled('date_end') ? new \DateTimeImmutable($request->string('date_end')->value()) : null,
);
return to_route('bo.slides.create')
->with('error', trans('crud.messages.cannot_be_created', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
});
$slide = $this->executor->execute(
$action,
new CreateSlideInput(
title: $request->string('title')->value(),
text: $request->filled('text') ? $request->string('text')->value() : null,
picture: $this->resolvePicture($request),
pictureAlt: $request->filled('picture_alt') ? $request->string('picture_alt')->value() : null,
pictureTitle: $request->filled('picture_title') ? $request->string('picture_title')->value() : null,
link: $request->filled('link') ? $request->string('link')->value() : null,
linkLabel: $request->filled('link_label') ? $request->string('link_label')->value() : null,
linkNewTab: $request->boolean('link_new_tab'),
connected: $request->boolean('connected'),
period: $period,
),
);
return to_route('bo.slides.edit', $slide)
->with('success', trans('crud.messages.has_been_created', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
}
public function edit(Slide $slide): View
{
$this->authorize('update', $slide);
return view('bo.slides.edit', compact('slide'));
return view('bo.slides.edit', [
'slide' => SlideDto::fromModel($slide),
]);
}
public function update(UpdateSlideRequest $request, Slide $slide): RedirectResponse
public function update(UpdateSlideRequest $request, Slide $slide, UpdateSlide $action): RedirectResponse
{
$this->authorize('update', $slide);
return DB::transaction(function () use ($request, $slide) {
$slide->fill($request->only(
$slide->getFillable()
));
$period = new DatePeriod(
start: $request->filled('date_start') ? new \DateTimeImmutable($request->string('date_start')->value()) : null,
end: $request->filled('date_end') ? new \DateTimeImmutable($request->string('date_end')->value()) : null,
);
if ($slide->saveOrFail()) {
return to_route('bo.slides.edit', $slide)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
}
$this->executor->execute(
$action,
new UpdateSlideInput(
slide: $slide,
title: $request->string('title')->value(),
text: $request->filled('text') ? $request->string('text')->value() : null,
picture: $this->resolvePicture($request),
pictureAlt: $request->filled('picture_alt') ? $request->string('picture_alt')->value() : null,
pictureTitle: $request->filled('picture_title') ? $request->string('picture_title')->value() : null,
link: $request->filled('link') ? $request->string('link')->value() : null,
linkLabel: $request->filled('link_label') ? $request->string('link_label')->value() : null,
linkNewTab: $request->boolean('link_new_tab'),
connected: $request->boolean('connected'),
period: $period,
),
);
return to_route('bo.slides.edit', $slide)
->with('error', trans('crud.messages.cannot_be_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
});
return to_route('bo.slides.edit', $slide)
->with('success', trans('crud.messages.has_been_updated', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
}
public function destroy(Slide $slide): RedirectResponse
public function destroy(Slide $slide, DeleteSlide $action): RedirectResponse
{
$this->authorize('delete', $slide);
return DB::transaction(function () use ($slide) {
if ($slide->deleteOrFail()) {
return to_route('bo.slides.index')
->with('warning', trans('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.slides', 1))]));
}
$this->executor->execute(
$action,
new DeleteSlideInput(slide: $slide),
);
return to_route('bo.slides.edit', $slide)
->with('error', trans('crud.messages.cannot_be_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
});
return to_route('bo.slides.index')
->with('warning', trans('crud.messages.has_been_deleted', [
'model' => Str::ucfirst(trans_choice('models.classes.slides', 1)),
]));
}
private function resolvePicture(StoreSlideRequest | UpdateSlideRequest $request): UploadedFile | string | null
{
$file = $request->file('picture');
if ($file instanceof UploadedFile) {
return $file;
}
return $request->has('picture') && is_string($request->input('picture'))
? $request->input('picture')
: null;
}
}

View file

@ -21,7 +21,7 @@ use Shared\Helpers\DatesHelper;
* @property int $id
* @property string $title
* @property string|null $text
* @property string|null $picture
* @property string|\Illuminate\Http\UploadedFile|null $picture
* @property string|null $picture_alt
* @property string|null $picture_title
* @property string|null $link_label

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Policies\Backend;
use App\Dto\SlideDto;
use App\Models\Customer;
use App\Models\Slide;
use App\Models\User;
@ -20,7 +21,7 @@ class SlidePolicy
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function view(User | Customer $user, Slide $_): bool
public function view(User | Customer $user, Slide | SlideDto $_): bool
{
if ($user instanceof Customer) {
return false;
@ -38,7 +39,7 @@ class SlidePolicy
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function update(User | Customer $user, Slide $_): bool
public function update(User | Customer $user, Slide | SlideDto $_): bool
{
if ($user instanceof Customer) {
return false;
@ -47,7 +48,7 @@ class SlidePolicy
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function delete(User | Customer $user, Slide $_): bool
public function delete(User | Customer $user, Slide | SlideDto $_): bool
{
if ($user instanceof Customer) {
return false;

View file

@ -6,10 +6,12 @@ namespace App\Providers;
use App\Dto\FaqDto;
use App\Dto\PageDto;
use App\Dto\SlideDto;
use App\Dto\TagDto;
use App\Models\Customer;
use App\Policies\Backend\FaqPolicy;
use App\Policies\Backend\PagePolicy;
use App\Policies\Backend\SlidePolicy;
use App\Policies\Backend\TagPolicy;
use App\Policies\Frontend\BasketPolicy;
use App\Services\CrmService;
@ -36,6 +38,7 @@ class AuthServiceProvider extends ServiceProvider
BasketProduct::class => BasketPolicy::class,
FaqDto::class => FaqPolicy::class,
PageDto::class => PagePolicy::class,
SlideDto::class => SlidePolicy::class,
TagDto::class => TagPolicy::class,
];

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Queries\Slides;
use App\Contracts\Query;
use App\Dto\SlideDto;
use App\Models\Slide;
use App\Services\DataTableService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/**
* @implements \App\Contracts\Query<\Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\SlideDto>>
*/
final readonly class GetPaginatedSlidesQuery implements Query
{
public function __construct(
private DataTableService $dataTable,
) {
}
/**
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\SlideDto>
*/
public function execute(): LengthAwarePaginator
{
$query = Slide::query();
$this->dataTable->sortQuery(Slide::class, $query);
$paginator = $this->dataTable->paginateQuery($query);
return $paginator->through(fn (Slide $slide): SlideDto => SlideDto::fromModel($slide));
}
}

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
use Webmozart\Assert\Assert;
final readonly class DatePeriod
{
public ?\DateTimeImmutable $start;
public ?\DateTimeImmutable $end;
public function __construct(
?\DateTimeInterface $start = null,
?\DateTimeInterface $end = null,
) {
$this->start = $start !== null ? \DateTimeImmutable::createFromInterface($start) : null;
$this->end = $end !== null ? \DateTimeImmutable::createFromInterface($end) : null;
if ($this->start !== null && $this->end !== null) {
Assert::true($this->start <= $this->end, 'The start date must be before or equal to the end date.');
}
}
public function isActiveAt(?\DateTimeInterface $date = null): bool
{
$target = $date !== null ? \DateTimeImmutable::createFromInterface($date) : new \DateTimeImmutable();
if ($this->start !== null && $target < $this->start) {
return false;
}
if ($this->end !== null && $target > $this->end) {
return false;
}
return true;
}
}

View file

@ -3,6 +3,7 @@
declare(strict_types=1);
use App\Dto\PageDto;
use App\Dto\SlideDto;
use App\Models\Discount;
use App\Models\Faq;
use App\Models\Page;
@ -34,7 +35,7 @@ Breadcrumbs::for('bo.slides.create', function (Generator $trail) use ($creationL
$trail->parent('bo.slides.index');
$trail->push($creationLabel);
});
Breadcrumbs::for('bo.slides.edit', function (Generator $trail, Slide $slide) use ($editionLabel): void {
Breadcrumbs::for('bo.slides.edit', function (Generator $trail, Slide | SlideDto $slide) use ($editionLabel): void {
$trail->parent('bo.slides.index');
$trail->push($editionLabel, route('bo.slides.edit', $slide));
});

View file

@ -2,28 +2,35 @@
declare(strict_types=1);
use App\Dto\SlideDto;
use App\Models\Slide;
use App\Models\User;
use Domain\Enums\UserRole;
describe('SlidesController', function () {
beforeEach(function () {
describe('SlidesController', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($this->user, 'backend');
});
it('can access index', function () {
it('can access index', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Slide::factory()->create();
// WHEN
$response = $this->get('/bo/slides');
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('bo.slides.index');
$response->assertViewHas('slides', fn ($slides) => $slides->items()[0] instanceof SlideDto);
});
it('can access create', function () {
it('can access create', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
$response = $this->get('/bo/slides/create');
@ -32,7 +39,8 @@ describe('SlidesController', function () {
$response->assertViewIs('bo.slides.create');
});
it('can store a slide', function () {
it('can store a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$data = [
'title' => 'New Slide Title',
@ -50,12 +58,13 @@ describe('SlidesController', function () {
$response = $this->post('/bo/slides', $data);
// THEN
$slide = Slide::where('title', 'New Slide Title')->first();
$slide = Slide::query()->where('title', 'New Slide Title')->first();
expect($slide)->not->toBeNull();
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
});
it('can access edit', function () {
it('can access edit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
@ -65,9 +74,11 @@ describe('SlidesController', function () {
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('bo.slides.edit');
$response->assertViewHas('slide', fn ($slideData) => $slideData instanceof SlideDto && $slideData->id === $slide->id);
});
it('can update a slide', function () {
it('can update a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
@ -92,7 +103,8 @@ describe('SlidesController', function () {
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
});
it('can delete a slide', function () {
it('can delete a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
@ -100,11 +112,12 @@ describe('SlidesController', function () {
$response = $this->delete("/bo/slides/{$slide->id}");
// THEN
expect(Slide::find($slide->id))->toBeNull();
expect(Slide::query()->find($slide->id))->toBeNull();
$response->assertRedirect('/bo/slides');
});
it('can change order', function () {
it('can change order', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide1 = Slide::factory()->create(['order' => 1]);
$slide2 = Slide::factory()->create(['order' => 2]);

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\CreateSlide;
use App\Actions\Slides\CreateSlideInput;
use App\Contracts\ActionInput;
use App\Models\Slide;
use Domain\Models\ValueObjects\DatePeriod;
describe('CreateSlide action', function (): void {
it('creates and persists a slide in database', function (): void {
// GIVEN
$action = new CreateSlide();
$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
$action = new CreateSlide();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\DeleteSlide;
use App\Actions\Slides\DeleteSlideInput;
use App\Contracts\ActionInput;
use App\Models\Slide;
describe('DeleteSlide action', function (): void {
it('deletes an existing slide from database', function (): void {
// GIVEN
$slide = new Slide();
$slide->title = 'Slide à supprimer';
$slide->text = '<p>Contenu</p>';
$slide->saveOrFail();
$action = new DeleteSlide();
$input = new DeleteSlideInput(slide: $slide);
// WHEN
$result = $action($input);
// THEN
expect($result)->toBeTrue();
expect(Slide::query()->find($slide->id))->toBeNull();
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new DeleteSlide();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\UpdateSlide;
use App\Actions\Slides\UpdateSlideInput;
use App\Contracts\ActionInput;
use App\Models\Slide;
use Domain\Models\ValueObjects\DatePeriod;
describe('UpdateSlide action', function (): void {
it('updates an existing slide in database', function (): void {
// GIVEN
$slide = new Slide();
$slide->title = 'Ancien Titre';
$slide->text = '<p>Ancien texte</p>';
$slide->saveOrFail();
$action = new UpdateSlide();
$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>');
});
it('throws when an invalid input is passed', function (): void {
// GIVEN
$action = new UpdateSlide();
$invalidInput = new class () implements ActionInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
use App\Dto\SlideDto;
use App\Models\Slide;
use Illuminate\Support\Carbon;
describe('SlideDto integration', function (): void {
it('creates a DTO from Slide model with booted application', function (): void {
// GIVEN
$dateStart = Carbon::parse('2026-06-01 10:00:00');
$dateEnd = Carbon::parse('2026-06-30 20:00:00');
$slide = new Slide();
$slide->id = 42;
$slide->title = 'Bannière Été';
$slide->text = '<p>Découvrez notre nouvelle collection</p>';
$slide->picture = 'storage/modelfiles/slides/26/06/01/ab/summer.jpg';
$slide->picture_alt = 'Collection Été';
$slide->picture_title = 'Bannière promotionnelle été';
$slide->link = 'https://example.com/ete';
$slide->link_label = 'Voir les offres';
$slide->link_new_tab = true;
$slide->connected = false;
$slide->date_start = $dateStart;
$slide->date_end = $dateEnd;
$slide->order = 3;
$slide->status = true;
// WHEN
$dto = SlideDto::fromModel($slide);
// THEN
expect($dto->id)->toBe(42)
->and($dto->title)->toBe('Bannière Été')
->and($dto->text)->toBe('<p>Découvrez notre nouvelle collection</p>')
->and($dto->picture)->toBe('storage/modelfiles/slides/26/06/01/ab/summer.jpg')
->and($dto->picture_alt)->toBe('Collection Été')
->and($dto->picture_title)->toBe('Bannière promotionnelle été')
->and($dto->link)->toBe('https://example.com/ete')
->and($dto->link_label)->toBe('Voir les offres')
->and($dto->link_new_tab)->toBeTrue()
->and($dto->connected)->toBeFalse()
->and($dto->date_start)->toEqual($dateStart)
->and($dto->date_end)->toEqual($dateEnd)
->and($dto->order)->toBe(3)
->and($dto->status)->toBeTrue();
});
});

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
use App\Dto\SlideDto;
use App\Models\Slide;
use App\Queries\Slides\GetPaginatedSlidesQuery;
describe('GetPaginatedSlidesQuery', function (): void {
it('returns a paginator of SlideDto instances', function (): void {
// GIVEN
$slide1 = new Slide();
$slide1->title = 'Premier Slide';
$slide1->text = 'Texte premier slide';
$slide1->order = 1;
$slide1->save();
$slide2 = new Slide();
$slide2->title = 'Deuxième Slide';
$slide2->text = 'Texte deuxième slide';
$slide2->order = 2;
$slide2->save();
/** @var GetPaginatedSlidesQuery $query */
$query = app(GetPaginatedSlidesQuery::class);
// WHEN
$paginator = $query->execute();
// THEN
expect($paginator->total())->toBe(2)
->and($paginator->items())->toHaveCount(2)
->and($paginator->items()[0])->toBeInstanceOf(SlideDto::class)
->and($paginator->items()[1])->toBeInstanceOf(SlideDto::class);
});
});

View file

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\CreateSlideInput;
use Domain\Models\ValueObjects\DatePeriod;
describe('CreateSlideInput', function (): void {
it('creates a valid CreateSlideInput instance with text', function (): void {
// GIVEN
$title = 'Nouveau Slide';
$text = '<p>Contenu du slide</p>';
$period = new DatePeriod();
// WHEN
$input = new CreateSlideInput(
title: $title,
text: $text,
period: $period,
);
// THEN
expect($input->title)->toBe($title)
->and($input->text)->toBe($text)
->and($input->picture)->toBeNull()
->and($input->period)->toBe($period);
});
it('creates a valid CreateSlideInput instance with picture', function (): void {
// GIVEN
$title = 'Nouveau Slide Photo';
$picture = 'storage/modelfiles/slides/picture.jpg';
// WHEN
$input = new CreateSlideInput(
title: $title,
picture: $picture,
pictureAlt: 'Description alternative',
pictureTitle: 'Titre image',
);
// THEN
expect($input->title)->toBe($title)
->and($input->picture)->toBe($picture)
->and($input->pictureAlt)->toBe('Description alternative')
->and($input->pictureTitle)->toBe('Titre image');
});
it('throws when title is empty', function (): void {
// GIVEN
$emptyTitle = ' ';
// WHEN
expect(fn () => new CreateSlideInput(title: $emptyTitle, text: 'Texte'))
// THEN
->toThrow(\InvalidArgumentException::class);
});
it('throws when title exceeds 75 characters', function (): void {
// GIVEN
$tooLongTitle = str_repeat('a', 76);
// WHEN
expect(fn () => new CreateSlideInput(title: $tooLongTitle, text: 'Texte'))
// THEN
->toThrow(\InvalidArgumentException::class);
});
it('throws when neither picture nor text is provided', function (): void {
// GIVEN
$title = 'Slide sans contenu';
// WHEN
expect(fn () => new CreateSlideInput(title: $title))
// THEN
->toThrow(\InvalidArgumentException::class, 'A slide must have either a picture or text.');
});
it('throws when picture_alt exceeds 75 characters', function (): void {
// GIVEN
$tooLongAlt = str_repeat('a', 76);
// WHEN
expect(fn () => new CreateSlideInput(title: 'Slide', picture: 'pic.jpg', pictureAlt: $tooLongAlt))
// THEN
->toThrow(\InvalidArgumentException::class);
});
it('throws when link exceeds 255 characters', function (): void {
// GIVEN
$tooLongLink = 'https://example.com/' . str_repeat('a', 256);
// WHEN
expect(fn () => new CreateSlideInput(title: 'Slide', text: 'Texte', link: $tooLongLink))
// THEN
->toThrow(\InvalidArgumentException::class);
});
});

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\DeleteSlideInput;
use App\Models\Slide;
describe('DeleteSlideInput', function (): void {
it('creates a valid DeleteSlideInput instance', function (): void {
// GIVEN
$slide = new Slide();
// WHEN
$input = new DeleteSlideInput(slide: $slide);
// THEN
expect($input->slide)->toBe($slide);
});
});

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
use App\Actions\Slides\UpdateSlideInput;
use App\Models\Slide;
describe('UpdateSlideInput', function (): void {
it('creates a valid UpdateSlideInput instance with existing slide picture', function (): void {
// GIVEN
$slide = new Slide();
$slide->picture = 'storage/modelfiles/slides/existing.jpg';
$title = 'Slide Modifié';
// WHEN
$input = new UpdateSlideInput(
slide: $slide,
title: $title,
);
// THEN
expect($input->slide)->toBe($slide)
->and($input->title)->toBe($title)
->and($input->picture)->toBeNull();
});
it('creates a valid UpdateSlideInput instance with new text', function (): void {
// GIVEN
$slide = new Slide();
$title = 'Slide Modifié Texte';
$text = '<p>Nouveau texte</p>';
// WHEN
$input = new UpdateSlideInput(
slide: $slide,
title: $title,
text: $text,
);
// THEN
expect($input->slide)->toBe($slide)
->and($input->title)->toBe($title)
->and($input->text)->toBe($text);
});
it('throws when title is empty', function (): void {
// GIVEN
$slide = new Slide();
// WHEN
expect(fn () => new UpdateSlideInput(slide: $slide, title: ' ', text: 'Texte'))
// THEN
->toThrow(\InvalidArgumentException::class);
});
it('throws when neither new nor existing picture nor text exists', function (): void {
// GIVEN
$slide = new Slide();
// WHEN
expect(fn () => new UpdateSlideInput(slide: $slide, title: 'Slide'))
// THEN
->toThrow(\InvalidArgumentException::class, 'A slide must have either a picture or text.');
});
});

View file

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
use App\Dto\SlideDto;
use Illuminate\Support\Carbon;
describe('SlideDto', function (): void {
it('creates a DTO from Slide model', function (): void {
// GIVEN
$dateStart = Carbon::parse('2026-06-01 10:00:00');
$dateEnd = Carbon::parse('2026-06-30 20:00:00');
// WHEN
$dto = new SlideDto(
id: 42,
title: 'Bannière Été',
text: '<p>Découvrez notre nouvelle collection</p>',
picture: 'storage/modelfiles/slides/26/06/01/ab/summer.jpg',
picture_alt: 'Collection Été',
picture_title: 'Bannière promotionnelle été',
link: 'https://example.com/ete',
link_label: 'Voir les offres',
link_new_tab: true,
connected: false,
date_start: $dateStart,
date_end: $dateEnd,
order: 3,
status: true,
);
// THEN
expect($dto->id)->toBe(42)
->and($dto->title)->toBe('Bannière Été')
->and($dto->text)->toBe('<p>Découvrez notre nouvelle collection</p>')
->and($dto->picture)->toBe('storage/modelfiles/slides/26/06/01/ab/summer.jpg')
->and($dto->picture_alt)->toBe('Collection Été')
->and($dto->picture_title)->toBe('Bannière promotionnelle été')
->and($dto->link)->toBe('https://example.com/ete')
->and($dto->link_label)->toBe('Voir les offres')
->and($dto->link_new_tab)->toBeTrue()
->and($dto->connected)->toBeFalse()
->and($dto->date_start)->toEqual($dateStart)
->and($dto->date_end)->toEqual($dateEnd)
->and($dto->order)->toBe(3)
->and($dto->status)->toBeTrue()
->and($dto->getRouteKey())->toBe(42)
->and($dto->getRouteKeyName())->toBe('id')
->and($dto->resolveRouteBinding(42))->toBeNull()
->and($dto->resolveChildRouteBinding('child', 'val', 'field'))->toBeNull()
->and($dto->toArray())->toBe([
'id' => 42,
'title' => 'Bannière Été',
'text' => '<p>Découvrez notre nouvelle collection</p>',
'picture' => 'storage/modelfiles/slides/26/06/01/ab/summer.jpg',
'picture_alt' => 'Collection Été',
'picture_title' => 'Bannière promotionnelle été',
'link' => 'https://example.com/ete',
'link_label' => 'Voir les offres',
'link_new_tab' => true,
'connected' => false,
'date_start' => $dateStart,
'date_end' => $dateEnd,
'order' => 3,
'status' => true,
]);
});
});

View file

@ -2,6 +2,7 @@
declare(strict_types=1);
use App\Dto\SlideDto;
use App\Models\Customer;
use App\Models\Slide;
use App\Models\User;
@ -164,4 +165,32 @@ describe('SlidePolicy', function () {
// THEN
expect($result)->toBeTrue();
});
test('view, update, delete return true for admin user with SlideDto', function () {
// GIVEN
$user = new User();
$user->role = UserRole::Admin;
$policy = new SlidePolicy();
$dto = new SlideDto(
id: 1,
title: 'Titre',
text: null,
picture: 'pic.jpg',
picture_alt: null,
picture_title: null,
link: null,
link_label: null,
link_new_tab: false,
connected: false,
date_start: null,
date_end: null,
order: 1,
status: true,
);
// WHEN & THEN
expect($policy->view($user, $dto))->toBeTrue()
->and($policy->update($user, $dto))->toBeTrue()
->and($policy->delete($user, $dto))->toBeTrue();
});
});

View file

@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
use Domain\Models\ValueObjects\DatePeriod;
describe('DatePeriod', function (): void {
it('creates a period with null dates', function (): void {
// GIVEN
$period = new DatePeriod();
// WHEN
$isActive = $period->isActiveAt();
// THEN
expect($period->start)->toBeNull()
->and($period->end)->toBeNull()
->and($isActive)->toBeTrue();
});
it('creates a period with start and end dates', function (): void {
// GIVEN
$start = new \DateTimeImmutable('2026-01-01 00:00:00');
$end = new \DateTimeImmutable('2026-12-31 23:59:59');
// WHEN
$period = new DatePeriod($start, $end);
// THEN
expect($period->start)->toEqual($start)
->and($period->end)->toEqual($end);
});
it('throws when start date is after end date', function (): void {
// GIVEN
$start = new \DateTimeImmutable('2026-12-31 23:59:59');
$end = new \DateTimeImmutable('2026-01-01 00:00:00');
// WHEN
expect(fn () => new DatePeriod($start, $end))
// THEN
->toThrow(\InvalidArgumentException::class, 'The start date must be before or equal to the end date.');
});
it('evaluates isActiveAt correctly', function (): void {
// GIVEN
$start = new \DateTimeImmutable('2026-06-01 00:00:00');
$end = new \DateTimeImmutable('2026-06-30 23:59:59');
$period = new DatePeriod($start, $end);
// WHEN
$before = $period->isActiveAt(new \DateTimeImmutable('2026-05-31 23:59:59'));
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
$after = $period->isActiveAt(new \DateTimeImmutable('2026-07-01 00:00:00'));
// THEN
expect($before)->toBeFalse()
->and($during)->toBeTrue()
->and($after)->toBeFalse();
});
it('evaluates isActiveAt with only start date', function (): void {
// GIVEN
$start = new \DateTimeImmutable('2026-06-01 00:00:00');
$period = new DatePeriod(start: $start);
// WHEN
$before = $period->isActiveAt(new \DateTimeImmutable('2026-05-31 23:59:59'));
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
// THEN
expect($before)->toBeFalse()
->and($during)->toBeTrue();
});
it('evaluates isActiveAt with only end date', function (): void {
// GIVEN
$end = new \DateTimeImmutable('2026-06-30 23:59:59');
$period = new DatePeriod(end: $end);
// WHEN
$during = $period->isActiveAt(new \DateTimeImmutable('2026-06-15 12:00:00'));
$after = $period->isActiveAt(new \DateTimeImmutable('2026-07-01 00:00:00'));
// THEN
expect($during)->toBeTrue()
->and($after)->toBeFalse();
});
});