ldc-cocotte/tests/Integration/WithApp/App/Cqs/Queries/Slides/GetPaginatedSlidesQueryTest.php

118 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Queries\Slides;
use App\Cqs\Queries\Slides\GetPaginatedSlidesQuery;
use App\Cqs\Queries\Slides\GetPaginatedSlidesQueryInput;
use App\Dto\SlideDto;
use App\Models\Slide;
use App\Services\DataTableService;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Schema;
use Shared\Pagination\PageNumber;
use Shared\Pagination\PageSize;
use Shared\Pagination\Pagination;
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
Date::setTestNow($now);
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
DataTableService::clearColumnsCache();
Date::setTestNow();
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
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();
$query = new GetPaginatedSlidesQuery(
new DataTableService(
Request::create('/'),
new Store('test', new NullSessionHandler()),
static fn (string $table): array => Schema::getColumnListing($table),
),
);
$input = new GetPaginatedSlidesQueryInput(
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
);
// WHEN
$paginator = $query($input);
// THEN
expect($paginator->total())->toBe(2)
->and($paginator->items())->toHaveCount(2);
});
it('filters slides by publication date when publishedOnly is true', function (): void {
// GIVEN
$now = Date::now();
$activeSlide = new Slide();
$activeSlide->title = 'Active Slide';
$activeSlide->date_start = $now->copy()->subDay();
$activeSlide->date_end = $now->copy()->addDay();
$activeSlide->save();
$permanentSlide = new Slide();
$permanentSlide->title = 'Permanent Slide';
$permanentSlide->save();
$expiredSlide = new Slide();
$expiredSlide->title = 'Expired Slide';
$expiredSlide->date_start = $now->copy()->subDays(5);
$expiredSlide->date_end = $now->copy()->subDays(2);
$expiredSlide->save();
$futureSlide = new Slide();
$futureSlide->title = 'Future Slide';
$futureSlide->date_start = $now->copy()->addDays(2);
$futureSlide->date_end = $now->copy()->addDays(5);
$futureSlide->save();
$query = new GetPaginatedSlidesQuery(
new DataTableService(
Request::create('/'),
new Store('test', new NullSessionHandler()),
static fn (string $table): array => Schema::getColumnListing($table),
),
);
$input = new GetPaginatedSlidesQueryInput(
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
publishedOnly: true,
);
// WHEN
$paginator = $query($input);
// THEN
$titles = array_map(fn (SlideDto $dto) => $dto->title, $paginator->items());
expect($titles)->toContain('Active Slide', 'Permanent Slide')
->and($titles)->not->toContain('Expired Slide', 'Future Slide');
});
});