shouldReceive('getBuilderCompletedOrFail') ->once() ->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andReturn($order); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action($input); // THEN expect($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::SUCCESS) ->and($result->orderReference)->not->toBeNull() ->and($result->orderReference?->value)->toBe($orderReference->value); }); it('returns expiredTimeSlot output when finalizeOrder throws ExpiredPickupTimeSlotException', function (): void { // GIVEN $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andThrow(new ExpiredPickupTimeSlotException('Invalid or unavailable time slot')); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action($input); // THEN expect($action)->not->toBeInstanceOf(HandlesFailure::class) ->and($action)->not->toBeInstanceOf(HandlesSuccess::class) ->and($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::EXPIRED_TIME_SLOT) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBe('Invalid or unavailable time slot'); }); it('returns unorderableProduct output when finalizeOrder throws UnorderableProductException', function (): void { // GIVEN $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andThrow(new UnorderableProductException('One or more products in your basket are no longer available for order.')); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action($input); // THEN expect($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::UNORDERABLE_PRODUCT) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBe('One or more products in your basket are no longer available for order.'); }); it('returns orderLimitExceeded output when finalizeOrder throws OrderLimitExceededException', function (): void { // GIVEN $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andThrow(new OrderLimitExceededException()); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action($input); // THEN expect($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::ORDER_LIMIT_EXCEEDED) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBe('The total amount of this order has been exceeded. Please modify it to validate.'); }); it('returns weeklyLimitExceeded output when finalizeOrder throws WeeklyLimitExceededException', function (): void { // GIVEN $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andThrow(new WeeklyLimitExceededException()); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action($input); // THEN expect($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::WEEKLY_LIMIT_EXCEEDED) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBe('The total amount for this delivery date has been exceeded. Please modify it to validate.'); }); it('propagates RegateExportFailedException so ActionExecutorService rolls back the transaction', function (): void { // GIVEN $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail')->once()->andReturn($builder); $orderTunnelService->shouldReceive('finalizeOrder') ->once() ->with($builder) ->andThrow(new RegateExportFailedException('SMTP transport error')); $action = new FinalizeOrder($orderTunnelService); // WHEN expect(fn () => $action($input)) // THEN ->toThrow(RegateExportFailedException::class, 'SMTP transport error'); }); it('has Locked attribute with expected TTL and wait seconds and implements lock contracts', function (): void { // GIVEN $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $action = new FinalizeOrder($orderTunnelService); $reflection = new \ReflectionClass(FinalizeOrder::class); $attributes = $reflection->getAttributes(Locked::class); // WHEN $locked = ($attributes[0] ?? null)?->newInstance(); // THEN expect($locked)->toBeInstanceOf(Locked::class) ->and($locked->ttlSeconds)->toBe(30) ->and($locked->waitSeconds)->toBe(10) ->and($locked->key)->toBeNull() ->and($action)->toBeInstanceOf(ProvidesLockKey::class) ->and($action)->toBeInstanceOf(HandlesLockTimeout::class); }); it('builds lockKey from input shopperId when provided', function (): void { // GIVEN $shopperId = new ShopperId(Uuid::uuid7()); $input = new FinalizeOrderInput(shopperId: $shopperId); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldNotReceive('getBuilderCompletedOrFail'); $action = new FinalizeOrder($orderTunnelService); // WHEN $key = $action->lockKey($input); // THEN expect($key)->toBe("order:finalize:shopper:{$shopperId->value}"); }); it('builds lockKey from completed builder when input shopperId is null', function (): void { // GIVEN $shopperId = new ShopperId(Uuid::uuid7()); $input = new FinalizeOrderInput(); $builder = \Mockery::mock(OrderBuilderCompleted::class); $builder->shouldReceive('getShopperId') ->once() ->andReturn($shopperId); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $orderTunnelService->shouldReceive('getBuilderCompletedOrFail') ->once() ->andReturn($builder); $action = new FinalizeOrder($orderTunnelService); // WHEN $key = $action->lockKey($input); // THEN expect($key)->toBe("order:finalize:shopper:{$shopperId->value}"); }); it('returns lockTimeout output on lock timeout', function (): void { // GIVEN $input = new FinalizeOrderInput(); $orderTunnelService = \Mockery::mock(OrderTunnelService::class); $action = new FinalizeOrder($orderTunnelService); // WHEN $result = $action->onLockTimeout($input); // THEN expect($result)->toBeInstanceOf(FinalizeOrderOutput::class) ->and($result->status)->toBe(FinalizeOrderStatus::LOCK_TIMEOUT) ->and($result->orderReference)->toBeNull() ->and($result->errorMessage)->toBeNull(); }); });