ldc-boutique-cnc-sable/tests/Unit/App/Queries/Orders/GetOrderConfirmationQueryTest.php

88 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Dto\ShopDto;
use App\Queries\Orders\GetOrderConfirmationQuery;
use App\Queries\Orders\GetOrderConfirmationQueryInput;
use App\Queries\Orders\GetOrderConfirmationQueryOutput;
use Domain\Builders\OrderBuilderCompleted;
use Domain\Enums\OrderTunnelStep;
use Domain\Models\Entities\Basket;
use Domain\Models\Entities\Shop;
use Domain\Models\ValueObjects\Entities\BasketId;
use Domain\Models\ValueObjects\Entities\ShopId;
use Domain\Ports\Repositories\ShopRepository;
use Domain\Services\OrderTunnelService;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
describe('GetOrderConfirmationQuery', function (): void {
it('returns order confirmation output with step, basket, builder and shop', 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);
$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');
});
});