diff --git a/app/Actions/Orders/ApplyPickTimeSlot.php b/app/Actions/Orders/ApplyPickTimeSlot.php new file mode 100644 index 00000000..fb96685a --- /dev/null +++ b/app/Actions/Orders/ApplyPickTimeSlot.php @@ -0,0 +1,42 @@ + + */ +final class ApplyPickTimeSlot implements Action +{ + public function __construct( + private readonly OrderTunnelService $orderTunnelService, + ) { + } + + /** + * @param \App\Actions\Orders\ApplyPickTimeSlotInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): ?PickupTimeSlot + { + Assert::isInstanceOf($input, ApplyPickTimeSlotInput::class); + + $builder = $this->orderTunnelService->getBuilderFromBasketOrFail(); + $shop = $this->orderTunnelService->getShopForCustomer($input->shopperId); + $timeSlots = $this->orderTunnelService->getAvailableTimeSlots($shop); + + return $this->orderTunnelService->applyTimeSlot( + $builder, + $shop, + $input->timeslot, + $timeSlots + ); + } +} diff --git a/app/Actions/Orders/ApplyPickTimeSlotInput.php b/app/Actions/Orders/ApplyPickTimeSlotInput.php new file mode 100644 index 00000000..351f4662 --- /dev/null +++ b/app/Actions/Orders/ApplyPickTimeSlotInput.php @@ -0,0 +1,18 @@ + + */ +final class FinalizeOrder implements Action +{ + public function __construct( + private readonly OrderTunnelService $orderTunnelService, + ) { + } + + /** + * @param \App\Actions\Orders\FinalizeOrderInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): OrderReference + { + Assert::isInstanceOf($input, FinalizeOrderInput::class); + + $builder = $this->orderTunnelService->getBuilderCompletedOrFail(); + $order = $this->orderTunnelService->finalizeOrder($builder); + + return $order->reference; + } +} diff --git a/app/Actions/Orders/FinalizeOrderInput.php b/app/Actions/Orders/FinalizeOrderInput.php new file mode 100644 index 00000000..37f6ea0a --- /dev/null +++ b/app/Actions/Orders/FinalizeOrderInput.php @@ -0,0 +1,16 @@ + + */ +final class InitializeOrderTunnel implements Action +{ + public function __construct( + private readonly OrderTunnelService $orderTunnelService, + ) { + } + + /** + * @param \App\Actions\Orders\InitializeOrderTunnelInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): void + { + Assert::isInstanceOf($input, InitializeOrderTunnelInput::class); + + $this->orderTunnelService->initializeFromBasket( + $input->basket, + $input->shopperId + ); + } +} diff --git a/app/Actions/Orders/InitializeOrderTunnelInput.php b/app/Actions/Orders/InitializeOrderTunnelInput.php new file mode 100644 index 00000000..3bd31b09 --- /dev/null +++ b/app/Actions/Orders/InitializeOrderTunnelInput.php @@ -0,0 +1,18 @@ +id->value->toString(), + name: $shop->name, + address: $shop->address, + postcode: $shop->postcode, + city: $shop->city, + phone: $shop->phone, + latitude: $shop->latitude, + longitude: $shop->longitude, + default: $shop->isDefault, + ); + } + #[\Override] public function getRouteKey(): mixed { diff --git a/app/Http/Controllers/Fo/TunnelController.php b/app/Http/Controllers/Fo/TunnelController.php index 2e4ca5f8..68f29a50 100644 --- a/app/Http/Controllers/Fo/TunnelController.php +++ b/app/Http/Controllers/Fo/TunnelController.php @@ -4,58 +4,64 @@ declare(strict_types=1); namespace App\Http\Controllers\Fo; +use App\Actions\Orders\ApplyPickTimeSlot; +use App\Actions\Orders\FinalizeOrder; +use App\Actions\Orders\InitializeOrderTunnel; +use App\Contracts\Executor; use App\Http\Controllers\Controller; +use App\Http\Requests\Fo\Orders\ConfirmOrderRequest; use App\Http\Requests\Fo\Orders\PickTimeSlotRequest; use App\Http\Requests\Fo\Orders\ValidateSummaryRequest; use App\Models\Order; -use App\Services\MapUrlService; +use App\Queries\Orders\GetBasketSummaryQuery; +use App\Queries\Orders\GetBasketSummaryQueryInput; +use App\Queries\Orders\GetOrderConfirmationQuery; +use App\Queries\Orders\GetOrderConfirmationQueryInput; +use App\Queries\Orders\GetPickTimeSlotQuery; +use App\Queries\Orders\GetPickTimeSlotQueryInput; +use App\Queries\Orders\GetTunnelOrderConfirmedQuery; +use App\Queries\Orders\GetTunnelOrderConfirmedQueryInput; use Domain\Enums\OrderTunnelStep; use Domain\Exceptions\OrderTunnel\OrderLimitExceededException; use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException; -use Domain\Ports\Repositories\ShopRepository; -use Domain\Services\BasketService; -use Domain\Services\OrderTunnelService; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Infrastructure\Adapters\Services\AppCustomerAuthService; class TunnelController extends Controller { public function __construct( private readonly AppCustomerAuthService $authService, - private readonly BasketService $basketService, - private readonly OrderTunnelService $orderTunnelService, - private readonly MapUrlService $mapUrlService, - private readonly ShopRepository $shopRepository, + private readonly Executor $executor, ) { } // * -- STEP 1 -- * - public function basketSummary(BasketService $basketService): View + public function basketSummary(GetBasketSummaryQuery $query): View { $this->authorize('canReviewBasketSummary', OrderTunnelStep::class); - $step = OrderTunnelStep::REVIEW; - $basket = $basketService->getBasket(); + $output = $this->executor->execute( + $query, + new GetBasketSummaryQueryInput() + ); - return view('fo.tunnel.basket-summary', compact( - 'step', - 'basket' - )); + return view('fo.tunnel.basket-summary', [ + 'step' => $output->step, + 'basket' => $output->basket, + ]); } - public function basketSummarySubmit(ValidateSummaryRequest $request): RedirectResponse - { + public function basketSummarySubmit( + ValidateSummaryRequest $request, + InitializeOrderTunnel $action + ): RedirectResponse { $this->authorize('canReviewBasketSummary', OrderTunnelStep::class); - $customer = $this->authService->getCustomerAsMandatory(); - $basket = $this->basketService->getBasket(); - - $this->orderTunnelService->initializeFromBasket( - $basket, - $customer->getId() + $this->executor->execute( + $action, + $request->toInitializeOrderTunnelInput() ); return to_route('fo.sales.pick-time-slot'); @@ -63,40 +69,34 @@ class TunnelController extends Controller // * -- STEP 2 -- * - public function pickTimeSlot(): View - { - $this->authorize('canPickTimeSlot', OrderTunnelStep::class); - - $customer = $this->authService->getCustomerAsMandatory(); - $shop = $this->orderTunnelService->getShopForCustomer($customer->getId()); - $timeSlots = $this->orderTunnelService->getAvailableTimeSlots($shop); - $step = OrderTunnelStep::BOOKING; - - $mapUrl = $this->mapUrlService->generateUrl($shop); - - return view('fo.tunnel.pick-time-slot', compact( - 'step', - 'shop', - 'mapUrl', - 'timeSlots' - )); - } - - public function pickTimeSlotSubmit(PickTimeSlotRequest $request): RedirectResponse + public function pickTimeSlot(GetPickTimeSlotQuery $query): View { $this->authorize('canPickTimeSlot', OrderTunnelStep::class); $customer = $this->authService->getCustomerAsMandatory(); - $builder = $this->orderTunnelService->getBuilderFromBasketOrFail(); - $shop = $this->orderTunnelService->getShopForCustomer($customer->getId()); - $timeSlots = $this->orderTunnelService->getAvailableTimeSlots($shop); + $output = $this->executor->execute( + $query, + new GetPickTimeSlotQueryInput($customer->getId()) + ); - $matchedSlot = $this->orderTunnelService->applyTimeSlot( - $builder, - $shop, - $request->getTimeslot(), - $timeSlots + return view('fo.tunnel.pick-time-slot', [ + 'step' => $output->step, + 'shop' => $output->shop, + 'mapUrl' => $output->mapUrl, + 'timeSlots' => $output->timeSlots, + ]); + } + + public function pickTimeSlotSubmit( + PickTimeSlotRequest $request, + ApplyPickTimeSlot $action + ): RedirectResponse { + $this->authorize('canPickTimeSlot', OrderTunnelStep::class); + + $matchedSlot = $this->executor->execute( + $action, + $request->toApplyPickTimeSlotInput() ); if ($matchedSlot === null) { @@ -110,67 +110,60 @@ class TunnelController extends Controller // * -- STEP 3 -- * - public function confirmation(): View + public function confirmation(GetOrderConfirmationQuery $query): View { $this->authorize('canFinalizeOrder', OrderTunnelStep::class); - $step = OrderTunnelStep::CONFIRMATION; - $builder = $this->orderTunnelService->getBuilderCompletedOrFail(); - $basket = $builder->getBasket(); + $output = $this->executor->execute( + $query, + new GetOrderConfirmationQueryInput() + ); - $shop = $this->shopRepository->find($builder->getShopId()); - - if (! $shop) { - abort(404, 'Shop not found'); - } - - return view('fo.tunnel.confirmation', compact( - 'step', - 'basket', - 'builder', - 'shop' - )); + return view('fo.tunnel.confirmation', [ + 'step' => $output->step, + 'basket' => $output->basket, + 'builder' => $output->builder, + 'shop' => $output->shop, + ]); } - public function confirmationSubmit(Request $request): RedirectResponse - { + public function confirmationSubmit( + ConfirmOrderRequest $request, + FinalizeOrder $action + ): RedirectResponse { $this->authorize('canFinalizeOrder', OrderTunnelStep::class); - $request->validate([ - 'ok_cgv' => ['accepted'], - ]); - - $builder = $this->orderTunnelService->getBuilderCompletedOrFail(); - try { - $order = $this->orderTunnelService->finalizeOrder($builder); + /** @var \Domain\Models\ValueObjects\OrderReference $orderReference */ + $orderReference = $this->executor->execute( + $action, + $request->toFinalizeOrderInput() + ); } catch (WeeklyLimitExceededException | OrderLimitExceededException $e) { return back()->with('message', $e->getMessage()); } - return to_route('fo.sales.confirmation.show-confirmed', $order->reference->value); + return to_route('fo.sales.confirmation.show-confirmed', $orderReference->value); } // * -- CONFIRMATION -- * - public function confirmed(Order $order): View - { + public function confirmed( + Order $order, + GetTunnelOrderConfirmedQuery $query + ): View { $this->authorize('viewOwn', $order); - $step = OrderTunnelStep::CONFIRMATION; - $shop = $order->shop; + $output = $this->executor->execute( + $query, + new GetTunnelOrderConfirmedQueryInput($order) + ); - if (! $shop) { - throw new \UnexpectedValueException('Shop not found for order ' . $order->getId()); - } - - $mapUrl = $this->mapUrlService->generateUrl($shop); - - return view('fo.tunnel.confirmed', compact( - 'step', - 'order', - 'shop', - 'mapUrl' - )); + return view('fo.tunnel.confirmed', [ + 'step' => $output->step, + 'order' => $output->order, + 'shop' => $output->shop, + 'mapUrl' => $output->mapUrl, + ]); } } diff --git a/app/Http/Requests/Fo/Orders/ConfirmOrderRequest.php b/app/Http/Requests/Fo/Orders/ConfirmOrderRequest.php new file mode 100644 index 00000000..9399e55c --- /dev/null +++ b/app/Http/Requests/Fo/Orders/ConfirmOrderRequest.php @@ -0,0 +1,41 @@ +authService->check(); + } + + public function toFinalizeOrderInput(): FinalizeOrderInput + { + $customer = $this->authService->getCustomerAsMandatory(); + + return new FinalizeOrderInput( + shopperId: $customer->getId(), + ); + } + + /** + * @return array + */ + public function rules(): array + { + return [ + 'ok_cgv' => ['accepted'], + ]; + } +} diff --git a/app/Http/Requests/Fo/Orders/PickTimeSlotRequest.php b/app/Http/Requests/Fo/Orders/PickTimeSlotRequest.php index c45fb8b6..7799d40d 100644 --- a/app/Http/Requests/Fo/Orders/PickTimeSlotRequest.php +++ b/app/Http/Requests/Fo/Orders/PickTimeSlotRequest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Http\Requests\Fo\Orders; +use App\Actions\Orders\ApplyPickTimeSlotInput; use Carbon\CarbonImmutable; use Carbon\CarbonInterface; use Domain\Exceptions\OrderTunnel\WeeklyLimitExceededException; @@ -23,6 +24,16 @@ class PickTimeSlotRequest extends FormRequest ) { } + public function toApplyPickTimeSlotInput(): ApplyPickTimeSlotInput + { + $customer = $this->authService->getCustomerAsMandatory(); + + return new ApplyPickTimeSlotInput( + shopperId: $customer->getId(), + timeslot: $this->getTimeslot(), + ); + } + public function authorize(): bool { return $this->authService->check(); diff --git a/app/Http/Requests/Fo/Orders/ValidateSummaryRequest.php b/app/Http/Requests/Fo/Orders/ValidateSummaryRequest.php index b0c649b4..5adc0b3b 100644 --- a/app/Http/Requests/Fo/Orders/ValidateSummaryRequest.php +++ b/app/Http/Requests/Fo/Orders/ValidateSummaryRequest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Http\Requests\Fo\Orders; +use App\Actions\Orders\InitializeOrderTunnelInput; use Domain\Exceptions\OrderTunnel\OrderLimitExceededException; use Domain\Services\BasketService; use Domain\Services\OrderTunnelService; @@ -19,6 +20,17 @@ class ValidateSummaryRequest extends FormRequest ) { } + public function toInitializeOrderTunnelInput(): InitializeOrderTunnelInput + { + $customer = $this->authService->getCustomerAsMandatory(); + $basket = $this->basketService->getBasket(); + + return new InitializeOrderTunnelInput( + basket: $basket, + shopperId: $customer->getId(), + ); + } + public function authorize(): bool { return $this->authService->check(); diff --git a/app/Queries/Orders/GetBasketSummaryQuery.php b/app/Queries/Orders/GetBasketSummaryQuery.php new file mode 100644 index 00000000..373c43cf --- /dev/null +++ b/app/Queries/Orders/GetBasketSummaryQuery.php @@ -0,0 +1,36 @@ + + */ +final readonly class GetBasketSummaryQuery implements Query +{ + public function __construct( + private BasketService $basketService, + ) { + } + + /** + * @param \App\Queries\Orders\GetBasketSummaryQueryInput $input + */ + #[\Override] + public function __invoke(QueryInput $input): GetBasketSummaryQueryOutput + { + Assert::isInstanceOf($input, GetBasketSummaryQueryInput::class); + + return new GetBasketSummaryQueryOutput( + step: OrderTunnelStep::REVIEW, + basket: $this->basketService->getBasket(), + ); + } +} diff --git a/app/Queries/Orders/GetBasketSummaryQueryInput.php b/app/Queries/Orders/GetBasketSummaryQueryInput.php new file mode 100644 index 00000000..6c2e4c51 --- /dev/null +++ b/app/Queries/Orders/GetBasketSummaryQueryInput.php @@ -0,0 +1,14 @@ + + */ +final readonly class GetOrderConfirmationQuery implements Query +{ + public function __construct( + private OrderTunnelService $orderTunnelService, + private ShopRepository $shopRepository, + ) { + } + + /** + * @param \App\Queries\Orders\GetOrderConfirmationQueryInput $input + */ + #[\Override] + public function __invoke(QueryInput $input): GetOrderConfirmationQueryOutput + { + Assert::isInstanceOf($input, GetOrderConfirmationQueryInput::class); + + $builder = $this->orderTunnelService->getBuilderCompletedOrFail(); + $basket = $builder->getBasket(); + + $shop = $this->shopRepository->find($builder->getShopId()); + + if (! $shop) { + throw new NotFoundHttpException('Shop not found'); + } + + $shopDto = ShopDto::fromDomain($shop); + + return new GetOrderConfirmationQueryOutput( + step: OrderTunnelStep::CONFIRMATION, + basket: $basket, + builder: $builder, + shop: $shopDto, + ); + } +} diff --git a/app/Queries/Orders/GetOrderConfirmationQueryInput.php b/app/Queries/Orders/GetOrderConfirmationQueryInput.php new file mode 100644 index 00000000..b2585d9e --- /dev/null +++ b/app/Queries/Orders/GetOrderConfirmationQueryInput.php @@ -0,0 +1,14 @@ + + */ +final readonly class GetPickTimeSlotQuery implements Query +{ + public function __construct( + private OrderTunnelService $orderTunnelService, + private MapUrlService $mapUrlService, + ) { + } + + /** + * @param \App\Queries\Orders\GetPickTimeSlotQueryInput $input + */ + #[\Override] + public function __invoke(QueryInput $input): GetPickTimeSlotQueryOutput + { + Assert::isInstanceOf($input, GetPickTimeSlotQueryInput::class); + + $shop = $this->orderTunnelService->getShopForCustomer($input->shopperId); + $timeSlots = $this->orderTunnelService->getAvailableTimeSlots($shop); + $shopDto = ShopDto::fromDomain($shop); + $mapUrl = $this->mapUrlService->generateUrl($shopDto); + + return new GetPickTimeSlotQueryOutput( + step: OrderTunnelStep::BOOKING, + shop: $shopDto, + timeSlots: $timeSlots, + mapUrl: $mapUrl, + ); + } +} diff --git a/app/Queries/Orders/GetPickTimeSlotQueryInput.php b/app/Queries/Orders/GetPickTimeSlotQueryInput.php new file mode 100644 index 00000000..3266d40e --- /dev/null +++ b/app/Queries/Orders/GetPickTimeSlotQueryInput.php @@ -0,0 +1,16 @@ + $timeSlots + */ + public function __construct( + public OrderTunnelStep $step, + public ShopDto $shop, + public array $timeSlots, + public string $mapUrl, + ) { + } +} diff --git a/app/Queries/Orders/GetTunnelOrderConfirmedQuery.php b/app/Queries/Orders/GetTunnelOrderConfirmedQuery.php new file mode 100644 index 00000000..cec60aca --- /dev/null +++ b/app/Queries/Orders/GetTunnelOrderConfirmedQuery.php @@ -0,0 +1,52 @@ + + */ +final readonly class GetTunnelOrderConfirmedQuery implements Query +{ + public function __construct( + private MapUrlService $mapUrlService, + ) { + } + + /** + * @param \App\Queries\Orders\GetTunnelOrderConfirmedQueryInput $input + */ + #[\Override] + public function __invoke(QueryInput $input): GetTunnelOrderConfirmedQueryOutput + { + Assert::isInstanceOf($input, GetTunnelOrderConfirmedQueryInput::class); + + $order = $input->order; + $order->loadMissing(['shop', 'lines']); + + $shop = $order->shop; + if (! $shop) { + throw new \UnexpectedValueException('Shop not found for order ' . $order->getId()); + } + + $shopDto = ShopDto::fromModel($shop); + $mapUrl = $this->mapUrlService->generateUrl($shopDto); + $orderDto = OrderDto::fromModel($order); + + return new GetTunnelOrderConfirmedQueryOutput( + step: OrderTunnelStep::CONFIRMATION, + order: $orderDto, + shop: $shopDto, + mapUrl: $mapUrl, + ); + } +} diff --git a/app/Queries/Orders/GetTunnelOrderConfirmedQueryInput.php b/app/Queries/Orders/GetTunnelOrderConfirmedQueryInput.php new file mode 100644 index 00000000..d28a06c4 --- /dev/null +++ b/app/Queries/Orders/GetTunnelOrderConfirmedQueryInput.php @@ -0,0 +1,16 @@ +name} {$addressable->address} {$addressable->postcode} {$addressable->city}"); diff --git a/tests/Feature/App/Http/Controllers/Fo/TunnelControllerTest.php b/tests/Feature/App/Http/Controllers/Fo/TunnelControllerTest.php index a4a9c920..45a9a079 100644 --- a/tests/Feature/App/Http/Controllers/Fo/TunnelControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Fo/TunnelControllerTest.php @@ -2,13 +2,43 @@ declare(strict_types=1); +use App\Contracts\Executor; +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 App\Queries\Orders\GetBasketSummaryQueryOutput; +use App\Queries\Orders\GetOrderConfirmationQueryOutput; +use App\Queries\Orders\GetPickTimeSlotQueryOutput; 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\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 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() @@ -25,6 +55,7 @@ describe('TunnelController', function (): void { }); 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() @@ -41,6 +72,7 @@ describe('TunnelController', function (): void { }); 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() @@ -57,6 +89,7 @@ describe('TunnelController', function (): void { }); 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() @@ -73,6 +106,7 @@ describe('TunnelController', function (): void { }); it('redirects unauthenticated guest accessing confirmation to login', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() @@ -89,6 +123,7 @@ describe('TunnelController', function (): void { }); it('redirects unauthenticated guest submitting confirmation to login', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $this->seed(ContentSeeder::class); View::share('PageList', Page::getCached() @@ -105,6 +140,7 @@ describe('TunnelController', function (): void { }); 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() @@ -119,4 +155,452 @@ describe('TunnelController', function (): void { // 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: 'Fac', + site: 'Site', + service: 'Service', + ); + $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: 'Fac', + site: 'Site', + service: 'Service', + ); + $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(\Symfony\Component\Uid\Uuid::v7()->toBase58()); + $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', $orderReference->value); + }); + + 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(\Symfony\Component\Uid\Uuid::v7()->toBase58()); + $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: 'Fac', + site: 'Site', + service: 'Service', + ); + $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/' . $orderReference->value); + + // THEN + $response->assertOk() + ->assertViewIs('fo.tunnel.confirmed') + ->assertViewHas('step', OrderTunnelStep::CONFIRMATION) + ->assertViewHas('order') + ->assertViewHas('shop') + ->assertViewHas('mapUrl'); + }); }); diff --git a/tests/Feature/App/Http/Requests/Fo/Orders/ConfirmOrderRequestTest.php b/tests/Feature/App/Http/Requests/Fo/Orders/ConfirmOrderRequestTest.php new file mode 100644 index 00000000..f1dcabaf --- /dev/null +++ b/tests/Feature/App/Http/Requests/Fo/Orders/ConfirmOrderRequestTest.php @@ -0,0 +1,73 @@ +shouldReceive('check')->once()->andReturn(true); + + $request = new ConfirmOrderRequest($authService); + + // 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 ConfirmOrderRequest($authService); + + // WHEN + $request->initialize(); + + // THEN + expect($request->authorize())->toBeFalse(); + }); + + it('has required rules for cgv acceptance', function () { + // GIVEN + $authService = \Mockery::mock(AppCustomerAuthService::class); + $request = new ConfirmOrderRequest($authService); + + // WHEN + $rules = $request->rules(); + + // THEN + expect($rules)->toHaveKey('ok_cgv') + ->and($rules['ok_cgv'])->toContain('accepted'); + }); + + it('maps to FinalizeOrderInput', 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 ConfirmOrderRequest($authService); + $request->initialize(); + + // WHEN + $input = $request->toFinalizeOrderInput(); + + // THEN + expect($input)->toBeInstanceOf(FinalizeOrderInput::class) + ->and($input->shopperId)->toBe($shopperId); + }); +}); diff --git a/tests/Feature/App/Http/Requests/Fo/Orders/PickTimeSlotRequestTest.php b/tests/Feature/App/Http/Requests/Fo/Orders/PickTimeSlotRequestTest.php index 665cbd27..c9453192 100644 --- a/tests/Feature/App/Http/Requests/Fo/Orders/PickTimeSlotRequestTest.php +++ b/tests/Feature/App/Http/Requests/Fo/Orders/PickTimeSlotRequestTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Actions\Orders\ApplyPickTimeSlotInput; use App\Http\Requests\Fo\Orders\PickTimeSlotRequest; use App\Models\Customer; use Carbon\CarbonInterface; @@ -193,4 +194,31 @@ describe('PickTimeSlotRequest', function () { ->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); + }); }); diff --git a/tests/Feature/App/Http/Requests/Fo/Orders/ValidateSummaryRequestTest.php b/tests/Feature/App/Http/Requests/Fo/Orders/ValidateSummaryRequestTest.php index ec2d9ebd..dd16bb0b 100644 --- a/tests/Feature/App/Http/Requests/Fo/Orders/ValidateSummaryRequestTest.php +++ b/tests/Feature/App/Http/Requests/Fo/Orders/ValidateSummaryRequestTest.php @@ -2,10 +2,13 @@ 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; @@ -82,4 +85,33 @@ describe('ValidateSummaryRequest', function () { 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); + }); }); diff --git a/tests/Integration/WithApp/App/Queries/Orders/GetTunnelOrderConfirmedQueryTest.php b/tests/Integration/WithApp/App/Queries/Orders/GetTunnelOrderConfirmedQueryTest.php new file mode 100644 index 00000000..d86e46c2 --- /dev/null +++ b/tests/Integration/WithApp/App/Queries/Orders/GetTunnelOrderConfirmedQueryTest.php @@ -0,0 +1,109 @@ +name = 'Boutique Retrait'; + $shop->address = '10 rue Principale'; + $shop->postcode = '75001'; + $shop->city = 'Paris'; + $shop->default = true; + $shop->save(); + + $reference = new OrderReference(Uuid::v7()->toBase58()); + + $order = new Order(); + $order->reference = $reference; + $order->total = Money::EUR(2500); + $order->status = OrderStatus::WAITING; + $order->pickup_day = now()->addDays(2); + $order->shop_id = $shop->id; + $order->delivery_address = new DeliveryAddress( + name: $shop->name, + address: $shop->address, + postcode: $shop->postcode, + city: $shop->city, + ); + $order->employee = new OrderEmployee( + firstname: 'Jean', + lastname: 'Dupont', + matricule: new Matricule('1234567'), + facility: 'Fac', + site: 'Site', + service: 'Service', + ); + $order->save(); + + /** @var GetTunnelOrderConfirmedQuery $query */ + $query = app(GetTunnelOrderConfirmedQuery::class); + $input = new GetTunnelOrderConfirmedQueryInput($order); + + // WHEN + $output = $query($input); + + // THEN + expect($output)->toBeInstanceOf(GetTunnelOrderConfirmedQueryOutput::class) + ->and($output->step)->toBe(OrderTunnelStep::CONFIRMATION) + ->and($output->order)->toBeInstanceOf(OrderDto::class) + ->and((string) $output->order->reference)->toBe($reference->value) + ->and($output->shop)->toBeInstanceOf(ShopDto::class) + ->and($output->shop->name)->toBe('Boutique Retrait') + ->and($output->mapUrl)->toBeString(); + }); + + it('throws UnexpectedValueException when order has no shop', function (): void { + // GIVEN + $reference = new OrderReference(Uuid::v7()->toBase58()); + + $order = new Order(); + $order->reference = $reference; + $order->total = Money::EUR(2500); + $order->status = OrderStatus::WAITING; + $order->pickup_day = now()->addDays(2); + $order->shop_id = null; + $order->employee = new OrderEmployee( + firstname: 'Jean', + lastname: 'Dupont', + matricule: new Matricule('1234567'), + facility: 'Fac', + site: 'Site', + service: 'Service', + ); + $order->delivery_address = new DeliveryAddress( + name: 'Nom', + address: '1 rue Test', + postcode: '75001', + city: 'Paris', + ); + $order->save(); + + /** @var GetTunnelOrderConfirmedQuery $query */ + $query = app(GetTunnelOrderConfirmedQuery::class); + $input = new GetTunnelOrderConfirmedQueryInput($order); + + // WHEN & THEN + expect(fn () => $query($input))->toThrow(UnexpectedValueException::class); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotInputTest.php b/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotInputTest.php new file mode 100644 index 00000000..dc6f4e30 --- /dev/null +++ b/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotInputTest.php @@ -0,0 +1,26 @@ +shopperId)->toBe($shopperId) + ->and($input->timeslot)->toBe($timeslot); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotTest.php b/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotTest.php new file mode 100644 index 00000000..371c4f58 --- /dev/null +++ b/tests/Unit/App/Actions/Orders/ApplyPickTimeSlotTest.php @@ -0,0 +1,62 @@ +shouldReceive('getBuilderFromBasketOrFail') + ->once() + ->andReturn($builder); + $orderTunnelService->shouldReceive('getShopForCustomer') + ->once() + ->with($shopperId) + ->andReturn($shop); + $orderTunnelService->shouldReceive('getAvailableTimeSlots') + ->once() + ->with($shop) + ->andReturn($timeSlots); + $orderTunnelService->shouldReceive('applyTimeSlot') + ->once() + ->with($builder, $shop, $timeslot, $timeSlots) + ->andReturn($slot); + + $action = new ApplyPickTimeSlot($orderTunnelService); + + // WHEN + $result = $action($input); + + // THEN + expect($result)->toBe($slot); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/FinalizeOrderInputTest.php b/tests/Unit/App/Actions/Orders/FinalizeOrderInputTest.php new file mode 100644 index 00000000..f3341781 --- /dev/null +++ b/tests/Unit/App/Actions/Orders/FinalizeOrderInputTest.php @@ -0,0 +1,28 @@ +shopperId)->toBeNull(); + }); + + it('can be instantiated with valid shopperId', function (): void { + // GIVEN + $shopperId = new ShopperId(Uuid::uuid7()); + + // WHEN + $input = new FinalizeOrderInput(shopperId: $shopperId); + + // THEN + expect($input->shopperId)->toBe($shopperId); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/FinalizeOrderTest.php b/tests/Unit/App/Actions/Orders/FinalizeOrderTest.php new file mode 100644 index 00000000..2bd69e88 --- /dev/null +++ b/tests/Unit/App/Actions/Orders/FinalizeOrderTest.php @@ -0,0 +1,70 @@ +toBase58()); + $builder = \Mockery::mock(OrderBuilderCompleted::class); + $order = new Order( + id: new OrderId(Uuid::uuid7()), + shopId: new ShopId(Uuid::uuid7()), + reference: $orderReference, + employee: new OrderEmployee( + firstname: 'Jean', + lastname: 'Dupont', + matricule: new Matricule('1234567'), + facility: 'Fac', + site: 'Site', + service: 'Service', + ), + deliveryAddress: new DeliveryAddress( + name: 'Shop', + address: '1 rue Test', + postcode: '75001', + city: 'Paris', + ), + pickupTimeSlot: new PickupTimeSlot(now(), new \DateInterval('PT1H')), + status: OrderStatus::WAITING, + total: Money::EUR(1000), + lines: [], + ); + + $orderTunnelService = \Mockery::mock(OrderTunnelService::class); + $orderTunnelService->shouldReceive('getBuilderCompletedOrFail') + ->once() + ->andReturn($builder); + $orderTunnelService->shouldReceive('finalizeOrder') + ->once() + ->with($builder) + ->andReturn($order); + + $action = new FinalizeOrder($orderTunnelService); + + // WHEN + $result = $action($input); + + // THEN + expect($result)->toBeInstanceOf(OrderReference::class) + ->and($result->value)->toBe($orderReference->value); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/InitializeOrderTunnelInputTest.php b/tests/Unit/App/Actions/Orders/InitializeOrderTunnelInputTest.php new file mode 100644 index 00000000..88a00f54 --- /dev/null +++ b/tests/Unit/App/Actions/Orders/InitializeOrderTunnelInputTest.php @@ -0,0 +1,27 @@ +basket)->toBe($basket) + ->and($input->shopperId)->toBe($shopperId); + }); +}); diff --git a/tests/Unit/App/Actions/Orders/InitializeOrderTunnelTest.php b/tests/Unit/App/Actions/Orders/InitializeOrderTunnelTest.php new file mode 100644 index 00000000..3ebc0cde --- /dev/null +++ b/tests/Unit/App/Actions/Orders/InitializeOrderTunnelTest.php @@ -0,0 +1,36 @@ +shouldReceive('initializeFromBasket') + ->once() + ->with($basket, $shopperId); + + $action = new InitializeOrderTunnel($orderTunnelService); + + // WHEN + $action($input); + + // THEN + expect(true)->toBeTrue(); + }); +}); diff --git a/tests/Unit/App/Dto/ShopDtoTest.php b/tests/Unit/App/Dto/ShopDtoTest.php index 560e3584..a35702d0 100644 --- a/tests/Unit/App/Dto/ShopDtoTest.php +++ b/tests/Unit/App/Dto/ShopDtoTest.php @@ -3,6 +3,9 @@ declare(strict_types=1); use App\Dto\ShopDto; +use Domain\Models\Entities\Shop; +use Domain\Models\ValueObjects\Entities\ShopId; +use Ramsey\Uuid\Uuid; describe('ShopDto', function () { test('it can be instantiated directly and converts to array', function () { @@ -37,4 +40,35 @@ describe('ShopDto', function () { 'default' => true, ]); }); + + test('it can be created from domain Shop entity', function () { + // GIVEN + $uuid = Uuid::uuid7(); + $shopId = new ShopId($uuid); + $domainShop = new Shop( + id: $shopId, + name: 'Boutique Domain', + address: '10 avenue de la Gare', + postcode: '69001', + city: 'Lyon', + phone: '0478000000', + latitude: '45.75', + longitude: '4.85', + isDefault: true, + ); + + // WHEN + $dto = ShopDto::fromDomain($domainShop); + + // THEN + expect($dto->id)->toBe($uuid->toString()) + ->and($dto->name)->toBe('Boutique Domain') + ->and($dto->address)->toBe('10 avenue de la Gare') + ->and($dto->postcode)->toBe('69001') + ->and($dto->city)->toBe('Lyon') + ->and($dto->phone)->toBe('0478000000') + ->and($dto->latitude)->toBe('45.75') + ->and($dto->longitude)->toBe('4.85') + ->and($dto->default)->toBeTrue(); + }); }); diff --git a/tests/Unit/App/Queries/Orders/GetBasketSummaryQueryTest.php b/tests/Unit/App/Queries/Orders/GetBasketSummaryQueryTest.php new file mode 100644 index 00000000..a92df208 --- /dev/null +++ b/tests/Unit/App/Queries/Orders/GetBasketSummaryQueryTest.php @@ -0,0 +1,34 @@ +shouldReceive('getBasket') + ->once() + ->andReturn($basket); + + $query = new GetBasketSummaryQuery($basketService); + $input = new GetBasketSummaryQueryInput(); + + // WHEN + $output = $query($input); + + // THEN + expect($output)->toBeInstanceOf(GetBasketSummaryQueryOutput::class) + ->and($output->step)->toBe(OrderTunnelStep::REVIEW) + ->and($output->basket)->toBe($basket); + }); +}); diff --git a/tests/Unit/App/Queries/Orders/GetOrderConfirmationQueryTest.php b/tests/Unit/App/Queries/Orders/GetOrderConfirmationQueryTest.php new file mode 100644 index 00000000..bd37aa72 --- /dev/null +++ b/tests/Unit/App/Queries/Orders/GetOrderConfirmationQueryTest.php @@ -0,0 +1,88 @@ +shouldReceive('getBasket')->once()->andReturn($basket); + $builder->shouldReceive('getShopId')->once()->andReturn($shopId); + + $shop = new Shop( + id: $shopId, + name: 'Shop 1', + address: '1 rue Test', + postcode: '75001', + city: 'Paris', + ); + + $orderTunnelService = \Mockery::mock(OrderTunnelService::class); + $orderTunnelService->shouldReceive('getBuilderCompletedOrFail') + ->once() + ->andReturn($builder); + + $shopRepository = \Mockery::mock(ShopRepository::class); + $shopRepository->shouldReceive('find') + ->once() + ->with($shopId) + ->andReturn($shop); + + $query = new GetOrderConfirmationQuery($orderTunnelService, $shopRepository); + $input = new GetOrderConfirmationQueryInput(); + + // WHEN + $output = $query($input); + + // THEN + expect($output)->toBeInstanceOf(GetOrderConfirmationQueryOutput::class) + ->and($output->step)->toBe(OrderTunnelStep::CONFIRMATION) + ->and($output->basket)->toBe($basket) + ->and($output->builder)->toBe($builder) + ->and($output->shop)->toBeInstanceOf(ShopDto::class) + ->and($output->shop->name)->toBe('Shop 1'); + }); + + it('throws NotFoundHttpException when shop is not found', function (): void { + // GIVEN + $shopId = new ShopId(Uuid::uuid7()); + $basket = new Basket(new BasketId(Uuid::uuid7())); + $builder = \Mockery::mock(OrderBuilderCompleted::class); + $builder->shouldReceive('getBasket')->once()->andReturn($basket); + $builder->shouldReceive('getShopId')->once()->andReturn($shopId); + + $orderTunnelService = \Mockery::mock(OrderTunnelService::class); + $orderTunnelService->shouldReceive('getBuilderCompletedOrFail') + ->once() + ->andReturn($builder); + + $shopRepository = \Mockery::mock(ShopRepository::class); + $shopRepository->shouldReceive('find') + ->once() + ->with($shopId) + ->andReturn(null); + + $query = new GetOrderConfirmationQuery($orderTunnelService, $shopRepository); + $input = new GetOrderConfirmationQueryInput(); + + // WHEN & THEN + expect(fn () => $query($input))->toThrow(NotFoundHttpException::class, 'Shop not found'); + }); +}); diff --git a/tests/Unit/App/Queries/Orders/GetPickTimeSlotQueryTest.php b/tests/Unit/App/Queries/Orders/GetPickTimeSlotQueryTest.php new file mode 100644 index 00000000..f7d824e9 --- /dev/null +++ b/tests/Unit/App/Queries/Orders/GetPickTimeSlotQueryTest.php @@ -0,0 +1,63 @@ +shouldReceive('getShopForCustomer') + ->once() + ->with($shopperId) + ->andReturn($shop); + $orderTunnelService->shouldReceive('getAvailableTimeSlots') + ->once() + ->with($shop) + ->andReturn($timeSlots); + + $mapUrlService = \Mockery::mock(MapUrlService::class); + $mapUrlService->shouldReceive('generateUrl') + ->once() + ->withArgs(fn ($arg) => $arg instanceof ShopDto && $arg->name === 'Shop 1') + ->andReturn('https://maps.example.com'); + + $query = new GetPickTimeSlotQuery($orderTunnelService, $mapUrlService); + + // WHEN + $output = $query($input); + + // THEN + expect($output)->toBeInstanceOf(GetPickTimeSlotQueryOutput::class) + ->and($output->step)->toBe(OrderTunnelStep::BOOKING) + ->and($output->shop)->toBeInstanceOf(ShopDto::class) + ->and($output->shop->name)->toBe('Shop 1') + ->and($output->mapUrl)->toBe('https://maps.example.com') + ->and($output->timeSlots)->toBe($timeSlots); + }); +}); diff --git a/tests/Unit/App/Services/MapUrlServiceTest.php b/tests/Unit/App/Services/MapUrlServiceTest.php index 520a2345..a7a91f62 100644 --- a/tests/Unit/App/Services/MapUrlServiceTest.php +++ b/tests/Unit/App/Services/MapUrlServiceTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Dto\ShopDto; use App\Services\MapUrlService; use Domain\Models\ValueObjects\DeliveryAddress; @@ -48,4 +49,26 @@ describe('MapUrlService', function () { $query = urlencode('My Shop 123 Test St 12345 Test City'); expect($url)->toBe("https://maps.google.com/?q={$query}"); }); + + it('generates an OpenStreetMap URL for a ShopDto', function () { + // GIVEN + $service = new MapUrlService(); + $shopDto = new ShopDto( + id: 'test-id', + name: 'DTO Shop', + address: '456 DTO St', + postcode: '69001', + city: 'Lyon', + phone: null, + latitude: '45.75', + longitude: '4.85', + default: false, + ); + + // WHEN + $url = $service->generateUrl($shopDto); + + // THEN + expect($url)->toBe('https://www.openstreetmap.org/?mlat=45.75&mlon=4.85#map=16/45.75/4.85'); + }); });