117 lines
4 KiB
PHP
117 lines
4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Actions\Orders\InitializeOrderTunnelInput;
|
|
use App\Http\Requests\Fo\Orders\ValidateSummaryRequest;
|
|
use App\Models\Customer;
|
|
use Domain\Exceptions\OrderTunnel\OrderLimitExceededException;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
use Domain\Models\ValueObjects\Entities\ShopperId;
|
|
use Domain\Services\BasketService;
|
|
use Domain\Services\OrderTunnelService;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('ValidateSummaryRequest', function () {
|
|
it('authorizes when user is logged in', function () {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('check')->once()->andReturn(true);
|
|
|
|
$request = new ValidateSummaryRequest(
|
|
$authService,
|
|
\Mockery::mock(BasketService::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 ValidateSummaryRequest(
|
|
$authService,
|
|
\Mockery::mock(BasketService::class),
|
|
\Mockery::mock(OrderTunnelService::class),
|
|
);
|
|
|
|
// WHEN
|
|
$request->initialize();
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeFalse();
|
|
});
|
|
|
|
it('fails basketTotal rule when order limit is exceeded', function () {
|
|
// GIVEN
|
|
$basket = new Basket(new BasketId(Uuid::uuid7()), []);
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$basketService = \Mockery::mock(BasketService::class);
|
|
$basketService->shouldReceive('getBasket')->once()->andReturn($basket);
|
|
|
|
$orderTunnelService = \Mockery::mock(OrderTunnelService::class);
|
|
$orderTunnelService->shouldReceive('assertBasketAmountWithinLimit')
|
|
->once()
|
|
->with($basket)
|
|
->andThrow(new OrderLimitExceededException());
|
|
|
|
$request = new ValidateSummaryRequest(
|
|
$authService,
|
|
$basketService,
|
|
$orderTunnelService,
|
|
);
|
|
$request->initialize();
|
|
|
|
$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 have reached the maximum order limit for this basket.'));
|
|
});
|
|
|
|
it('maps to InitializeOrderTunnelInput', 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()), []);
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('getCustomerAsMandatory')->once()->andReturn($customer);
|
|
|
|
$basketService = \Mockery::mock(BasketService::class);
|
|
$basketService->shouldReceive('getBasket')->once()->andReturn($basket);
|
|
|
|
$request = new ValidateSummaryRequest(
|
|
$authService,
|
|
$basketService,
|
|
\Mockery::mock(OrderTunnelService::class),
|
|
);
|
|
$request->initialize();
|
|
|
|
// WHEN
|
|
$input = $request->toInitializeOrderTunnelInput();
|
|
|
|
// THEN
|
|
expect($input)->toBeInstanceOf(InitializeOrderTunnelInput::class)
|
|
->and($input->basket)->toBe($basket)
|
|
->and($input->shopperId)->toBe($shopperId);
|
|
});
|
|
});
|