ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/TunnelControllerTest.php

990 lines
36 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Fo;
use App\Contracts\Executor;
use App\Cqs\Actions\Orders\ApplyPickTimeSlotOutput;
use App\Cqs\Actions\Orders\CancelPendingOrder;
use App\Cqs\Actions\Orders\CancelPendingOrderInput;
use App\Cqs\Actions\Orders\CancelPendingOrderOutput;
use App\Cqs\Actions\Orders\ConfirmPlacedOrder;
use App\Cqs\Actions\Orders\ConfirmPlacedOrderInput;
use App\Cqs\Actions\Orders\ConfirmPlacedOrderOutput;
use App\Cqs\Actions\Orders\ExportOrderToRegate;
use App\Cqs\Actions\Orders\ExportOrderToRegateInput;
use App\Cqs\Actions\Orders\ExportOrderToRegateOutput;
use App\Cqs\Actions\Orders\PlacePendingOrder;
use App\Cqs\Actions\Orders\PlacePendingOrderInput;
use App\Cqs\Actions\Orders\PlacePendingOrderOutput;
use App\Cqs\Queries\Orders\GetBasketSummaryQueryOutput;
use App\Cqs\Queries\Orders\GetOrderConfirmationQueryOutput;
use App\Cqs\Queries\Orders\GetPickTimeSlotQueryOutput;
use App\Dto\ShopDto;
use App\Http\Middleware\CustomerIsEmployed;
use App\Http\Middleware\EnsureCustomerEmailIsVerified;
use App\Models\Customer;
use App\Models\Order;
use App\Models\Page;
use App\Models\Shop;
use Database\Seeders\ContentSeeder;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Builders\OrderBuilderWithBasket;
use Domain\Enums\OrderStatus;
use Domain\Enums\OrderTunnelStep;
use Domain\Exceptions\OrderTunnel\RegateExportFailedException;
use Domain\Models\Entities\Basket;
use Domain\Models\Entities\Shopper;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Entities\BasketId;
use Domain\Models\ValueObjects\Entities\OrderId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\PickupTimeSlot;
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\Cache;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\View;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Sentry\State\HubInterface;
use Shared\ValueObjects\CrmHashcode;
describe('TunnelController', function (): void {
it('redirects unauthenticated guest accessing basket summary to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/commande/panier');
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest submitting basket summary to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/commande/panier', []);
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest accessing pick time slot to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/commande/creneaux');
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest submitting pick time slot to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/commande/creneaux', []);
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest accessing confirmation to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/commande/confirmation');
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest submitting confirmation to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->post('/commande/confirmation', []);
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('redirects unauthenticated guest accessing confirmed order to login', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
// WHEN
$response = $this->get('/commande/confirmation/1');
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
});
it('displays basket summary when customer is authorized', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$basket = new Basket(new BasketId(Uuid::uuid7()));
$this->mock(Executor::class, function ($mock) use ($basket): void {
$mock->shouldReceive('execute')
->once()
->andReturn(new GetBasketSummaryQueryOutput(
step: OrderTunnelStep::REVIEW,
basket: $basket,
));
});
// WHEN
$response = $this->get('/commande/panier');
// THEN
$response->assertOk()
->assertViewIs('fo.tunnel.basket-summary')
->assertViewHas('step', OrderTunnelStep::REVIEW)
->assertViewHas('basket', $basket);
});
it('submits basket summary and redirects to pick time slot', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once();
});
// WHEN
$response = $this->post('/commande/panier', []);
// THEN
$response->assertRedirectToRoute('fo.sales.pick-time-slot');
});
it('displays pick time slot when customer is authorized', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$shopDto = new ShopDto(
id: 'test-shop',
name: 'Test Shop',
address: '1 rue Test',
postcode: '75001',
city: 'Paris',
phone: null,
latitude: null,
longitude: null,
default: true,
);
$this->mock(Executor::class, function ($mock) use ($shopDto): void {
$mock->shouldReceive('execute')
->once()
->andReturn(new GetPickTimeSlotQueryOutput(
step: OrderTunnelStep::BOOKING,
shop: $shopDto,
timeSlots: [],
mapUrl: 'https://maps.example.com',
));
});
// WHEN
$response = $this->get('/commande/creneaux');
// THEN
$response->assertOk()
->assertViewIs('fo.tunnel.pick-time-slot')
->assertViewHas('step', OrderTunnelStep::BOOKING)
->assertViewHas('shop', $shopDto)
->assertViewHas('mapUrl', 'https://maps.example.com');
});
it('submits pick time slot and redirects to confirmation when slot is valid', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$shopper = new Shopper(
id: $customer->getId(),
email: new Email('test@example.com'),
matricule: new Matricule('1234567'),
nom: 'Dupont',
prenom: 'Jean',
startDate: now()->subMonth(),
endDate: null,
etablissement: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$this->mock(ShopperRepository::class, function ($mock) use ($shopper): void {
$mock->shouldReceive('getByIdOrFail')->andReturn($shopper);
});
$basket = new Basket(new BasketId(Uuid::uuid7()));
$builder = \Mockery::mock(OrderBuilderWithBasket::class);
$builder->shouldReceive('getBasket')->andReturn($basket);
$this->mock(OrderTunnelService::class, function ($mock) use ($builder): void {
$mock->shouldReceive('getBuilderFromBasketOrFail')->andReturn($builder);
$mock->shouldReceive('assertBasketWeeklyAmountWithinLimitFor');
});
$slot = new PickupTimeSlot(now(), new \DateInterval('PT1H'));
$this->mock(Executor::class, function ($mock) use ($slot): void {
$mock->shouldReceive('execute')
->once()
->andReturn(ApplyPickTimeSlotOutput::matched($slot));
});
// WHEN
$response = $this->post('/commande/creneaux', [
'timeslot' => now()->format('Y-m-d'),
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation');
});
it('submits pick time slot and redirects back with error when slot is invalid', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$shopper = new Shopper(
id: $customer->getId(),
email: new Email('test@example.com'),
matricule: new Matricule('1234567'),
nom: 'Dupont',
prenom: 'Jean',
startDate: now()->subMonth(),
endDate: null,
etablissement: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$this->mock(ShopperRepository::class, function ($mock) use ($shopper): void {
$mock->shouldReceive('getByIdOrFail')->andReturn($shopper);
});
$basket = new Basket(new BasketId(Uuid::uuid7()));
$builder = \Mockery::mock(OrderBuilderWithBasket::class);
$builder->shouldReceive('getBasket')->andReturn($basket);
$this->mock(OrderTunnelService::class, function ($mock) use ($builder): void {
$mock->shouldReceive('getBuilderFromBasketOrFail')->andReturn($builder);
$mock->shouldReceive('assertBasketWeeklyAmountWithinLimitFor');
});
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(ApplyPickTimeSlotOutput::unavailable());
});
// WHEN
$response = $this->from('/commande/creneaux')->post('/commande/creneaux', [
'timeslot' => now()->format('Y-m-d'),
]);
// THEN
$response->assertRedirect('/commande/creneaux')
->assertSessionHasErrors('timeslot');
});
it('displays order confirmation when customer is authorized', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$basket = new Basket(new BasketId(Uuid::uuid7()));
$builder = \Mockery::mock(OrderBuilderCompleted::class);
$slot = new PickupTimeSlot(now(), new \DateInterval('PT1H'));
$builder->shouldReceive('getPickupTimeSlot')->andReturn($slot);
$shopDto = new ShopDto(
id: 'test-shop',
name: 'Test Shop',
address: '1 rue Test',
postcode: '75001',
city: 'Paris',
phone: null,
latitude: null,
longitude: null,
default: true,
);
$this->mock(Executor::class, function ($mock) use ($basket, $builder, $shopDto): void {
$mock->shouldReceive('execute')
->once()
->andReturn(new GetOrderConfirmationQueryOutput(
step: OrderTunnelStep::CONFIRMATION,
basket: $basket,
builder: $builder,
shop: $shopDto,
));
});
// WHEN
$response = $this->get('/commande/confirmation');
// THEN
$response->assertOk()
->assertViewIs('fo.tunnel.confirmation')
->assertViewHas('step', OrderTunnelStep::CONFIRMATION)
->assertViewHas('basket', $basket)
->assertViewHas('shop', $shopDto);
});
it('submits confirmation and redirects to confirmed view when cgv accepted', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$orderId = new OrderId(Uuid::uuid7());
$orderReference = new OrderReference(Uuid::uuid7());
$this->mock(Executor::class, function ($mock) use ($orderId, $orderReference): void {
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(PlacePendingOrder::class), \Mockery::type(PlacePendingOrderInput::class))
->andReturn(PlacePendingOrderOutput::pendingCreated($orderId, $orderReference));
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(ExportOrderToRegate::class), \Mockery::type(ExportOrderToRegateInput::class))
->andReturn(ExportOrderToRegateOutput::SENT);
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(ConfirmPlacedOrder::class), \Mockery::type(ConfirmPlacedOrderInput::class))
->andReturn(ConfirmPlacedOrderOutput::CONFIRMED);
});
// WHEN
$response = $this->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation.show-confirmed', (string) $orderReference);
});
it('cancels pending order and redirects back cleanly when Regate export fails on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('regate-export-fail@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$orderId = new OrderId(Uuid::uuid7());
$orderReference = new OrderReference(Uuid::uuid7());
$this->mock(Executor::class, function ($mock) use ($orderId, $orderReference): void {
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(PlacePendingOrder::class), \Mockery::type(PlacePendingOrderInput::class))
->andReturn(PlacePendingOrderOutput::pendingCreated($orderId, $orderReference));
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(ExportOrderToRegate::class), \Mockery::type(ExportOrderToRegateInput::class))
->andReturn(ExportOrderToRegateOutput::FAILED);
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(CancelPendingOrder::class), \Mockery::type(CancelPendingOrderInput::class))
->andReturn(CancelPendingOrderOutput::CANCELLED);
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation')
->assertSessionHas('message', __('An error occurred while validating your order. Please try again later.'));
});
it('redirects to confirmation with error when ConfirmPlacedOrder fails after Regate export', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('confirm-sql-fail@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$orderId = new OrderId(Uuid::uuid7());
$orderReference = new OrderReference(Uuid::uuid7());
$this->mock(Executor::class, function ($mock) use ($orderId, $orderReference): void {
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(PlacePendingOrder::class), \Mockery::type(PlacePendingOrderInput::class))
->andReturn(PlacePendingOrderOutput::pendingCreated($orderId, $orderReference));
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(ExportOrderToRegate::class), \Mockery::type(ExportOrderToRegateInput::class))
->andReturn(ExportOrderToRegateOutput::SENT);
$mock->shouldReceive('execute')
->once()
->with(\Mockery::type(ConfirmPlacedOrder::class), \Mockery::type(ConfirmPlacedOrderInput::class))
->andReturn(ConfirmPlacedOrderOutput::FAILED);
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation')
->assertSessionHas('message', __('An error occurred while validating your order. Please try again later.'));
});
it('redirects back with message when limit exception occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::weeklyLimitExceeded(
'The total amount for this delivery date has been exceeded. Please modify it to validate.'
));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirect('/commande/confirmation')
->assertSessionHas('message');
});
it('redirects to pick time slot with errors when ExpiredPickupTimeSlotException occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::expiredTimeSlot('Invalid or unavailable time slot'));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.pick-time-slot')
->assertSessionHasErrors([
'timeslot' => trans('Invalid or unavailable time slot'),
]);
});
it('redirects to basket summary with errors when UnorderableProductException occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::unorderableProduct(
'One or more products in your basket are no longer available for order.'
));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.summary')
->assertSessionHasErrors([
'basket' => trans('One or more products in your basket are no longer available for order.'),
]);
});
it('redirects to basket summary with errors when BasketPriceChangedException occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::basketPriceChanged(
'The price of one or more products in your basket has changed.'
));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.summary')
->assertSessionHasErrors([
'basket' => trans('The price of one or more products in your basket has changed.'),
]);
});
it('redirects back with message when LockTimeout occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::lockTimeout());
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirect('/commande/confirmation')
->assertSessionHas('message', __('Your order is already being processed. Please wait a moment and try again.'));
});
it('logs out and redirects home with blocked message when inactiveShopper output occurs on confirmation submit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test-inactive@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(PlacePendingOrderOutput::inactiveShopper('Employee account is no longer active.'));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.pages.home')
->assertSessionHasErrors();
expect(auth('frontend')->check())->toBeFalse();
});
it('displays confirmed order view when customer is authorized', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('test@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$shop = new Shop();
$shop->name = 'Boutique Test';
$shop->address = '1 rue Test';
$shop->postcode = '75001';
$shop->city = 'Paris';
$shop->default = true;
$shop->save();
$orderReference = new OrderReference(Uuid::uuid7());
$order = new Order();
$order->reference = $orderReference;
$order->total = Money::EUR(3000);
$order->status = OrderStatus::WAITING;
$order->pickup_day = now()->addDays(2);
$order->shop_id = $shop->id;
$order->employee = new OrderEmployee(
firstname: 'Jean',
lastname: 'Dupont',
matricule: new Matricule('1234567'),
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101'),
);
$order->delivery_address = new DeliveryAddress(
name: $shop->name,
address: $shop->address,
postcode: $shop->postcode,
city: $shop->city,
);
$order->save();
// WHEN
$response = $this->get('/commande/confirmation/' . (string) $orderReference);
// THEN
$response->assertOk()
->assertViewIs('fo.tunnel.confirmed')
->assertViewHas('step', OrderTunnelStep::CONFIRMATION)
->assertViewHas('order')
->assertViewHas('shop')
->assertViewHas('mapUrl');
});
it('reports RegateExportFailedException to Sentry and redirects cleanly to confirmation instead of 500', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$customer = new Customer();
$customer->email = new Email('regate-fail@example.com');
$customer->hashcode = new CrmHashcode(Uuid::uuid4());
$customer->save();
$this->actingAs($customer, 'frontend');
$this->withoutMiddleware([
CustomerIsEmployed::class,
EnsureCustomerEmailIsVerified::class,
]);
Gate::before(fn () => true);
$sentryHub = \Mockery::mock(HubInterface::class);
$sentryHub->shouldReceive('captureException')
->once()
->with(\Mockery::type(RegateExportFailedException::class));
$this->app->instance(HubInterface::class, $sentryHub);
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andThrow(new RegateExportFailedException('SMTP transport down'));
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation')
->assertSessionHas('message', __('An error occurred while validating your order. Please try again later.'));
});
});