ldc-cocotte/tests/Integration/WithApp/App/Cqs/Queries/Discounts/GetPaginatedDiscountsQueryTest.php

82 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Queries\Discounts;
use App\Contracts\QueryInput;
use App\Cqs\Queries\Discounts\GetPaginatedDiscountsQuery;
use App\Cqs\Queries\Discounts\GetPaginatedDiscountsQueryInput;
use App\Models\Discount;
use App\Services\DataTableService;
use Domain\Enums\DiscountType;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Http\Request;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Schema;
use Shared\Pagination\PageNumber;
use Shared\Pagination\PageSize;
use Shared\Pagination\Pagination;
afterEach(function (): void {
DataTableService::clearColumnsCache();
});
describe('GetPaginatedDiscountsQuery', function (): void {
it('returns a paginator of DiscountDto instances', function (): void {
// GIVEN
$discount1 = new Discount();
$discount1->type = DiscountType::PERCENT;
$discount1->value = DiscountValue::fromPercentage(10);
$discount1->is_old_price_displayed = true;
$discount1->save();
$discount2 = new Discount();
$discount2->type = DiscountType::PERCENT;
$discount2->value = DiscountValue::fromPercentage(20);
$discount2->is_old_price_displayed = false;
$discount2->save();
$query = new GetPaginatedDiscountsQuery(
new DataTableService(
Request::create('/'),
new Store('test', new NullSessionHandler()),
static fn (string $table): array => Schema::getColumnListing($table),
),
);
$input = new GetPaginatedDiscountsQueryInput(
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('throws when an invalid input is passed', function (): void {
// GIVEN
$query = new GetPaginatedDiscountsQuery(
new DataTableService(
Request::create('/'),
new Store('test', new NullSessionHandler()),
static fn (string $table): array => Schema::getColumnListing($table),
),
);
$invalidInput = new class () implements QueryInput
{
};
// WHEN
expect(
/** @phpstan-ignore argument.type */
fn () => $query($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});