ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Orders/ApplyPickTimeSlotTest.php

189 lines
7.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Actions\Orders;
use App\Cqs\Actions\Orders\ApplyPickTimeSlot;
use App\Cqs\Actions\Orders\ApplyPickTimeSlotInput;
use App\Enums\ApplyPickTimeSlotStatus;
use App\Models\Customer;
use App\Models\Employee;
use App\Models\PickupException;
use App\Models\Shop;
use Carbon\CarbonImmutable;
use Domain\Builders\OrderBuilder;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\Entities\Basket;
use Domain\Models\ValueObjects\BasketProduct;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\DiscountRate;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\BasketId;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Domain\Policies\CustomerOrderLimitPolicy;
use Domain\Ports\Services\RegateService;
use Domain\Services\OrderService;
use Domain\Services\OrderTunnelService;
use Domain\Services\PickupSchedulingService;
use Domain\Services\ShopperService;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Session;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Repositories\EloquentOrderRepository;
use Infrastructure\Adapters\Repositories\EloquentPickupExceptionRepository;
use Infrastructure\Adapters\Repositories\EloquentProductRepository;
use Infrastructure\Adapters\Repositories\EloquentShopperRepository;
use Infrastructure\Adapters\Repositories\EloquentShopRepository;
use Infrastructure\Adapters\Services\DatabaseTunnelAttemptTrackerService;
use Infrastructure\Adapters\Services\EloquentEmployeeStatisticsService;
use Infrastructure\Adapters\Services\SessionOrderBuilderManager;
use Money\Currency;
use Money\Money;
use Shared\Helpers\UuidHelper;
use Shared\ValueObjects\CrmHashcode;
describe('ApplyPickTimeSlot action', function (): void {
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-11-16 10:00:00', 'Europe/Paris');
CarbonImmutable::setTestNow($now);
Date::setTestNow($now);
});
afterEach(function (): void {
CarbonImmutable::setTestNow();
Date::setTestNow();
});
/**
* @return array{0: Customer, 1: Shop, 2: SessionOrderBuilderManager, 3: OrderTunnelService}
*/
function makePickSlotEnvironment(): array
{
$shop = Shop::factory()->create(['default' => true]);
$customer = new Customer();
$customer->email = new Email('slot.buyer@example.com');
$customer->hashcode = new CrmHashcode(UuidHelper::v4());
$customer->saveOrFail();
$employee = new Employee();
$employee->customer_id = $customer->id;
$employee->matricule = new Matricule('1000303');
$employee->code = new Code('ABCDEF');
$employee->nom = 'Durand';
$employee->prenom = 'Sophie';
$employee->start_date = Carbon::parse('2025-01-01');
$employee->etablissement = new ShopperEstablishmentCode('01');
$employee->site = new ShopperSiteCode('SITE');
$employee->service = new ShopperServiceCode('01');
$employee->saveOrFail();
$productId = new ProductId(UuidHelper::v7());
$basket = new Basket(new BasketId(UuidHelper::v7()), [
$productId->value->toString() => new BasketProduct(
id: $productId,
name: 'Filet de Poulet',
qty: 1,
unitPriceHt: Money::EUR(1000),
unitDiscount: Money::EUR(0),
taxRate: TaxRate::TVA_5_5,
weightGrams: 500,
brand: ProductBrand::loue,
incoId: new IncoId(654321),
orderUnit: OrderUnit::PCE,
slug: 'filet-de-poulet',
picture: null,
description: null,
weightVariable: false,
discountRate: DiscountRate::zero(),
priceKgTtc: Money::EUR(1000),
options: [],
),
]);
/** @var \Illuminate\Contracts\Session\Session $session */
$session = Session::driver();
$builderManager = new SessionOrderBuilderManager($session, 'order_tunnel_builder_session');
$builderManager->save(OrderBuilder::fromBasket($basket, $customer->getId()));
$shopRepo = new EloquentShopRepository();
$shopperRepo = new EloquentShopperRepository();
$orderRepo = new EloquentOrderRepository();
/** @var \Domain\Ports\Services\RegateService&\Mockery\MockInterface $regateService */
$regateService = \Mockery::mock(RegateService::class);
$tunnelService = new OrderTunnelService(
new LaravelDomainEventDispatcher(),
new ShopperService($shopRepo, $shopperRepo),
$builderManager,
new PickupSchedulingService(new EloquentPickupExceptionRepository()),
$orderRepo,
new EloquentProductRepository(),
new CustomerOrderLimitPolicy(20000, 20000, new Currency('EUR')),
$shopRepo,
$shopperRepo,
new OrderService($orderRepo),
new EloquentEmployeeStatisticsService(),
$regateService,
new DatabaseTunnelAttemptTrackerService($session, 'order_tunnel_attempt_id'),
);
return [$customer, $shop, $builderManager, $tunnelService];
}
it('matches an open pickup day and upgrades the session builder to OrderBuilderCompleted', function (): void {
// GIVEN
[$customer, $shop, $builderManager, $tunnelService] = makePickSlotEnvironment();
$action = new ApplyPickTimeSlot($tunnelService);
// Friday of current week after Monday 10:00 cutoff -> open
$targetFriday = CarbonImmutable::parse('2026-11-20', 'Europe/Paris');
// WHEN
$output = $action(new ApplyPickTimeSlotInput($customer->getId(), $targetFriday));
// THEN
expect($output->status)->toBe(ApplyPickTimeSlotStatus::MATCHED)
->and($output->timeSlot)->not->toBeNull()
->and($output->timeSlot?->getAt()->format('Y-m-d'))->toBe('2026-11-20');
$completed = $builderManager->get();
assert($completed instanceof OrderBuilderCompleted);
expect($completed->getShopId()->equals($shop->getId()))->toBeTrue();
});
it('returns UNAVAILABLE when the requested date is blocked by a shop PickupException', function (): void {
// GIVEN
[$customer, $shop, $_, $tunnelService] = makePickSlotEnvironment();
$blockedException = PickupException::factory()->create([
'date' => '2026-11-20',
'label' => 'Fermeture exceptionnelle',
]);
$shop->pickupExceptions()->attach($blockedException->id, [
'replacement_date' => null,
'order_deadline_at' => null,
]);
$action = new ApplyPickTimeSlot($tunnelService);
$targetFriday = CarbonImmutable::parse('2026-11-20', 'Europe/Paris');
// WHEN
$output = $action(new ApplyPickTimeSlotInput($customer->getId(), $targetFriday));
// THEN
expect($output->status)->toBe(ApplyPickTimeSlotStatus::UNAVAILABLE)
->and($output->timeSlot)->toBeNull();
});
});