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

208 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Queries\Orders;
use App\Cqs\Queries\Orders\GetFoCustomerPaginatedOrdersQuery;
use App\Cqs\Queries\Orders\GetFoCustomerPaginatedOrdersQueryInput;
use App\Models\Customer;
use App\Models\Employee;
use App\Models\Order;
use App\Models\Shop;
use Carbon\CarbonImmutable;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Email;
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\Support\Carbon;
use Money\Money;
use Shared\Helpers\UuidHelper;
use Shared\Pagination\PageNumber;
use Shared\Pagination\PageSize;
use Shared\Pagination\Pagination;
use Shared\ValueObjects\CrmHashcode;
afterEach(function (): void {
Carbon::setTestNow();
});
describe('GetFoCustomerPaginatedOrdersQuery', function (): void {
$createCustomer = function (): Customer {
$customer = new Customer();
$customer->email = new Email('customer-' . uniqid() . '@example.com');
$customer->hashcode = new CrmHashcode(UuidHelper::v4());
$customer->save();
return $customer;
};
$createEmployee = function (Customer $customer, string $matricule, string $code): Employee {
$employee = new Employee();
$employee->customer_id = $customer->id;
$employee->matricule = new Matricule($matricule);
$employee->nom = 'Dupont';
$employee->prenom = 'Jean';
$employee->etablissement = new ShopperEstablishmentCode('FAC01');
$employee->site = new ShopperSiteCode('SITE');
$employee->service = new ShopperServiceCode('101');
$employee->code = new Code($code);
$employee->save();
return $employee;
};
$createShop = function (): Shop {
$shop = new Shop();
$shop->name = 'Boutique ' . uniqid();
$shop->address = '10 rue Principale';
$shop->postcode = '75001';
$shop->city = 'Paris';
$shop->default = true;
$shop->save();
return $shop;
};
$createOrder = function (Shop $shop, string $matricule): Order {
$order = new Order();
$order->reference = new OrderReference(UuidHelper::v7());
$order->total = Money::EUR(2500);
$order->status = OrderStatus::WAITING;
$order->pickup_day = Carbon::now()->addDays(2);
$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($matricule),
firstname: 'Jean',
lastname: 'Dupont',
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$order->save();
return $order;
};
it('returns an empty paginator when customer has no linked employee', function () use ($createCustomer): void {
// GIVEN
$customer = $createCustomer();
$query = new GetFoCustomerPaginatedOrdersQuery();
$pagination = Pagination::ofPage(new PageNumber(2), new PageSize(5));
// WHEN
$result = $query(new GetFoCustomerPaginatedOrdersQueryInput(
customer: $customer,
pagination: $pagination,
));
// THEN
expect($result->total())->toBe(0)
->and($result->items())->toBe([])
->and($result->perPage())->toBe(5)
->and($result->currentPage())->toBe(2);
});
it('returns only the linked employee orders sorted by id DESC, mapped to OrderDto and paginated', function () use (
$createCustomer,
$createEmployee,
$createShop,
$createOrder,
): void {
// GIVEN
$customer1 = $createCustomer();
$customer2 = $createCustomer();
$createEmployee($customer1, '1234567', 'ABCDEFG');
$createEmployee($customer2, '7654321', 'HJKMNPR');
$shop = $createShop();
$baseTime = CarbonImmutable::parse('2026-03-10 10:00:00');
Carbon::setTestNow($baseTime);
$order1 = $createOrder($shop, '1234567');
Carbon::setTestNow($baseTime->addSecond());
$order2 = $createOrder($shop, '1234567');
Carbon::setTestNow($baseTime->addSeconds(2));
$order3 = $createOrder($shop, '1234567');
Carbon::setTestNow($baseTime->addSeconds(3));
$createOrder($shop, '7654321');
$query = new GetFoCustomerPaginatedOrdersQuery();
// WHEN: page 1 with pageSize 2
$page1 = $query(new GetFoCustomerPaginatedOrdersQueryInput(
customer: $customer1,
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(2)),
));
// THEN
expect($page1->total())->toBe(3)
->and($page1->perPage())->toBe(2)
->and($page1->currentPage())->toBe(1)
->and($page1->items())->toHaveCount(2)
->and($page1->items()[0]->id)->toBe($order3->id)
->and($page1->items()[1]->id)->toBe($order2->id);
// WHEN: page 2 with pageSize 2
$page2 = $query(new GetFoCustomerPaginatedOrdersQueryInput(
customer: $customer1,
pagination: Pagination::ofPage(new PageNumber(2), new PageSize(2)),
));
// THEN
expect($page2->total())->toBe(3)
->and($page2->currentPage())->toBe(2)
->and($page2->items())->toHaveCount(1)
->and($page2->items()[0]->id)->toBe($order1->id);
});
it('excludes PENDING and FAILED technical orders from customer order history', function () use (
$createCustomer,
$createEmployee,
$createShop,
$createOrder,
): void {
// GIVEN
$customer = $createCustomer();
$createEmployee($customer, '1234568', 'HJKMNPS');
$shop = $createShop();
$validated = $createOrder($shop, '1234568');
$validated->status = OrderStatus::VALIDATED;
$validated->save();
$pending = $createOrder($shop, '1234568');
$pending->status = OrderStatus::PENDING;
$pending->save();
$failed = $createOrder($shop, '1234568');
$failed->status = OrderStatus::FAILED;
$failed->save();
$query = new GetFoCustomerPaginatedOrdersQuery();
// WHEN
$result = $query(new GetFoCustomerPaginatedOrdersQueryInput(
customer: $customer,
pagination: Pagination::ofPage(new PageNumber(1), new PageSize(10)),
));
// THEN
expect($result->total())->toBe(1)
->and($result->items()[0]->id)->toBe($validated->id);
});
});