ldc-cocotte/tests/Unit/App/Actions/Orders/FinalizeOrderTest.php

181 lines
7.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Cqs\Actions\Orders\FinalizeOrder;
use App\Cqs\Actions\Orders\FinalizeOrderInput;
use App\Cqs\Actions\Orders\FinalizeOrderOutput;
use App\Enums\FinalizeOrderStatus;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Enums\OrderStatus;
use Domain\Exceptions\OrderTunnel\ExpiredPickupTimeSlotException;
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
use Domain\Models\Entities\Order;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Entities\OrderId;
use Domain\Models\ValueObjects\Entities\ShopId;
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\Services\OrderTunnelService;
use Money\Money;
use Ramsey\Uuid\Uuid;
describe('FinalizeOrder', function (): void {
it('finalizes order and returns reference via OrderTunnelService', function (): void {
// GIVEN
$input = new FinalizeOrderInput();
$orderReference = new OrderReference(Uuid::uuid7());
$builder = \Mockery::mock(OrderBuilderCompleted::class);
$order = new Order(
id: new OrderId(Uuid::uuid7()),
shopId: new ShopId(Uuid::uuid7()),
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(now(), new \DateInterval('PT1H')),
status: OrderStatus::WAITING,
total: Money::EUR(1000),
lines: [],
);
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$orderTunnelService->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 ExpiredPickupTimeSlotException is thrown', 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($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 UnorderableProductException is thrown', 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 OrderLimitExceededException is thrown', 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 WeeklyLimitExceededException is thrown', 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.');
});
});