1128 lines
44 KiB
PHP
1128 lines
44 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use Carbon\CarbonImmutable;
|
|
use Carbon\WeekDay;
|
|
use Domain\Builders\OrderBuilder;
|
|
use Domain\Builders\OrderBuilderCompleted;
|
|
use Domain\Builders\OrderBuilderWithBasket;
|
|
use Domain\Enums\OrderStatus;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Events\AnOrderHasBeenPlaced;
|
|
use Domain\Exceptions\OrderTunnel\BasketPriceChangedException;
|
|
use Domain\Exceptions\OrderTunnel\ExpiredPickupTimeSlotException;
|
|
use Domain\Exceptions\OrderTunnel\InactiveShopperException;
|
|
use Domain\Exceptions\OrderTunnel\MissingOrderBuilderException;
|
|
use Domain\Exceptions\OrderTunnel\RegateExportFailedException;
|
|
use Domain\Exceptions\OrderTunnel\UnorderableProductException;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\Entities\Order;
|
|
use Domain\Models\Entities\Shop;
|
|
use Domain\Models\Entities\Shopper;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
use Domain\Models\ValueObjects\Entities\OrderId;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\Entities\ShopId;
|
|
use Domain\Models\ValueObjects\Entities\ShopperId;
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
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\Policies\CustomerOrderLimitPolicy;
|
|
use Domain\Ports\Repositories\OrderRepository;
|
|
use Domain\Ports\Repositories\PickupExceptionRepository;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
use Domain\Ports\Repositories\ShopperRepository;
|
|
use Domain\Ports\Repositories\ShopRepository;
|
|
use Domain\Ports\Services\EmployeeStatisticsService;
|
|
use Domain\Ports\Services\OrderBuilderSessionManager;
|
|
use Domain\Ports\Services\RegateService;
|
|
use Domain\Ports\Services\TunnelAttemptTrackerService;
|
|
use Domain\Services\OrderService;
|
|
use Domain\Services\OrderTunnelService;
|
|
use Domain\Services\PickupSchedulingService;
|
|
use Domain\Services\ShopperService;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Shared\DDD\DomainEventDispatcher;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
function createTestBasket(int $qty = 0): Basket
|
|
{
|
|
$items = [];
|
|
|
|
if ($qty > 0) {
|
|
$productId = new ProductId(UuidHelper::fromString('019e9e37-f2a1-74ef-b9c2-66d222470128'));
|
|
$basketProduct = new BasketProduct(
|
|
id: $productId,
|
|
name: 'Test Product',
|
|
qty: $qty,
|
|
unitPriceHt: Money::EUR(100),
|
|
unitDiscount: Money::EUR(0),
|
|
taxRate: TaxRate::TVA_20,
|
|
weightGrams: 100,
|
|
brand: null,
|
|
incoId: new IncoId(123),
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'test-product',
|
|
picture: null,
|
|
description: null,
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
priceKgTtc: Money::EUR(120)
|
|
);
|
|
$items[(string) $productId->value] = $basketProduct;
|
|
}
|
|
|
|
$basketId = new BasketId(UuidHelper::fromString('019e9e37-f2a1-74ef-b9c2-66d222470128'));
|
|
|
|
return new Basket($basketId, $items);
|
|
}
|
|
|
|
/**
|
|
* @return ($completed is true ? OrderBuilderCompleted : OrderBuilderWithBasket)
|
|
*/
|
|
function createTestBuilder(bool $completed = false): OrderBuilderWithBasket
|
|
{
|
|
$basket = createTestBasket(1);
|
|
$shopperId = new ShopperId(UuidHelper::v7());
|
|
$builder = OrderBuilder::fromBasket($basket, $shopperId);
|
|
|
|
if ($completed) {
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
|
|
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
|
|
|
|
return $builder->withPickupTimeSlot($shopId, $timeSlot);
|
|
}
|
|
|
|
return $builder;
|
|
}
|
|
|
|
function createOrderTunnelService(
|
|
?ProductRepository $productRepository = null,
|
|
?ShopRepository $shopRepository = null,
|
|
?ShopperRepository $shopperRepository = null,
|
|
?PickupExceptionRepository $pickupExceptionRepository = null,
|
|
?OrderBuilderSessionManager $orderBuilderSessionManager = null,
|
|
?OrderRepository $orderRepository = null,
|
|
?DomainEventDispatcher $eventDispatcher = null,
|
|
?RegateService $regateService = null,
|
|
?TunnelAttemptTrackerService $tunnelAttemptTracker = null,
|
|
): OrderTunnelService {
|
|
if ($productRepository === null) {
|
|
$productRepository = \Mockery::mock(ProductRepository::class);
|
|
$productRepository->shouldReceive('getBasketProduct')->andReturnUsing(function (ProductId $id, int $qty): BasketProduct {
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: 'Test Product',
|
|
qty: $qty,
|
|
unitPriceHt: Money::EUR(100),
|
|
unitDiscount: Money::EUR(0),
|
|
taxRate: TaxRate::TVA_20,
|
|
weightGrams: 100,
|
|
brand: null,
|
|
incoId: new IncoId(123),
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'test-product',
|
|
picture: null,
|
|
description: null,
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
priceKgTtc: Money::EUR(120)
|
|
);
|
|
})->byDefault();
|
|
}
|
|
$shopRepository ??= \Mockery::mock(ShopRepository::class);
|
|
$shopperRepository ??= \Mockery::mock(ShopperRepository::class);
|
|
if ($pickupExceptionRepository === null) {
|
|
$pickupExceptionRepository = \Mockery::mock(PickupExceptionRepository::class);
|
|
$pickupExceptionRepository->shouldReceive('getExceptionsForShop')->andReturn([])->byDefault();
|
|
}
|
|
$orderBuilderSessionManager ??= \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderRepository ??= \Mockery::mock(OrderRepository::class);
|
|
|
|
$eventDispatcher ??= \Mockery::mock(DomainEventDispatcher::class);
|
|
|
|
$employeeStatisticsService = \Mockery::mock(EmployeeStatisticsService::class);
|
|
$employeeStatisticsService->shouldReceive('getWeeklyTotalForEmployee')->andReturn(Money::EUR(0))->byDefault();
|
|
|
|
$shopperService = new ShopperService($shopRepository, $shopperRepository);
|
|
$pickupService = new PickupSchedulingService($pickupExceptionRepository);
|
|
$orderService = new OrderService($orderRepository);
|
|
|
|
if ($regateService === null) {
|
|
$regateService = \Mockery::mock(RegateService::class);
|
|
$regateService->shouldReceive('processOrder')->byDefault();
|
|
}
|
|
|
|
if ($tunnelAttemptTracker === null) {
|
|
$tunnelAttemptTracker = \Mockery::mock(TunnelAttemptTrackerService::class);
|
|
$tunnelAttemptTracker->shouldReceive('recordEntry')->byDefault();
|
|
$tunnelAttemptTracker->shouldReceive('markConverted')->byDefault();
|
|
$tunnelAttemptTracker->shouldReceive('clearActiveSession')->byDefault();
|
|
}
|
|
|
|
$orderLimitPolicy = new CustomerOrderLimitPolicy(500000, 500000, new Currency('EUR'));
|
|
|
|
return new OrderTunnelService(
|
|
$eventDispatcher,
|
|
$shopperService,
|
|
$orderBuilderSessionManager,
|
|
$pickupService,
|
|
$orderRepository,
|
|
$productRepository,
|
|
$orderLimitPolicy,
|
|
$shopRepository,
|
|
$shopperRepository,
|
|
$orderService,
|
|
$employeeStatisticsService,
|
|
$regateService,
|
|
$tunnelAttemptTracker,
|
|
);
|
|
}
|
|
|
|
beforeEach(function (): void {
|
|
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
|
|
CarbonImmutable::setTestNow($now);
|
|
Carbon::setTestNow($now);
|
|
});
|
|
|
|
afterEach(function (): void {
|
|
CarbonImmutable::setTestNow();
|
|
Carbon::setTestNow();
|
|
});
|
|
|
|
describe('OrderTunnelService', function (): void {
|
|
it('allows access to BOOKING step when builder is from basket and has items', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder();
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessBooking();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeTrue();
|
|
});
|
|
|
|
it('denies access to BOOKING step when builder has an empty basket', function (): void {
|
|
// GIVEN
|
|
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(UuidHelper::v7()));
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessBooking();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeFalse();
|
|
});
|
|
|
|
it('denies access to BOOKING step when builder is missing', function (): void {
|
|
// GIVEN
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn(null);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessBooking();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeFalse();
|
|
});
|
|
|
|
it('allows access to CONFIRMATION step when builder is completed and has items', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessConfirmation();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeTrue();
|
|
});
|
|
|
|
it('denies access to CONFIRMATION step when builder is not completed', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: false);
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessConfirmation();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeFalse();
|
|
});
|
|
|
|
it('denies access to CONFIRMATION step when builder has an empty basket', function (): void {
|
|
// GIVEN
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
|
|
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
|
|
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(UuidHelper::v7()))
|
|
->withPickupTimeSlot($shopId, $timeSlot);
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessConfirmation();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeFalse();
|
|
});
|
|
|
|
it('denies access to CONFIRMATION step when builder is missing', function (): void {
|
|
// GIVEN
|
|
$orderBuilderRepository = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepository->shouldReceive('get')->andReturn(null);
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderRepository
|
|
);
|
|
|
|
// WHEN
|
|
$canAccess = $service->canAccessConfirmation();
|
|
|
|
// THEN
|
|
expect($canAccess)->toBeFalse();
|
|
});
|
|
|
|
it('gets shop for customer successfully', function (): void {
|
|
// GIVEN
|
|
$shopperId = new ShopperId(UuidHelper::v7());
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$shop = new Shop(
|
|
id: $shopId,
|
|
name: 'Shop',
|
|
address: 'Address',
|
|
postcode: '75000',
|
|
city: 'Paris'
|
|
);
|
|
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('find')->with($shopperId)->andReturn($shopper);
|
|
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('findExceptionShopFor')->with($shopper->matricule)->andReturn(null);
|
|
$shopRepo->shouldReceive('findShopForSiteAndService')->with($shopper->site, $shopper->service)->andReturn(null);
|
|
$shopRepo->shouldReceive('findShopForSiteAndService')->with($shopper->site, null)->andReturn(null);
|
|
$shopRepo->shouldReceive('getDefaultShop')->andReturn($shop);
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo
|
|
);
|
|
|
|
// WHEN
|
|
$result = $service->getShopForCustomer($shopperId);
|
|
|
|
// THEN
|
|
expect($result)->toBe($shop);
|
|
});
|
|
|
|
it('throws exception when get shop for customer fails', function (): void {
|
|
// GIVEN
|
|
$shopperId = new ShopperId(UuidHelper::v7());
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('find')->with($shopperId)->andReturn(null);
|
|
|
|
$service = createOrderTunnelService(shopperRepository: $shopperRepo);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->getShopForCustomer($shopperId);
|
|
// THEN
|
|
expect($action)->toThrow(\UnexpectedValueException::class);
|
|
});
|
|
|
|
it('gets available time slots', function (): void {
|
|
// GIVEN
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
|
|
$pickupExceptionRepo = \Mockery::mock(PickupExceptionRepository::class);
|
|
$pickupExceptionRepo->shouldReceive('getExceptionsForShop')->andReturn([]);
|
|
|
|
$service = createOrderTunnelService(pickupExceptionRepository: $pickupExceptionRepo);
|
|
|
|
// WHEN
|
|
$slots = $service->getAvailableTimeSlots($shop);
|
|
|
|
// THEN
|
|
expect($slots)->not->toBeEmpty();
|
|
});
|
|
|
|
it('initializes order from basket', function (): void {
|
|
// GIVEN
|
|
$basket = createTestBasket(1);
|
|
$customerId = new ShopperId(UuidHelper::v7());
|
|
|
|
/** @var OrderBuilderSessionManager&\Mockery\MockInterface $orderBuilderRepo */
|
|
$orderBuilderRepo = \Mockery::spy(OrderBuilderSessionManager::class);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$service->initializeFromBasket($basket, $customerId);
|
|
|
|
// THEN
|
|
$orderBuilderRepo->shouldHaveReceived('save')->once();
|
|
});
|
|
|
|
it('gets builder from basket or fail', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder();
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$result = $service->getBuilderFromBasketOrFail();
|
|
|
|
// THEN
|
|
expect($result)->toBe($builder);
|
|
});
|
|
|
|
it('throws exception if builder not from basket', function (): void {
|
|
// GIVEN
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn(null);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->getBuilderFromBasketOrFail();
|
|
// THEN
|
|
expect($action)->toThrow(MissingOrderBuilderException::class);
|
|
});
|
|
|
|
it('throws exception in getBuilderFromBasketOrFail when builder has an empty basket', function (): void {
|
|
// GIVEN
|
|
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(UuidHelper::v7()));
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->getBuilderFromBasketOrFail();
|
|
|
|
// THEN
|
|
expect($action)->toThrow(MissingOrderBuilderException::class);
|
|
});
|
|
|
|
it('gets completed builder or fail', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$result = $service->getBuilderCompletedOrFail();
|
|
|
|
// THEN
|
|
expect($result)->toBe($builder);
|
|
});
|
|
|
|
it('throws exception if builder not completed', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: false);
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->getBuilderCompletedOrFail();
|
|
// THEN
|
|
expect($action)->toThrow(MissingOrderBuilderException::class);
|
|
});
|
|
|
|
it('throws exception in getBuilderCompletedOrFail when builder has an empty basket', function (): void {
|
|
// GIVEN
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$nextFriday = CarbonImmutable::now('Europe/Paris')->next(WeekDay::Friday->value)->addWeek()->startOfDay();
|
|
$timeSlot = new PickupTimeSlot($nextFriday, new \DateInterval('PT1H'));
|
|
$builder = OrderBuilder::fromBasket(createTestBasket(0), new ShopperId(UuidHelper::v7()))->withPickupTimeSlot($shopId, $timeSlot);
|
|
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('get')->once()->andReturn($builder);
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->getBuilderCompletedOrFail();
|
|
|
|
// THEN
|
|
expect($action)->toThrow(MissingOrderBuilderException::class);
|
|
});
|
|
|
|
it('applies time slot', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: false);
|
|
$shop = new Shop(new ShopId(UuidHelper::v7()), 'Shop', 'Address', '75000', 'Paris');
|
|
$selectedDate = CarbonImmutable::parse('2025-01-01');
|
|
$ts1 = new PickupTimeSlot($selectedDate, new \DateInterval('PT1H'));
|
|
$ts2 = new PickupTimeSlot(CarbonImmutable::parse('2025-01-02'), new \DateInterval('PT1H'));
|
|
$availableSlots = [$ts1, $ts2];
|
|
|
|
$orderBuilderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderRepo->shouldReceive('save')->once();
|
|
|
|
$service = createOrderTunnelService(orderBuilderSessionManager: $orderBuilderRepo);
|
|
|
|
// WHEN
|
|
$matched = $service->applyTimeSlot($builder, $shop, $selectedDate, $availableSlots);
|
|
|
|
// THEN
|
|
expect($matched)->toBe($ts1);
|
|
});
|
|
|
|
it('returns null if no matching time slot', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: false);
|
|
$shop = new Shop(new ShopId(UuidHelper::v7()), 'Shop', 'Address', '75000', 'Paris');
|
|
$selectedDate = CarbonImmutable::parse('2025-01-01');
|
|
$ts1 = new PickupTimeSlot(CarbonImmutable::parse('2025-01-02'), new \DateInterval('PT1H'));
|
|
$availableSlots = [$ts1];
|
|
|
|
$service = createOrderTunnelService();
|
|
|
|
// WHEN
|
|
$matched = $service->applyTimeSlot($builder, $shop, $selectedDate, $availableSlots);
|
|
|
|
// THEN
|
|
expect($matched)->toBeNull();
|
|
});
|
|
|
|
it('finalizes order', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = $builder->getShopperId();
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper);
|
|
|
|
$shopId = clone clone $builder->getShopId();
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn($shop); // any with shopId is fine
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('getWeeklyTotalForEmployee')->andReturn(Money::EUR(5000));
|
|
$orderRepo->shouldReceive('persist')->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldReceive('dispatch')->once();
|
|
|
|
$builderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$builderRepo->shouldNotReceive('clear');
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderBuilderSessionManager: $builderRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher
|
|
);
|
|
|
|
// WHEN
|
|
$order = $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($order->employee->matricule->value)->toBe('1234567');
|
|
expect($order->deliveryAddress->name)->toBe('Shop');
|
|
});
|
|
|
|
it('throws exception if shop not found during finalize order', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = clone clone $builder->getShopperId();
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper);
|
|
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn(null);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('getWeeklyTotalForEmployee')->andReturn(Money::EUR(5000));
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderRepository: $orderRepo,
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->finalizeOrder($builder);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('revalidates basket prices from product repository when finalizing order', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = $builder->getShopperId();
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper);
|
|
|
|
$shopId = clone $builder->getShopId();
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn($shop);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('getWeeklyTotalForEmployee')->andReturn(Money::EUR(5000));
|
|
$orderRepo->shouldReceive('persist')->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldReceive('dispatch')->once();
|
|
|
|
$builderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$builderRepo->shouldNotReceive('clear');
|
|
|
|
$productRepo = \Mockery::mock(ProductRepository::class);
|
|
$productRepo->shouldReceive('getBasketProduct')->once()->andReturnUsing(function (ProductId $id, int $qty): BasketProduct {
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: 'Updated Product Name',
|
|
qty: $qty,
|
|
unitPriceHt: Money::EUR(100),
|
|
unitDiscount: Money::EUR(0),
|
|
taxRate: TaxRate::TVA_20,
|
|
weightGrams: 200,
|
|
brand: null,
|
|
incoId: new IncoId(123),
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'updated-product',
|
|
picture: null,
|
|
description: null,
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.0),
|
|
priceKgTtc: Money::EUR(120)
|
|
);
|
|
});
|
|
|
|
$service = createOrderTunnelService(
|
|
productRepository: $productRepo,
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderBuilderSessionManager: $builderRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher
|
|
);
|
|
|
|
// WHEN
|
|
$order = $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($order->status)->toBe(OrderStatus::VALIDATED);
|
|
});
|
|
|
|
it('throws BasketPriceChangedException with refreshed basket when product price changes before finalization', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldNotReceive('getByIdOrFail');
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldNotReceive('persist');
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$productRepo = \Mockery::mock(ProductRepository::class);
|
|
$productRepo->shouldReceive('getBasketProduct')->once()->andReturnUsing(function (ProductId $id, int $qty): BasketProduct {
|
|
return new BasketProduct(
|
|
id: $id,
|
|
name: 'Updated Price Product',
|
|
qty: $qty,
|
|
unitPriceHt: Money::EUR(250),
|
|
unitDiscount: Money::EUR(50),
|
|
taxRate: TaxRate::TVA_20,
|
|
weightGrams: 200,
|
|
brand: null,
|
|
incoId: new IncoId(123),
|
|
orderUnit: OrderUnit::PCE,
|
|
slug: 'updated-price-product',
|
|
picture: null,
|
|
description: null,
|
|
weightVariable: false,
|
|
discountRate: DiscountRate::fromRatio(0.2),
|
|
priceKgTtc: Money::EUR(240)
|
|
);
|
|
});
|
|
|
|
$service = createOrderTunnelService(
|
|
productRepository: $productRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher
|
|
);
|
|
|
|
// WHEN
|
|
$caught = null;
|
|
|
|
try {
|
|
$service->finalizeOrder($builder);
|
|
} catch (BasketPriceChangedException $e) {
|
|
$caught = $e;
|
|
}
|
|
|
|
// THEN
|
|
expect($caught)->toBeInstanceOf(BasketPriceChangedException::class)
|
|
->and($caught?->refreshedBasket->totalTaxIncluded()->getAmount())->toBe('240');
|
|
});
|
|
|
|
it('throws ExpiredPickupTimeSlotException when finalizing order with an expired or unavailable pickup time slot', function (): void {
|
|
// GIVEN
|
|
$basket = createTestBasket(1);
|
|
$shopperId = new ShopperId(UuidHelper::v7());
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$expiredSlot = new PickupTimeSlot(CarbonImmutable::parse('2025-01-01'), new \DateInterval('PT1H'));
|
|
$builder = OrderBuilder::fromBasket($basket, $shopperId)->withPickupTimeSlot($shopId, $expiredSlot);
|
|
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper);
|
|
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn($shop);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldNotReceive('persist');
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$builderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$builderRepo->shouldNotReceive('clear');
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderBuilderSessionManager: $builderRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(ExpiredPickupTimeSlotException::class);
|
|
});
|
|
|
|
it('throws UnorderableProductException when finalizing order with an unorderable product in basket', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
|
|
$productRepo = \Mockery::mock(ProductRepository::class);
|
|
$productRepo->shouldReceive('getBasketProduct')
|
|
->once()
|
|
->andThrow(new UnorderableProductException());
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldNotReceive('persist');
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$builderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$builderRepo->shouldNotReceive('clear');
|
|
|
|
$service = createOrderTunnelService(
|
|
productRepository: $productRepo,
|
|
orderBuilderSessionManager: $builderRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(UnorderableProductException::class);
|
|
});
|
|
|
|
it('propagates RegateExportFailedException without dispatching AnOrderHasBeenPlaced', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = $builder->getShopperId();
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('test@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($shopper);
|
|
|
|
$shopId = clone $builder->getShopId();
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn($shop);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('persist')->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$builderRepo = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$builderRepo->shouldNotReceive('clear');
|
|
|
|
$regateService = \Mockery::mock(RegateService::class);
|
|
$regateService->shouldReceive('processOrder')->once()->andThrow(new RegateExportFailedException('SMTP failed'));
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderBuilderSessionManager: $builderRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher,
|
|
regateService: $regateService,
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(RegateExportFailedException::class, 'SMTP failed');
|
|
});
|
|
|
|
it('throws InactiveShopperException when shopper is inactive during finalizeOrder', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = $builder->getShopperId();
|
|
$inactiveShopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('inactive@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'Jane',
|
|
startDate: CarbonImmutable::now()->subMonth(),
|
|
endDate: CarbonImmutable::now()->subDay(),
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->with(\Mockery::type(ShopperId::class))->andReturn($inactiveShopper);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldNotReceive('persist');
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$service = createOrderTunnelService(
|
|
shopperRepository: $shopperRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher,
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn () => $service->finalizeOrder($builder);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(InactiveShopperException::class);
|
|
});
|
|
|
|
it('places a WAITING order without calling Regate or dispatching AnOrderHasBeenPlaced', function (): void {
|
|
// GIVEN
|
|
$builder = createTestBuilder(completed: true);
|
|
$shopperId = $builder->getShopperId();
|
|
$shopper = new Shopper(
|
|
id: $shopperId,
|
|
email: new Email('pending@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$shopperRepo = \Mockery::mock(ShopperRepository::class);
|
|
$shopperRepo->shouldReceive('getByIdOrFail')->andReturn($shopper);
|
|
|
|
$shop = new Shop(clone $builder->getShopId(), 'Shop', 'Address', '75000', 'Paris');
|
|
$shopRepo = \Mockery::mock(ShopRepository::class);
|
|
$shopRepo->shouldReceive('find')->andReturn($shop);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('persist')->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldNotReceive('dispatch');
|
|
|
|
$regateService = \Mockery::mock(RegateService::class);
|
|
$regateService->shouldNotReceive('processOrder');
|
|
|
|
$service = createOrderTunnelService(
|
|
shopRepository: $shopRepo,
|
|
shopperRepository: $shopperRepo,
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher,
|
|
regateService: $regateService,
|
|
);
|
|
|
|
// WHEN
|
|
$order = $service->placePendingOrder($builder);
|
|
|
|
// THEN
|
|
expect($order->status)->toBe(OrderStatus::PENDING);
|
|
});
|
|
|
|
it('exports, confirms and fails a pending order across split steps', function (): void {
|
|
// GIVEN
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopper = new Shopper(
|
|
id: new ShopperId(UuidHelper::v7()),
|
|
email: new Email('split@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$basket = createTestBasket(1);
|
|
$orderToOk = Order::pendingFromBasket(
|
|
new OrderId(UuidHelper::v7()),
|
|
new OrderReference(UuidHelper::v7()),
|
|
$basket,
|
|
new PickupTimeSlot(CarbonImmutable::now()->next('Friday'), new \DateInterval('PT1H')),
|
|
$shopper,
|
|
$shop
|
|
);
|
|
$orderToKo = Order::pendingFromBasket(
|
|
new OrderId(UuidHelper::v7()),
|
|
new OrderReference(UuidHelper::v7()),
|
|
$basket,
|
|
new PickupTimeSlot(CarbonImmutable::now()->next('Friday'), new \DateInterval('PT1H')),
|
|
$shopper,
|
|
$shop
|
|
);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('find')->with($orderToOk->id())->andReturn($orderToOk);
|
|
$orderRepo->shouldReceive('findForUpdate')->with($orderToOk->id())->andReturn($orderToOk);
|
|
$orderRepo->shouldReceive('persist')->with($orderToOk)->once();
|
|
$orderRepo->shouldReceive('findForUpdate')->with($orderToKo->id())->andReturn($orderToKo);
|
|
$orderRepo->shouldReceive('persist')->with($orderToKo)->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldReceive('dispatch')
|
|
->once()
|
|
->with(\Mockery::type(AnOrderHasBeenPlaced::class));
|
|
|
|
$regateService = \Mockery::mock(RegateService::class);
|
|
$regateService->shouldReceive('processOrder')->once();
|
|
|
|
$service = createOrderTunnelService(
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher,
|
|
regateService: $regateService,
|
|
);
|
|
|
|
// WHEN
|
|
$service->exportOrderToRegate($orderToOk->id());
|
|
$confirmed = $service->confirmPlacedOrder($orderToOk->id());
|
|
$service->failPendingOrder($orderToKo->id());
|
|
|
|
// THEN
|
|
expect($confirmed->status)->toBe(OrderStatus::VALIDATED)
|
|
->and($orderToKo->status)->toBe(OrderStatus::FAILED);
|
|
});
|
|
|
|
it('records a tunnel attempt when initializing from basket', function (): void {
|
|
// GIVEN
|
|
$basket = createTestBasket(1);
|
|
$customerId = new ShopperId(UuidHelper::v7());
|
|
$recorded = false;
|
|
|
|
$orderBuilderSessionManager = \Mockery::mock(OrderBuilderSessionManager::class);
|
|
$orderBuilderSessionManager->shouldReceive('save')->once();
|
|
|
|
$tracker = \Mockery::mock(TunnelAttemptTrackerService::class);
|
|
$tracker->shouldReceive('recordEntry')
|
|
->once()
|
|
->with($customerId)
|
|
->andReturnUsing(function () use (&$recorded): void {
|
|
$recorded = true;
|
|
});
|
|
|
|
$service = createOrderTunnelService(
|
|
orderBuilderSessionManager: $orderBuilderSessionManager,
|
|
tunnelAttemptTracker: $tracker,
|
|
);
|
|
|
|
// WHEN
|
|
$service->initializeFromBasket($basket, $customerId);
|
|
|
|
// THEN
|
|
expect($recorded)->toBeTrue();
|
|
});
|
|
|
|
it('marks the tunnel attempt converted when confirming a placed order', function (): void {
|
|
// GIVEN
|
|
$shopId = new ShopId(UuidHelper::v7());
|
|
$shop = new Shop($shopId, 'Shop', 'Address', '75000', 'Paris');
|
|
$shopper = new Shopper(
|
|
id: new ShopperId(UuidHelper::v7()),
|
|
email: new Email('convert@test.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subYear(),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('ETAB01'),
|
|
site: new ShopperSiteCode('SITEA'),
|
|
service: new ShopperServiceCode('101')
|
|
);
|
|
$basket = createTestBasket(1);
|
|
$order = Order::pendingFromBasket(
|
|
new OrderId(UuidHelper::v7()),
|
|
new OrderReference(UuidHelper::v7()),
|
|
$basket,
|
|
new PickupTimeSlot(CarbonImmutable::now()->next('Friday'), new \DateInterval('PT1H')),
|
|
$shopper,
|
|
$shop
|
|
);
|
|
|
|
$orderRepo = \Mockery::mock(OrderRepository::class);
|
|
$orderRepo->shouldReceive('findForUpdate')->with($order->id())->andReturn($order);
|
|
$orderRepo->shouldReceive('persist')->with($order)->once();
|
|
|
|
$eventDispatcher = \Mockery::mock(DomainEventDispatcher::class);
|
|
$eventDispatcher->shouldReceive('dispatch')->once()->with(\Mockery::type(AnOrderHasBeenPlaced::class));
|
|
|
|
$tracker = \Mockery::mock(TunnelAttemptTrackerService::class);
|
|
$tracker->shouldReceive('markConverted')->once()->with($order->id());
|
|
|
|
$service = createOrderTunnelService(
|
|
orderRepository: $orderRepo,
|
|
eventDispatcher: $eventDispatcher,
|
|
tunnelAttemptTracker: $tracker,
|
|
);
|
|
|
|
// WHEN
|
|
$confirmed = $service->confirmPlacedOrder($order->id());
|
|
|
|
// THEN
|
|
expect($confirmed->status)->toBe(OrderStatus::VALIDATED);
|
|
});
|
|
});
|