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

222 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Requests\Fo\Orders;
use App\Cqs\Actions\Orders\ApplyPickTimeSlotInput;
use App\Http\Requests\Fo\Orders\PickTimeSlotRequest;
use App\Models\Customer;
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 Infrastructure\Adapters\Services\AppCustomerAuthService;
use Ramsey\Uuid\Uuid;
const VALID_TIMESLOT_DATE = '2023-10-15';
describe('PickTimeSlotRequest', function () {
it('authorizes when user is logged in', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->once()->andReturn(true);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
// WHEN
$request->initialize();
// THEN
expect($request->authorize())->toBeTrue();
});
it('does not authorize when user is not logged in', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->once()->andReturn(false);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
// WHEN
$request->initialize();
// THEN
expect($request->authorize())->toBeFalse();
});
it('gets timeslot successfully', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize();
$request->merge(['timeslot' => VALID_TIMESLOT_DATE]);
// WHEN
$timeslot = $request->getTimeslot();
// THEN
expect($timeslot)->toBeInstanceOf(CarbonInterface::class)
->and($timeslot->format('Y-m-d H:i:s'))->toBe(VALID_TIMESLOT_DATE . ' 00:00:00');
});
it('throws exception on invalid timeslot format', function () {
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
$request->initialize();
$request->merge(['timeslot' => 'invalid-date']);
$request->getTimeslot();
})->throws(\InvalidArgumentException::class);
it('validates rules', function () {
$authService = \Mockery::mock(AppCustomerAuthService::class);
// GIVEN
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
// WHEN
$request->initialize();
// THEN
expect($request->rules())->toBeArray()
->and($request->rules())->toHaveKeys(['timeslot']);
});
it('returns custom attributes', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new PickTimeSlotRequest(
$authService,
\Mockery::mock(ShopperRepository::class),
\Mockery::mock(OrderTunnelService::class),
);
// WHEN
$request->initialize();
// THEN
expect($request->attributes())->toBeArray()
->and($request->attributes())->toHaveKeys(['timeslot']);
});
it('fails basketTotal rule when weekly limit is exceeded', function () {
// GIVEN
$shopperId = new ShopperId(Uuid::uuid7());
$customer = \Mockery::mock(Customer::class);
$customer->shouldReceive('getId')->once()->andReturn($shopperId);
$basket = new Basket(new BasketId(Uuid::uuid7()), []);
$shopper = new Shopper(
$shopperId,
new Email('shopper@example.com'),
new Matricule('1234567'),
'Nom',
'Prenom',
now(),
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()->with($shopperId)->andReturn($shopper);
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
$orderTunnelService->shouldReceive('getBuilderFromBasketOrFail')->once()->andReturn($builder);
$orderTunnelService->shouldReceive('assertBasketWeeklyAmountWithinLimitFor')
->once()
->withArgs(function ($receivedShopper, $receivedBasket, $receivedDate) use ($shopper, $basket) {
return $receivedShopper === $shopper
&& $receivedBasket === $basket
&& $receivedDate instanceof CarbonInterface
&& $receivedDate->format('Y-m-d') === VALID_TIMESLOT_DATE;
})
->andThrow(new WeeklyLimitExceededException());
$request = new PickTimeSlotRequest(
$authService,
$shopperRepository,
$orderTunnelService,
);
$request->initialize();
$request->merge(['timeslot' => VALID_TIMESLOT_DATE]);
$rules = $request->rules();
$basketTotalFn = $rules['basketTotal'][0];
$failMessages = [];
// WHEN
$basketTotalFn('basketTotal', 1000, function (string $message) use (&$failMessages): void {
$failMessages[] = $message;
});
// THEN
expect($failMessages)->toHaveCount(1)
->and($failMessages[0])
->toBe(trans('You will reach the maximum weekly order limit with this basket.'));
});
it('maps to ApplyPickTimeSlotInput', function () {
// GIVEN
$shopperId = new ShopperId(Uuid::uuid7());
$customer = \Mockery::mock(Customer::class);
$customer->shouldReceive('getId')->once()->andReturn($shopperId);
$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->merge(['timeslot' => VALID_TIMESLOT_DATE]);
// WHEN
$input = $request->toApplyPickTimeSlotInput();
// THEN
expect($input)->toBeInstanceOf(ApplyPickTimeSlotInput::class)
->and($input->shopperId)->toBe($shopperId)
->and($input->timeslot->format('Y-m-d'))->toBe(VALID_TIMESLOT_DATE);
});
});