ldc-cocotte/tests/Integration/WithApp/App/Cqs/Queries/Orders/GetBoPaginatedOrdersQueryTest.php

371 lines
13 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Queries\Orders;
use App\Cqs\Queries\Orders\GetBoPaginatedOrdersQuery;
use App\Cqs\Queries\Orders\GetBoPaginatedOrdersQueryInput;
use App\Dto\OrderDto;
use App\Models\Order;
use App\Models\Shop;
use App\Services\DataTableService;
use Carbon\CarbonImmutable;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Http\Request;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\Store;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Schema;
use Money\Money;
use Shared\Helpers\UuidHelper;
use Shared\Pagination\PageNumber;
use Shared\Pagination\PageSize;
use Shared\Pagination\Pagination;
afterEach(function (): void {
DataTableService::clearColumnsCache();
Carbon::setTestNow();
});
describe('GetBoPaginatedOrdersQuery', function (): void {
$createShop = function (string $name): Shop {
$shop = new Shop();
$shop->name = $name;
$shop->address = '10 rue Principale';
$shop->postcode = '75001';
$shop->city = 'Paris';
$shop->default = true;
$shop->save();
return $shop;
};
$createOrder = function (
Shop $shop,
OrderStatus $status,
CarbonImmutable $pickupDay,
?\DateTimeImmutable $createdAt = null,
): Order {
$order = new Order();
if ($createdAt !== null) {
$order->id = (string) UuidHelper::v7($createdAt);
}
$order->reference = new OrderReference(UuidHelper::v7());
$order->total = Money::EUR(2500);
$order->status = $status;
$order->pickup_day = Carbon::instance($pickupDay);
$order->shop_id = $shop->id;
$order->delivery_address = new DeliveryAddress(
name: $shop->name,
address: $shop->address,
postcode: $shop->postcode,
city: $shop->city,
);
$order->employee = new OrderEmployee(
matricule: new Matricule('1234567'),
firstname: 'Jean',
lastname: 'Dupont',
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$order->save();
return $order;
};
$makeQuery = function (): GetBoPaginatedOrdersQuery {
return new GetBoPaginatedOrdersQuery(
new DataTableService(
Request::create('/'),
new Store('test', new NullSessionHandler()),
static fn (string $table): array => Schema::getColumnListing($table),
),
);
};
it('returns GetBoPaginatedOrdersQueryOutput sorted by id DESC and respects pagination', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shop = $createShop('Boutique Centre');
$pickupDay = CarbonImmutable::parse('2026-04-10');
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 09:00:00'));
$order1 = $createOrder($shop, OrderStatus::WAITING, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 09:00:01'));
$order2 = $createOrder($shop, OrderStatus::WAITING, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 09:00:02'));
$order3 = $createOrder($shop, OrderStatus::WAITING, $pickupDay);
$query = $makeQuery();
// WHEN: page 1 with pageSize 2
$outputPage1 = $query(new GetBoPaginatedOrdersQueryInput(
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(2)),
));
// THEN
expect($outputPage1->searchFields)->not->toBeEmpty()
->and($outputPage1->orderStatusList)->toBe(OrderStatus::cases())
->and($outputPage1->orders->total())->toBe(3)
->and($outputPage1->orders->items())->toHaveCount(2)
->and($outputPage1->orders->items()[0])->toBeInstanceOf(OrderDto::class)
->and($outputPage1->orders->items()[0]->id)->toBe($order3->id)
->and($outputPage1->orders->items()[1]->id)->toBe($order2->id);
// WHEN: page 2 with pageSize 2
$outputPage2 = $query(new GetBoPaginatedOrdersQueryInput(
pagination: Pagination::ofPage(new PageNumber(2), new PageSize(2)),
));
// THEN
expect($outputPage2->orders->items())->toHaveCount(1)
->and($outputPage2->orders->items()[0]->id)->toBe($order1->id);
});
it('filters orders by search on reference and shop name', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shopAlpha = $createShop('Boutique Alpha');
$shopBeta = $createShop('Boutique Beta');
$pickupDay = CarbonImmutable::parse('2026-04-10');
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:00'));
$orderAlpha = $createOrder($shopAlpha, OrderStatus::WAITING, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:01'));
$orderBeta = $createOrder($shopBeta, OrderStatus::WAITING, $pickupDay);
$query = $makeQuery();
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
// WHEN: search by reference
$byReference = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
search: (string) $orderAlpha->reference,
));
// THEN
expect($byReference->orders->total())->toBe(1)
->and($byReference->orders->items()[0]->id)->toBe($orderAlpha->id);
// WHEN: search by shop name
$byShopName = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
search: 'Beta',
));
// THEN
expect($byShopName->orders->total())->toBe(1)
->and($byShopName->orders->items()[0]->id)->toBe($orderBeta->id);
});
it('filters orders by orderDateStart and orderDateEnd (single day and range)', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shop = $createShop('Boutique Dates');
$pickupDay = CarbonImmutable::parse('2026-04-20');
$orderDay1 = $createOrder(
$shop,
OrderStatus::WAITING,
$pickupDay,
new \DateTimeImmutable('2026-04-01 12:00:00'),
);
$orderDay5 = $createOrder(
$shop,
OrderStatus::WAITING,
$pickupDay,
new \DateTimeImmutable('2026-04-05 12:00:00'),
);
$orderDay10 = $createOrder(
$shop,
OrderStatus::WAITING,
$pickupDay,
new \DateTimeImmutable('2026-04-10 12:00:00'),
);
$query = $makeQuery();
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
// WHEN: single day (only orderDateStart)
$singleDay = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderDateStart: CarbonImmutable::parse('2026-04-05'),
));
// THEN
expect($singleDay->orders->total())->toBe(1)
->and($singleDay->orders->items()[0]->id)->toBe($orderDay5->id);
// WHEN: date range
$range = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderDateStart: CarbonImmutable::parse('2026-04-01'),
orderDateEnd: CarbonImmutable::parse('2026-04-06'),
));
// THEN
expect($range->orders->total())->toBe(2)
->and($range->orders->items()[0]->id)->toBe($orderDay5->id)
->and($range->orders->items()[1]->id)->toBe($orderDay1->id);
// WHEN: single day on day 10
$day10 = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderDateStart: CarbonImmutable::parse('2026-04-10'),
));
// THEN
expect($day10->orders->total())->toBe(1)
->and($day10->orders->items()[0]->id)->toBe($orderDay10->id);
});
it('filters orders by orderPickupStart and orderPickupEnd (single day and range)', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shop = $createShop('Boutique Pickup');
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:00'));
$orderPickup1 = $createOrder($shop, OrderStatus::WAITING, CarbonImmutable::parse('2026-05-01'));
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:01'));
$orderPickup2 = $createOrder($shop, OrderStatus::WAITING, CarbonImmutable::parse('2026-05-05'));
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:02'));
$orderPickup3 = $createOrder($shop, OrderStatus::WAITING, CarbonImmutable::parse('2026-05-10'));
$query = $makeQuery();
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
// WHEN: single pickup day
$singlePickup = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderPickupStart: CarbonImmutable::parse('2026-05-05'),
));
// THEN
expect($singlePickup->orders->total())->toBe(1)
->and($singlePickup->orders->items()[0]->id)->toBe($orderPickup2->id);
// WHEN: pickup range
$pickupRange = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderPickupStart: CarbonImmutable::parse('2026-05-01'),
orderPickupEnd: CarbonImmutable::parse('2026-05-06'),
));
// THEN
expect($pickupRange->orders->total())->toBe(2)
->and($pickupRange->orders->items()[0]->id)->toBe($orderPickup2->id)
->and($pickupRange->orders->items()[1]->id)->toBe($orderPickup1->id);
// WHEN: single pickup day 3
$pickupDay3 = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
orderPickupStart: CarbonImmutable::parse('2026-05-10'),
));
// THEN
expect($pickupDay3->orders->total())->toBe(1)
->and($pickupDay3->orders->items()[0]->id)->toBe($orderPickup3->id);
});
it('filters orders by status', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shop = $createShop('Boutique Status');
$pickupDay = CarbonImmutable::parse('2026-05-01');
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:00'));
$waitingOrder = $createOrder($shop, OrderStatus::WAITING, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:01'));
$createOrder($shop, OrderStatus::CANCELED, $pickupDay);
$query = $makeQuery();
// WHEN
$output = $query(new GetBoPaginatedOrdersQueryInput(
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
status: OrderStatus::WAITING,
));
// THEN
expect($output->orders->total())->toBe(1)
->and($output->orders->items()[0]->id)->toBe($waitingOrder->id)
->and($output->orders->items()[0]->status)->toBe(OrderStatus::WAITING);
});
it('excludes PENDING and FAILED orders by default and shows them when explicitly filtered', function () use (
$createShop,
$createOrder,
$makeQuery,
): void {
// GIVEN
$shop = $createShop('Boutique Technique');
$pickupDay = CarbonImmutable::parse('2026-05-01');
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:00'));
$validated = $createOrder($shop, OrderStatus::VALIDATED, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:01'));
$pending = $createOrder($shop, OrderStatus::PENDING, $pickupDay);
Carbon::setTestNow(CarbonImmutable::parse('2026-04-01 10:00:02'));
$failed = $createOrder($shop, OrderStatus::FAILED, $pickupDay);
$query = $makeQuery();
$pagination = Pagination::ofPage(new PageNumber(1), new PageSize(10));
// WHEN: no status filter (default BO list)
$defaultOutput = $query(new GetBoPaginatedOrdersQueryInput(pagination: $pagination));
// THEN
expect($defaultOutput->orders->total())->toBe(1)
->and($defaultOutput->orders->items()[0]->id)->toBe($validated->id);
// WHEN: explicitly filtering on PENDING or FAILED
$pendingOutput = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
status: OrderStatus::PENDING,
));
$failedOutput = $query(new GetBoPaginatedOrdersQueryInput(
pagination: $pagination,
status: OrderStatus::FAILED,
));
// THEN
expect($pendingOutput->orders->total())->toBe(1)
->and($pendingOutput->orders->items()[0]->id)->toBe($pending->id)
->and($failedOutput->orders->total())->toBe(1)
->and($failedOutput->orders->items()[0]->id)->toBe($failed->id);
});
});