ldc-cocotte/tests/Feature/App/Http/Requests/Fo/Orders/PickTimeSlotRequestTest.php

188 lines
6.7 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Requests\Fo\Orders;
2026-06-14 21:32:07 +02:00
use App\Http\Requests\Fo\Orders\PickTimeSlotRequest;
use App\Models\Customer;
use Carbon\CarbonImmutable;
2026-06-14 21:32:07 +02:00
use Carbon\CarbonInterface;
use Domain\Builders\OrderBuilderWithBasket;
2026-06-14 21:32:07 +02:00
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
use Domain\Models\Entities\Basket;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\BasketId;
use Domain\Models\ValueObjects\Entities\ShopperId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
2026-06-14 21:32:07 +02:00
use Domain\Ports\Repositories\ShopperRepository;
use Domain\Services\OrderTunnelService;
use Illuminate\Support\Facades\Validator;
2026-06-14 21:32:07 +02:00
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Shared\Helpers\UuidHelper;
2026-06-14 21:32:07 +02:00
const VALID_TIMESLOT_DATE = '2023-10-15';
describe('PickTimeSlotRequest', function (): void {
it('authorizes only when customer auth service returns true', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->twice()->andReturn(true, false);
2026-06-14 21:32:07 +02:00
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize();
// WHEN
$authorized = $request->authorize();
$denied = $request->authorize();
2026-06-14 21:32:07 +02:00
// THEN
expect($authorized)->toBeTrue()
->and($denied)->toBeFalse();
2026-06-14 21:32:07 +02:00
});
it('gets timeslot successfully', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize(
request: ['timeslot' => VALID_TIMESLOT_DATE],
server: ['REQUEST_METHOD' => 'POST'],
);
2026-06-14 21:32:07 +02:00
// WHEN
$timeslot = $request->getTimeslot();
// THEN
expect($timeslot->format('Y-m-d H:i:s'))->toBe(VALID_TIMESLOT_DATE . ' 00:00:00');
2026-06-14 21:32:07 +02:00
});
it('throws exception on invalid timeslot format', function (): void {
// GIVEN
2026-06-14 21:32:07 +02:00
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize(
request: ['timeslot' => 'invalid-date'],
server: ['REQUEST_METHOD' => 'POST'],
);
2026-06-14 21:32:07 +02:00
// WHEN & THEN
2026-06-14 21:32:07 +02:00
$request->getTimeslot();
})->throws(\InvalidArgumentException::class);
it('fails basketTotal validation when weekly limit is exceeded', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$shopperId = new ShopperId(UuidHelper::v7());
$customer = new Customer();
$customer->id = $shopperId->value->toString();
2026-06-14 21:32:07 +02:00
$basket = new Basket(new BasketId(UuidHelper::v7()), []);
2026-06-14 21:32:07 +02:00
$shopper = new Shopper(
$shopperId,
new Email('shopper@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
CarbonImmutable::parse('2023-10-01 10:00:00'),
2026-06-14 21:32:07 +02:00
null,
new ShopperEstablishmentCode('FAC01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101'),
2026-06-14 21:32:07 +02:00
);
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('getCustomerAsMandatory')->once()->andReturn($customer);
$builder = \Mockery::mock(OrderBuilderWithBasket::class);
$builder->shouldReceive('getBasket')->once()->andReturn($basket);
2026-06-14 21:32:07 +02:00
$shopperRepository = \Mockery::mock(ShopperRepository::class);
$shopperRepository->shouldReceive('getByIdOrFail')
->once()
->withArgs(fn (ShopperId $id): bool => $id->value->toString() === $shopperId->value->toString())
->andReturn($shopper);
2026-06-14 21:32:07 +02:00
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$orderTunnelService->shouldReceive('getBuilderFromBasketOrFail')->once()->andReturn($builder);
2026-06-14 21:32:07 +02:00
$orderTunnelService->shouldReceive('assertBasketWeeklyAmountWithinLimitFor')
->once()
->withArgs(function (
Shopper $receivedShopper,
Basket $receivedBasket,
CarbonInterface $receivedDate,
) use (
$shopper,
$basket,
): bool {
return $receivedShopper === $shopper
&& $receivedBasket === $basket
2026-06-14 21:32:07 +02:00
&& $receivedDate->format('Y-m-d') === VALID_TIMESLOT_DATE;
})
->andThrow(new WeeklyLimitExceededException());
$request = new PickTimeSlotRequest(
$authService,
$shopperRepository,
$orderTunnelService,
);
$request->initialize(
request: ['timeslot' => VALID_TIMESLOT_DATE],
server: ['REQUEST_METHOD' => 'POST'],
);
2026-06-14 21:32:07 +02:00
// WHEN
$validator = Validator::make(
['timeslot' => VALID_TIMESLOT_DATE, 'basketTotal' => 1000],
$request->rules(),
);
2026-06-14 21:32:07 +02:00
// THEN
expect($validator->fails())->toBeTrue()
->and($validator->errors()->first('basketTotal'))
2026-06-14 21:32:07 +02:00
->toBe(trans('You will reach the maximum weekly order limit with this basket.'));
});
it('maps to ApplyPickTimeSlotInput', function (): void {
// GIVEN
$shopperId = new ShopperId(UuidHelper::v7());
$customer = new Customer();
$customer->id = $shopperId->value->toString();
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('getCustomerAsMandatory')->once()->andReturn($customer);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize(
request: ['timeslot' => VALID_TIMESLOT_DATE],
server: ['REQUEST_METHOD' => 'POST'],
);
// WHEN
$input = $request->toApplyPickTimeSlotInput();
// THEN
expect($input->shopperId->value->toString())->toBe($shopperId->value->toString())
->and($input->timeslot->format('Y-m-d'))->toBe(VALID_TIMESLOT_DATE);
});
2026-06-14 21:32:07 +02:00
});