diff --git a/spec/Handler/Cart/PickupCartHandlerSpec.php b/spec/Handler/Cart/PickupCartHandlerSpec.php index e0c08328e..9976ce131 100644 --- a/spec/Handler/Cart/PickupCartHandlerSpec.php +++ b/spec/Handler/Cart/PickupCartHandlerSpec.php @@ -13,15 +13,28 @@ use Sylius\Component\Locale\Model\LocaleInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; +use Sylius\ShopApiPlugin\Handler\Cart\PickupCartHandler; final class PickupCartHandlerSpec extends ObjectBehavior { - function let(FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, ChannelRepositoryInterface $channelRepository): void + function let( + FactoryInterface $cartFactory, + OrderRepositoryInterface $cartRepository, + ChannelRepositoryInterface $channelRepository + ): void { + $this->beConstructedWith( + $cartFactory, + $cartRepository, + $channelRepository + ); + } + + function it_is_initializable(): void { - $this->beConstructedWith($cartFactory, $cartRepository, $channelRepository); + $this->shouldHaveType(PickupCartHandler::class); } - function it_handles_cart_pickup( + function it_handles_cart_pickup_for_not_logged_in_user( ChannelInterface $channel, CurrencyInterface $currency, ChannelRepositoryInterface $channelRepository, @@ -43,7 +56,7 @@ function it_handles_cart_pickup( $cart->setCurrencyCode('EUR')->shouldBeCalled(); $cart->setLocaleCode('de_DE')->shouldBeCalled(); - $cartRepository->add($cart)->shouldBeCalled(); + $cartRepository->add($cart)->shouldBeCalledOnce(); $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); } diff --git a/src/Controller/Cart/PickupAction.php b/src/Controller/Cart/PickupCartAction.php similarity index 91% rename from src/Controller/Cart/PickupAction.php rename to src/Controller/Cart/PickupCartAction.php index 9debc2061..600d91dc1 100644 --- a/src/Controller/Cart/PickupAction.php +++ b/src/Controller/Cart/PickupCartAction.php @@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Validator\Validator\ValidatorInterface; -final class PickupAction +final class PickupCartAction { /** @var ViewHandlerInterface */ private $viewHandler; @@ -66,6 +66,8 @@ public function __invoke(Request $request): Response } } - return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST)); + return $this->viewHandler->handle( + View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST) + ); } } diff --git a/src/Handler/Cart/PickupCartHandler.php b/src/Handler/Cart/PickupCartHandler.php index 9f3f3efd0..00f5c41a7 100644 --- a/src/Handler/Cart/PickupCartHandler.php +++ b/src/Handler/Cart/PickupCartHandler.php @@ -23,17 +23,19 @@ final class PickupCartHandler /** @var ChannelRepositoryInterface */ private $channelRepository; - public function __construct(FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, ChannelRepositoryInterface $channelRepository) - { + public function __construct( + FactoryInterface $cartFactory, + OrderRepositoryInterface $cartRepository, + ChannelRepositoryInterface $channelRepository + ) { $this->cartFactory = $cartFactory; $this->cartRepository = $cartRepository; $this->channelRepository = $channelRepository; } - /** @param PickupCart $pickupCart */ public function handle(PickupCart $pickupCart) { - /** @var ChannelInterface $channel */ + /** @var ChannelInterface|null $channel */ $channel = $this->channelRepository->findOneByCode($pickupCart->channelCode()); Assert::notNull($channel, sprintf('Channel with %s code has not been found.', $pickupCart->channelCode())); diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 01802021e..69e2ae1f2 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -63,5 +63,10 @@ + + + + + diff --git a/src/Resources/config/services/actions/cart.xml b/src/Resources/config/services/actions/cart.xml index 3ea87ffff..205524c43 100644 --- a/src/Resources/config/services/actions/cart.xml +++ b/src/Resources/config/services/actions/cart.xml @@ -30,7 +30,7 @@ diff --git a/tests/Controller/Cart/CartPickupApiTest.php b/tests/Controller/Cart/CartPickupApiTest.php index 13ff9ba3a..b57fa8568 100644 --- a/tests/Controller/Cart/CartPickupApiTest.php +++ b/tests/Controller/Cart/CartPickupApiTest.php @@ -5,12 +5,16 @@ namespace Tests\Sylius\ShopApiPlugin\Controller\Cart; use League\Tactician\CommandBus; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; use Symfony\Component\HttpFoundation\Response; use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase; +use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait; final class CartPickupApiTest extends JsonApiTestCase { + use ShopUserLoginTrait; + /** * @test */ @@ -23,16 +27,44 @@ public function it_creates_a_new_cart(): void $response = $this->client->getResponse(); $this->assertResponse($response, 'cart/empty_response', Response::HTTP_CREATED); + + $orderRepository = $this->get('sylius.repository.order'); + $count = $orderRepository->count([]); + + $this->assertSame(1, $count, 'Only one cart should be created'); + } + + /** + * @test + */ + public function it_only_creates_one_cart_if_user_is_logged_in(): void + { + $this->loadFixturesFromFiles(['shop.yml', 'customer.yml']); + + $this->logInUser('oliver@queen.com', '123password'); + + $this->client->request('POST', '/shop-api/WEB_GB/carts', [], [], static::CONTENT_TYPE_HEADER); + $response = $this->client->getResponse(); + $this->assertResponseCode($response, Response::HTTP_CREATED); + + /** @var OrderRepositoryInterface $orderRepository */ + $orderRepository = $this->get('sylius.repository.order'); + $orders = $orderRepository->findAll(); + + $this->assertCount(1, $orders, 'Only one cart should be created'); } /** + * @deprecated * @test */ public function it_creates_a_new_cart_using_deprecated_api(): void { $this->loadFixturesFromFiles(['shop.yml']); - $this->client->request('POST', '/shop-api/WEB_GB/carts/SDAOSLEFNWU35H3QLI5325', [], [], self::CONTENT_TYPE_HEADER); + $this->client->request( + 'POST', '/shop-api/WEB_GB/carts/SDAOSLEFNWU35H3QLI5325', [], [], self::CONTENT_TYPE_HEADER + ); $response = $this->client->getResponse(); @@ -40,6 +72,7 @@ public function it_creates_a_new_cart_using_deprecated_api(): void } /** + * @deprecated * @test */ public function it_does_not_allow_to_create_a_new_cart_if_token_is_already_used(): void @@ -66,7 +99,9 @@ public function it_does_not_allow_to_create_a_new_cart_in_non_existent_channel() { $this->loadFixturesFromFiles(['shop.yml']); - $this->client->request('POST', '/shop-api/SPACE_KLINGON/carts/SDAOSLEFNWU35H3QLI5325', [], [], self::CONTENT_TYPE_HEADER); + $this->client->request( + 'POST', '/shop-api/SPACE_KLINGON/carts/SDAOSLEFNWU35H3QLI5325', [], [], self::CONTENT_TYPE_HEADER + ); $response = $this->client->getResponse();