2026-11-16 03:32:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:18:42 +01:00
|
|
|
namespace Tests\Unit\App\Cqs\Actions\Orders;
|
|
|
|
|
|
2026-11-16 03:32:57 +01:00
|
|
|
use App\Contracts\HandlesFailure;
|
|
|
|
|
use App\Contracts\HandlesSuccess;
|
|
|
|
|
use App\Cqs\Actions\Orders\ConfirmPlacedOrder;
|
|
|
|
|
use App\Cqs\Actions\Orders\ConfirmPlacedOrderInput;
|
|
|
|
|
use App\Cqs\Actions\Orders\ConfirmPlacedOrderOutput;
|
|
|
|
|
use App\Cqs\Actions\Orders\ExportOrderToRegate;
|
|
|
|
|
use App\Cqs\Actions\Orders\ExportOrderToRegateInput;
|
|
|
|
|
use App\Cqs\Actions\Orders\ExportOrderToRegateOutput;
|
2026-11-16 20:05:27 +01:00
|
|
|
use App\Cqs\Actions\Orders\FailPendingOrder;
|
|
|
|
|
use App\Cqs\Actions\Orders\FailPendingOrderInput;
|
|
|
|
|
use App\Cqs\Actions\Orders\FailPendingOrderOutput;
|
2026-11-16 03:32:57 +01:00
|
|
|
use App\Cqs\Actions\Orders\PlacePendingOrder;
|
|
|
|
|
use App\Cqs\Actions\Orders\PlacePendingOrderInput;
|
|
|
|
|
use App\Cqs\Actions\Orders\PlacePendingOrderOutput;
|
|
|
|
|
use App\Enums\PlacePendingOrderStatus;
|
|
|
|
|
use Domain\Builders\OrderBuilderCompleted;
|
|
|
|
|
use Domain\Enums\OrderStatus;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\BasketPriceChangedException;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\ExpiredPickupTimeSlotException;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\InactiveShopperException;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
|
|
|
|
|
use Domain\Models\Entities\Basket;
|
|
|
|
|
use Domain\Models\Entities\Order;
|
|
|
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\OrderId;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\ShopId;
|
|
|
|
|
use Domain\Models\ValueObjects\Entities\ShopperId;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
|
|
|
use Domain\Models\ValueObjects\PickupTimeSlot;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
|
|
|
use Domain\Ports\Repositories\BasketRepository;
|
|
|
|
|
use Domain\Services\OrderTunnelService;
|
|
|
|
|
use Money\Money;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-16 03:32:57 +01:00
|
|
|
|
|
|
|
|
describe('PlacePendingOrder', function (): void {
|
|
|
|
|
it('places a pending order and returns id and reference via OrderTunnelService', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$input = new PlacePendingOrderInput();
|
2026-11-17 12:15:39 +01:00
|
|
|
$orderId = new OrderId(UuidHelper::v7());
|
|
|
|
|
$orderReference = new OrderReference(UuidHelper::v7());
|
2026-11-16 03:32:57 +01:00
|
|
|
$builder = \Mockery::mock(OrderBuilderCompleted::class);
|
|
|
|
|
$order = new Order(
|
|
|
|
|
id: $orderId,
|
2026-11-17 12:15:39 +01:00
|
|
|
shopId: new ShopId(UuidHelper::v7()),
|
2026-11-16 03:32:57 +01:00
|
|
|
reference: $orderReference,
|
|
|
|
|
employee: new OrderEmployee(
|
|
|
|
|
matricule: new Matricule('1234567'),
|
|
|
|
|
firstname: 'Jean',
|
|
|
|
|
lastname: 'Dupont',
|
|
|
|
|
facility: new ShopperEstablishmentCode('ETAB01'),
|
|
|
|
|
site: new ShopperSiteCode('SITEA'),
|
|
|
|
|
service: new ShopperServiceCode('101'),
|
|
|
|
|
),
|
|
|
|
|
deliveryAddress: new DeliveryAddress(
|
|
|
|
|
name: 'Shop',
|
|
|
|
|
address: '1 rue Test',
|
|
|
|
|
postcode: '75001',
|
|
|
|
|
city: 'Paris',
|
|
|
|
|
),
|
|
|
|
|
pickupTimeSlot: new PickupTimeSlot(new \DateTimeImmutable('+2 days'), new \DateInterval('PT1H')),
|
|
|
|
|
status: OrderStatus::WAITING,
|
|
|
|
|
total: Money::EUR(1000),
|
|
|
|
|
lines: [],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
$orderTunnelService->shouldReceive('getBuilderCompletedOrFail')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn($builder);
|
|
|
|
|
$orderTunnelService->shouldReceive('placePendingOrder')
|
|
|
|
|
->once()
|
|
|
|
|
->with($builder)
|
|
|
|
|
->andReturn($order);
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&BasketRepository $basketRepository */
|
|
|
|
|
$basketRepository = \Mockery::mock(BasketRepository::class);
|
|
|
|
|
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($result->status)->toBe(PlacePendingOrderStatus::PENDING_CREATED)
|
2026-11-16 03:32:57 +01:00
|
|
|
->and($result->orderId)->toBe($orderId)
|
|
|
|
|
->and($result->orderReference?->value)->toBe($orderReference->value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('maps domain exceptions cleanly into PlacePendingOrderOutput', function (
|
|
|
|
|
\Throwable $exception,
|
|
|
|
|
PlacePendingOrderStatus $expectedStatus,
|
|
|
|
|
string $expectedMessage,
|
|
|
|
|
): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$input = new PlacePendingOrderInput();
|
|
|
|
|
$builder = \Mockery::mock(OrderBuilderCompleted::class);
|
|
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
$orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder);
|
|
|
|
|
$orderTunnelService->shouldReceive('placePendingOrder')
|
|
|
|
|
->once()
|
|
|
|
|
->with($builder)
|
|
|
|
|
->andThrow($exception);
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&BasketRepository $basketRepository */
|
|
|
|
|
$basketRepository = \Mockery::mock(BasketRepository::class);
|
|
|
|
|
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->not->toBeInstanceOf(HandlesFailure::class)
|
|
|
|
|
->and($action)->not->toBeInstanceOf(HandlesSuccess::class)
|
|
|
|
|
->and($result)->toBeInstanceOf(PlacePendingOrderOutput::class)
|
|
|
|
|
->and($result->status)->toBe($expectedStatus)
|
|
|
|
|
->and($result->orderId)->toBeNull()
|
|
|
|
|
->and($result->orderReference)->toBeNull()
|
|
|
|
|
->and($result->errorMessage)->toBe($expectedMessage);
|
|
|
|
|
})->with([
|
|
|
|
|
'expired slot' => [
|
|
|
|
|
new ExpiredPickupTimeSlotException('Invalid or unavailable time slot'),
|
|
|
|
|
PlacePendingOrderStatus::EXPIRED_TIME_SLOT,
|
|
|
|
|
'Invalid or unavailable time slot',
|
|
|
|
|
],
|
|
|
|
|
'inactive shopper' => [
|
|
|
|
|
new InactiveShopperException('Employee account is no longer active.'),
|
|
|
|
|
PlacePendingOrderStatus::INACTIVE_SHOPPER,
|
|
|
|
|
'Employee account is no longer active.',
|
|
|
|
|
],
|
|
|
|
|
'unorderable product' => [
|
|
|
|
|
new UnorderableProductException('One or more products in your basket are no longer available for order.'),
|
|
|
|
|
PlacePendingOrderStatus::UNORDERABLE_PRODUCT,
|
|
|
|
|
'One or more products in your basket are no longer available for order.',
|
|
|
|
|
],
|
|
|
|
|
'order limit' => [
|
|
|
|
|
new OrderLimitExceededException(),
|
|
|
|
|
PlacePendingOrderStatus::ORDER_LIMIT_EXCEEDED,
|
|
|
|
|
'The total amount of this order has been exceeded. Please modify it to validate.',
|
|
|
|
|
],
|
|
|
|
|
'weekly limit' => [
|
|
|
|
|
new WeeklyLimitExceededException(),
|
|
|
|
|
PlacePendingOrderStatus::WEEKLY_LIMIT_EXCEEDED,
|
|
|
|
|
'The total amount for this delivery date has been exceeded. Please modify it to validate.',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-16 16:29:17 +01:00
|
|
|
it('persists refreshed basket and returns basketPriceChanged when BasketPriceChangedException is thrown', function (): void {
|
2026-11-16 03:32:57 +01:00
|
|
|
// GIVEN
|
|
|
|
|
$input = new PlacePendingOrderInput();
|
|
|
|
|
$builder = \Mockery::mock(OrderBuilderCompleted::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$refreshedBasket = new Basket(new BasketId(UuidHelper::v7()), []);
|
2026-11-16 03:32:57 +01:00
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
$orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder);
|
|
|
|
|
$orderTunnelService->shouldReceive('placePendingOrder')
|
|
|
|
|
->once()
|
|
|
|
|
->with($builder)
|
|
|
|
|
->andThrow(new BasketPriceChangedException($refreshedBasket));
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&BasketRepository $basketRepository */
|
|
|
|
|
$basketRepository = \Mockery::mock(BasketRepository::class);
|
|
|
|
|
$basketRepository->shouldReceive('persist')
|
|
|
|
|
->once()
|
|
|
|
|
->with($refreshedBasket);
|
|
|
|
|
|
|
|
|
|
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($result->status)->toBe(PlacePendingOrderStatus::BASKET_PRICE_CHANGED)
|
2026-11-16 03:32:57 +01:00
|
|
|
->and($result->orderReference)->toBeNull()
|
|
|
|
|
->and($result->errorMessage)->toBe('The price of one or more products in your basket has changed.');
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:01:17 +01:00
|
|
|
it('builds shopper lock key and returns LOCK_TIMEOUT output on lock timeout', function (): void {
|
2026-11-16 03:32:57 +01:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$shopperId = new ShopperId(UuidHelper::v7());
|
2026-11-16 03:32:57 +01:00
|
|
|
$input = new PlacePendingOrderInput(shopperId: $shopperId);
|
|
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
/** @var \Mockery\MockInterface&BasketRepository $basketRepository */
|
|
|
|
|
$basketRepository = \Mockery::mock(BasketRepository::class);
|
|
|
|
|
|
2026-11-16 15:01:17 +01:00
|
|
|
$action = new PlacePendingOrder($orderTunnelService, $basketRepository);
|
2026-11-16 03:32:57 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$key = $action->lockKey($input);
|
|
|
|
|
$timeout = $action->onLockTimeout($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:01:17 +01:00
|
|
|
expect($key)->toBe("order:finalize:shopper:{$shopperId->value}")
|
2026-11-16 03:32:57 +01:00
|
|
|
->and($timeout->status)->toBe(PlacePendingOrderStatus::LOCK_TIMEOUT);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 20:05:27 +01:00
|
|
|
describe('ExportOrderToRegate, ConfirmPlacedOrder and FailPendingOrder', function (): void {
|
2026-11-16 03:32:57 +01:00
|
|
|
it('exports order via OrderTunnelService and handles lock timeout', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$orderId = new OrderId(UuidHelper::v7());
|
2026-11-16 03:32:57 +01:00
|
|
|
$input = new ExportOrderToRegateInput($orderId);
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
$orderTunnelService->shouldReceive('exportOrderToRegate')->once()->with($orderId);
|
|
|
|
|
|
|
|
|
|
$action = new ExportOrderToRegate($orderTunnelService);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe(ExportOrderToRegateOutput::SENT)
|
|
|
|
|
->and($action->lockKey($input))->toBe("order:export:{$orderId->value}")
|
|
|
|
|
->and($action->onLockTimeout($input))->toBe(ExportOrderToRegateOutput::LOCK_TIMEOUT);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 20:05:27 +01:00
|
|
|
it('confirms a placed order and fails a pending order', function (): void {
|
2026-11-16 03:32:57 +01:00
|
|
|
// GIVEN
|
2026-11-17 12:15:39 +01:00
|
|
|
$orderId = new OrderId(UuidHelper::v7());
|
2026-11-16 03:32:57 +01:00
|
|
|
$order = new Order(
|
|
|
|
|
id: $orderId,
|
2026-11-17 12:15:39 +01:00
|
|
|
shopId: new ShopId(UuidHelper::v7()),
|
|
|
|
|
reference: new OrderReference(UuidHelper::v7()),
|
2026-11-16 03:32:57 +01:00
|
|
|
employee: new OrderEmployee(
|
|
|
|
|
matricule: new Matricule('1234567'),
|
|
|
|
|
firstname: 'Jean',
|
|
|
|
|
lastname: 'Dupont',
|
|
|
|
|
facility: new ShopperEstablishmentCode('ETAB01'),
|
|
|
|
|
site: new ShopperSiteCode('SITEA'),
|
|
|
|
|
service: new ShopperServiceCode('101'),
|
|
|
|
|
),
|
|
|
|
|
deliveryAddress: new DeliveryAddress(
|
|
|
|
|
name: 'Shop',
|
|
|
|
|
address: '1 rue Test',
|
|
|
|
|
postcode: '75001',
|
|
|
|
|
city: 'Paris',
|
|
|
|
|
),
|
|
|
|
|
pickupTimeSlot: new PickupTimeSlot(new \DateTimeImmutable('+2 days'), new \DateInterval('PT1H')),
|
|
|
|
|
status: OrderStatus::VALIDATED,
|
|
|
|
|
total: Money::EUR(1000),
|
|
|
|
|
lines: [],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/** @var \Mockery\MockInterface&OrderTunnelService $orderTunnelService */
|
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
|
|
|
$orderTunnelService->shouldReceive('confirmPlacedOrder')
|
|
|
|
|
->once()
|
|
|
|
|
->with($orderId)
|
|
|
|
|
->andReturn($order);
|
2026-11-16 20:05:27 +01:00
|
|
|
$orderTunnelService->shouldReceive('failPendingOrder')
|
2026-11-16 03:32:57 +01:00
|
|
|
->once()
|
|
|
|
|
->with($orderId);
|
|
|
|
|
|
|
|
|
|
$confirmAction = new ConfirmPlacedOrder($orderTunnelService);
|
2026-11-16 20:05:27 +01:00
|
|
|
$failAction = new FailPendingOrder($orderTunnelService);
|
2026-11-16 03:32:57 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$confirmResult = $confirmAction(new ConfirmPlacedOrderInput($orderId));
|
2026-11-16 20:05:27 +01:00
|
|
|
$failResult = $failAction(new FailPendingOrderInput($orderId));
|
2026-11-16 03:32:57 +01:00
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($confirmResult)->toBe(ConfirmPlacedOrderOutput::CONFIRMED)
|
2026-11-16 20:05:27 +01:00
|
|
|
->and($failResult)->toBe(FailPendingOrderOutput::FAILED);
|
2026-11-16 03:32:57 +01:00
|
|
|
});
|
|
|
|
|
});
|