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 expect($result->status)->toBe(PlacePendingOrderStatus::PENDING_CREATED) ->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.', ], ]); it('persists refreshed basket and returns basketPriceChanged when BasketPriceChangedException is thrown', function (): void { // GIVEN $input = new PlacePendingOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $refreshedBasket = new Basket(new BasketId(UuidHelper::v7()), []); /** @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 expect($result->status)->toBe(PlacePendingOrderStatus::BASKET_PRICE_CHANGED) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBe('The price of one or more products in your basket has changed.'); }); it('builds shopper lock key and returns LOCK_TIMEOUT output on lock timeout', function (): void { // GIVEN $shopperId = new ShopperId(UuidHelper::v7()); $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); $action = new PlacePendingOrder($orderTunnelService, $basketRepository); // WHEN $key = $action->lockKey($input); $timeout = $action->onLockTimeout($input); // THEN expect($key)->toBe("order:finalize:shopper:{$shopperId->value}") ->and($timeout->status)->toBe(PlacePendingOrderStatus::LOCK_TIMEOUT); }); }); describe('ExportOrderToRegate, ConfirmPlacedOrder and FailPendingOrder', function (): void { it('exports order via OrderTunnelService and handles lock timeout', function (): void { // GIVEN $orderId = new OrderId(UuidHelper::v7()); $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); }); it('confirms a placed order and fails a pending order', function (): void { // GIVEN $orderId = new OrderId(UuidHelper::v7()); $order = new Order( id: $orderId, shopId: new ShopId(UuidHelper::v7()), reference: new OrderReference(UuidHelper::v7()), 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); $orderTunnelService->shouldReceive('failPendingOrder') ->once() ->with($orderId); $confirmAction = new ConfirmPlacedOrder($orderTunnelService); $failAction = new FailPendingOrder($orderTunnelService); // WHEN $confirmResult = $confirmAction(new ConfirmPlacedOrderInput($orderId)); $failResult = $failAction(new FailPendingOrderInput($orderId)); // THEN expect($confirmResult)->toBe(ConfirmPlacedOrderOutput::CONFIRMED) ->and($failResult)->toBe(FailPendingOrderOutput::FAILED); }); });