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