diff --git a/app/Actions/Slides/CreateSlide.php b/app/Actions/Slides/CreateSlide.php new file mode 100644 index 00000000..3c0f1585 --- /dev/null +++ b/app/Actions/Slides/CreateSlide.php @@ -0,0 +1,44 @@ + + */ +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; + } +} diff --git a/app/Actions/Slides/CreateSlideInput.php b/app/Actions/Slides/CreateSlideInput.php new file mode 100644 index 00000000..a0232444 --- /dev/null +++ b/app/Actions/Slides/CreateSlideInput.php @@ -0,0 +1,50 @@ +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); + } + } +} diff --git a/app/Actions/Slides/DeleteSlide.php b/app/Actions/Slides/DeleteSlide.php new file mode 100644 index 00000000..c18847bf --- /dev/null +++ b/app/Actions/Slides/DeleteSlide.php @@ -0,0 +1,26 @@ + + */ +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(); + } +} diff --git a/app/Actions/Slides/DeleteSlideInput.php b/app/Actions/Slides/DeleteSlideInput.php new file mode 100644 index 00000000..fc57ab1d --- /dev/null +++ b/app/Actions/Slides/DeleteSlideInput.php @@ -0,0 +1,16 @@ + + */ +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; + } +} diff --git a/app/Actions/Slides/UpdateSlideInput.php b/app/Actions/Slides/UpdateSlideInput.php new file mode 100644 index 00000000..5cf8845d --- /dev/null +++ b/app/Actions/Slides/UpdateSlideInput.php @@ -0,0 +1,52 @@ +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); + } + } +} diff --git a/app/Dto/SlideDto.php b/app/Dto/SlideDto.php new file mode 100644 index 00000000..f039e27a --- /dev/null +++ b/app/Dto/SlideDto.php @@ -0,0 +1,102 @@ + + */ +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 + */ + #[\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, + ]; + } +} diff --git a/app/Http/Controllers/Bo/SlidesController.php b/app/Http/Controllers/Bo/SlidesController.php index 35a526be..c76606b9 100644 --- a/app/Http/Controllers/Bo/SlidesController.php +++ b/app/Http/Controllers/Bo/SlidesController.php @@ -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; } } diff --git a/app/Models/Slide.php b/app/Models/Slide.php index b790cf1e..d800e380 100644 --- a/app/Models/Slide.php +++ b/app/Models/Slide.php @@ -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 diff --git a/app/Policies/Backend/SlidePolicy.php b/app/Policies/Backend/SlidePolicy.php index b98c2de9..8fc6f3da 100644 --- a/app/Policies/Backend/SlidePolicy.php +++ b/app/Policies/Backend/SlidePolicy.php @@ -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; diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 4c2e4450..addbada5 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -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, ]; diff --git a/app/Queries/Slides/GetPaginatedSlidesQuery.php b/app/Queries/Slides/GetPaginatedSlidesQuery.php new file mode 100644 index 00000000..d9014838 --- /dev/null +++ b/app/Queries/Slides/GetPaginatedSlidesQuery.php @@ -0,0 +1,36 @@ +> + */ +final readonly class GetPaginatedSlidesQuery implements Query +{ + public function __construct( + private DataTableService $dataTable, + ) { + } + + /** + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ + 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)); + } +} diff --git a/domain/Models/ValueObjects/DatePeriod.php b/domain/Models/ValueObjects/DatePeriod.php new file mode 100644 index 00000000..24b1bddb --- /dev/null +++ b/domain/Models/ValueObjects/DatePeriod.php @@ -0,0 +1,41 @@ +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; + } +} diff --git a/routes/breadcrumbs-bo.php b/routes/breadcrumbs-bo.php index 19f7f62c..1067d2f1 100644 --- a/routes/breadcrumbs-bo.php +++ b/routes/breadcrumbs-bo.php @@ -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)); }); diff --git a/tests/Feature/App/Http/Controllers/Bo/SlidesControllerTest.php b/tests/Feature/App/Http/Controllers/Bo/SlidesControllerTest.php index 6b72da12..f8e716be 100644 --- a/tests/Feature/App/Http/Controllers/Bo/SlidesControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Bo/SlidesControllerTest.php @@ -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]); diff --git a/tests/Integration/WithApp/App/Actions/Slides/CreateSlideTest.php b/tests/Integration/WithApp/App/Actions/Slides/CreateSlideTest.php new file mode 100644 index 00000000..d2b0f2c9 --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Slides/CreateSlideTest.php @@ -0,0 +1,62 @@ +Contenu promo

', + 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('

Contenu promo

') + ->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); + }); +}); diff --git a/tests/Integration/WithApp/App/Actions/Slides/DeleteSlideTest.php b/tests/Integration/WithApp/App/Actions/Slides/DeleteSlideTest.php new file mode 100644 index 00000000..f62eee68 --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Slides/DeleteSlideTest.php @@ -0,0 +1,44 @@ +title = 'Slide à supprimer'; + $slide->text = '

Contenu

'; + $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); + }); +}); diff --git a/tests/Integration/WithApp/App/Actions/Slides/UpdateSlideTest.php b/tests/Integration/WithApp/App/Actions/Slides/UpdateSlideTest.php new file mode 100644 index 00000000..254f666c --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Slides/UpdateSlideTest.php @@ -0,0 +1,68 @@ +title = 'Ancien Titre'; + $slide->text = '

Ancien texte

'; + $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: '

Texte mis à jour

', + 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('

Texte mis à jour

') + ->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('

Texte mis à jour

'); + }); + + 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); + }); +}); diff --git a/tests/Integration/WithApp/App/Dto/SlideDtoTest.php b/tests/Integration/WithApp/App/Dto/SlideDtoTest.php new file mode 100644 index 00000000..1b0ca647 --- /dev/null +++ b/tests/Integration/WithApp/App/Dto/SlideDtoTest.php @@ -0,0 +1,50 @@ +id = 42; + $slide->title = 'Bannière Été'; + $slide->text = '

Découvrez notre nouvelle collection

'; + $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('

Découvrez notre nouvelle collection

') + ->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(); + }); +}); diff --git a/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php b/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php new file mode 100644 index 00000000..87a327d4 --- /dev/null +++ b/tests/Integration/WithApp/App/Queries/Slides/GetPaginatedSlidesQueryTest.php @@ -0,0 +1,36 @@ +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); + }); +}); diff --git a/tests/Unit/App/Actions/Slides/CreateSlideInputTest.php b/tests/Unit/App/Actions/Slides/CreateSlideInputTest.php new file mode 100644 index 00000000..78393926 --- /dev/null +++ b/tests/Unit/App/Actions/Slides/CreateSlideInputTest.php @@ -0,0 +1,98 @@ +Contenu du slide

'; + $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); + }); +}); diff --git a/tests/Unit/App/Actions/Slides/DeleteSlideInputTest.php b/tests/Unit/App/Actions/Slides/DeleteSlideInputTest.php new file mode 100644 index 00000000..b4f26324 --- /dev/null +++ b/tests/Unit/App/Actions/Slides/DeleteSlideInputTest.php @@ -0,0 +1,19 @@ +slide)->toBe($slide); + }); +}); diff --git a/tests/Unit/App/Actions/Slides/UpdateSlideInputTest.php b/tests/Unit/App/Actions/Slides/UpdateSlideInputTest.php new file mode 100644 index 00000000..dd5d605d --- /dev/null +++ b/tests/Unit/App/Actions/Slides/UpdateSlideInputTest.php @@ -0,0 +1,65 @@ +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 = '

Nouveau texte

'; + + // 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.'); + }); +}); diff --git a/tests/Unit/App/Dto/SlideDtoTest.php b/tests/Unit/App/Dto/SlideDtoTest.php new file mode 100644 index 00000000..65aea3de --- /dev/null +++ b/tests/Unit/App/Dto/SlideDtoTest.php @@ -0,0 +1,68 @@ +Découvrez notre nouvelle collection

', + 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('

Découvrez notre nouvelle collection

') + ->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' => '

Découvrez notre nouvelle collection

', + '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, + ]); + }); +}); diff --git a/tests/Unit/App/Policies/Backend/SlidePolicyTest.php b/tests/Unit/App/Policies/Backend/SlidePolicyTest.php index 369336dc..0c6802c1 100644 --- a/tests/Unit/App/Policies/Backend/SlidePolicyTest.php +++ b/tests/Unit/App/Policies/Backend/SlidePolicyTest.php @@ -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(); + }); }); diff --git a/tests/Unit/Domain/Models/ValueObjects/DatePeriodTest.php b/tests/Unit/Domain/Models/ValueObjects/DatePeriodTest.php new file mode 100644 index 00000000..29518f99 --- /dev/null +++ b/tests/Unit/Domain/Models/ValueObjects/DatePeriodTest.php @@ -0,0 +1,89 @@ +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(); + }); +});