2026-09-30 12:43:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Queries\Discounts;
|
2026-09-30 12:43:55 +02:00
|
|
|
|
|
|
|
|
use App\Contracts\QueryInput;
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Queries\Discounts\GetPaginatedDiscountsQuery;
|
|
|
|
|
use App\Cqs\Queries\Discounts\GetPaginatedDiscountsQueryInput;
|
2026-09-30 12:43:55 +02:00
|
|
|
use App\Models\Discount;
|
|
|
|
|
use App\Services\DataTableService;
|
|
|
|
|
use Domain\Enums\DiscountType;
|
|
|
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
2026-11-14 16:06:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Session\NullSessionHandler;
|
|
|
|
|
use Illuminate\Session\Store;
|
2026-11-16 01:01:33 +01:00
|
|
|
use Illuminate\Support\Facades\Schema;
|
2026-10-02 12:14:48 +02:00
|
|
|
use Shared\Pagination\PageNumber;
|
|
|
|
|
use Shared\Pagination\PageSize;
|
|
|
|
|
use Shared\Pagination\Pagination;
|
2026-09-30 12:43:55 +02:00
|
|
|
|
2026-11-14 16:06:49 +01:00
|
|
|
afterEach(function (): void {
|
|
|
|
|
DataTableService::clearColumnsCache();
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-30 12:43:55 +02:00
|
|
|
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();
|
|
|
|
|
|
2026-11-16 01:01:33 +01:00
|
|
|
$query = new GetPaginatedDiscountsQuery(
|
|
|
|
|
new DataTableService(
|
|
|
|
|
Request::create('/'),
|
|
|
|
|
new Store('test', new NullSessionHandler()),
|
|
|
|
|
static fn (string $table): array => Schema::getColumnListing($table),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-09-30 12:43:55 +02:00
|
|
|
$input = new GetPaginatedDiscountsQueryInput(
|
2026-10-02 12:14:48 +02:00
|
|
|
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
|
2026-09-30 12:43:55 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$paginator = $query($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($paginator->total())->toBe(2)
|
2026-11-16 15:43:10 +01:00
|
|
|
->and($paginator->items())->toHaveCount(2);
|
2026-09-30 12:43:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-16 01:01:33 +01:00
|
|
|
$query = new GetPaginatedDiscountsQuery(
|
|
|
|
|
new DataTableService(
|
|
|
|
|
Request::create('/'),
|
|
|
|
|
new Store('test', new NullSessionHandler()),
|
|
|
|
|
static fn (string $table): array => Schema::getColumnListing($table),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-09-30 12:43:55 +02:00
|
|
|
$invalidInput = new class () implements QueryInput
|
|
|
|
|
{
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
expect(
|
|
|
|
|
/** @phpstan-ignore argument.type */
|
|
|
|
|
fn () => $query($invalidInput)
|
|
|
|
|
)
|
|
|
|
|
// THEN
|
|
|
|
|
->toThrow(\InvalidArgumentException::class);
|
|
|
|
|
});
|
|
|
|
|
});
|