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

610 lines
20 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Contracts\Executor;
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;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
use App\Models\Shop;
2026-06-14 21:32:07 +02:00
use Database\Seeders\ContentSeeder;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Enums\OrderStatus;
use Domain\Enums\OrderTunnelStep;
use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException;
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\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;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\View;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Shared\ValueObjects\CrmHashcode;
2026-06-14 21:32:07 +02:00
describe('TunnelController', function (): void {
it('redirects unauthenticated guest accessing basket summary to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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');
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest submitting basket summary to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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', []);
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest accessing pick time slot to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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');
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest submitting pick time slot to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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', []);
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest accessing confirmation to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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');
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest submitting confirmation to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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', []);
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
it('redirects unauthenticated guest accessing confirmed order to login', function (): void {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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');
2026-06-14 21:32:07 +02:00
// THEN
$response->assertRedirectToRoute('fo.auth.connexion');
2026-06-14 21:32:07 +02:00
});
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);
});
$this->mock(OrderTunnelService::class, function ($mock): void {
$mock->shouldReceive('assertBasketWeeklyAmountWithinLimitFor');
});
$slot = new PickupTimeSlot(now(), new \DateInterval('PT1H'));
$this->mock(Executor::class, function ($mock) use ($slot): void {
$mock->shouldReceive('execute')
->once()
->andReturn($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);
});
$this->mock(OrderTunnelService::class, function ($mock): void {
$mock->shouldReceive('assertBasketWeeklyAmountWithinLimitFor');
});
$this->mock(Executor::class, function ($mock): void {
$mock->shouldReceive('execute')
->once()
->andReturn(null);
});
// 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);
$orderReference = new OrderReference(Uuid::uuid7());
$this->mock(Executor::class, function ($mock) use ($orderReference): void {
$mock->shouldReceive('execute')
->once()
->andReturn($orderReference);
});
// WHEN
$response = $this->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirectToRoute('fo.sales.confirmation.show-confirmed', (string) $orderReference);
});
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()
->andThrow(new WeeklyLimitExceededException());
});
// WHEN
$response = $this->from('/commande/confirmation')->post('/commande/confirmation', [
'ok_cgv' => '1',
]);
// THEN
$response->assertRedirect('/commande/confirmation')
->assertSessionHas('message');
});
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');
});
2026-06-14 21:32:07 +02:00
});