test(casts): add tests for ShopperEstablishmentCodeCast functionality test(casts): add tests for ShopperServiceCodeCast functionality test(casts): add tests for ShopperSiteCodeCast functionality test(dto): update ProductDtoTest to include additional fields test(dto): update SettingDtoTest to remove arcadiaSites assertions test(events): update AnOrderHasBeenPlacedTest to use value objects test(entities): update OrderTest to use value objects for employee details test(value-objects): update EmployeeUpsertDataTest to use value objects test(pagination): update PaginationTest to access value properties test(services): update AppCustomerAuthServiceTest to use CrmHashcode test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
227 lines
8 KiB
PHP
227 lines
8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Actions\Orders\ApplyPickTimeSlotInput;
|
|
use App\Http\Requests\Fo\Orders\PickTimeSlotRequest;
|
|
use App\Models\Customer;
|
|
use Carbon\CarbonInterface;
|
|
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\BasketService;
|
|
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(BasketService::class),
|
|
\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(BasketService::class),
|
|
\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(BasketService::class),
|
|
\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(BasketService::class),
|
|
\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(BasketService::class),
|
|
\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(BasketService::class),
|
|
\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);
|
|
|
|
$basketService = \Mockery::mock(BasketService::class);
|
|
$basketService->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('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,
|
|
$basketService,
|
|
$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(BasketService::class),
|
|
\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);
|
|
});
|
|
});
|