From 433ec6300925a0455b4341864e97bad68a79f391 Mon Sep 17 00:00:00 2001 From: mamazu Date: Sat, 16 Feb 2019 18:36:04 +0100 Subject: [PATCH 1/4] Fix for the duplicate cart --- spec/Handler/Cart/PickupCartHandlerSpec.php | 49 +++++++++++++++++-- src/Handler/Cart/PickupCartHandler.php | 37 +++++++++++++- .../config/services/handler/cart.xml | 1 + tests/Controller/Cart/CartPickupApiTest.php | 39 ++++++++++++++- 4 files changed, 117 insertions(+), 9 deletions(-) diff --git a/spec/Handler/Cart/PickupCartHandlerSpec.php b/spec/Handler/Cart/PickupCartHandlerSpec.php index e0c08328e..bcc79f52d 100644 --- a/spec/Handler/Cart/PickupCartHandlerSpec.php +++ b/spec/Handler/Cart/PickupCartHandlerSpec.php @@ -5,28 +5,42 @@ namespace spec\Sylius\ShopApiPlugin\Handler\Cart; use PhpSpec\ObjectBehavior; +use Prophecy\Argument; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; +use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\OrderInterface; +use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Currency\Model\CurrencyInterface; use Sylius\Component\Locale\Model\LocaleInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; +use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface; final class PickupCartHandlerSpec extends ObjectBehavior { - function let(FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, ChannelRepositoryInterface $channelRepository): void - { - $this->beConstructedWith($cartFactory, $cartRepository, $channelRepository); + function let( + FactoryInterface $cartFactory, + OrderRepositoryInterface $cartRepository, + ChannelRepositoryInterface $channelRepository, + LoggedInShopUserProviderInterface $loggedInShopUserProvider + ): void { + $this->beConstructedWith( + $cartFactory, + $cartRepository, + $channelRepository, + $loggedInShopUserProvider + ); } - function it_handles_cart_pickup( + function it_handles_cart_pickup_for_not_logged_in_user( ChannelInterface $channel, CurrencyInterface $currency, ChannelRepositoryInterface $channelRepository, FactoryInterface $cartFactory, LocaleInterface $locale, + LoggedInShopUserProviderInterface $loggedInShopUserProvider, OrderInterface $cart, OrderRepositoryInterface $cartRepository ): void { @@ -43,11 +57,36 @@ function it_handles_cart_pickup( $cart->setCurrencyCode('EUR')->shouldBeCalled(); $cart->setLocaleCode('de_DE')->shouldBeCalled(); - $cartRepository->add($cart)->shouldBeCalled(); + $loggedInShopUserProvider->isUserLoggedIn()->willReturn(false); + $cart->setCustomer(Argument::any())->shouldNotBeCalled(); + + $cartRepository->add($cart)->shouldBeCalledOnce(); $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); } + function it_handles_cart_pickup_for_a_logged_in_user( + ChannelInterface $channel, + ChannelRepositoryInterface $channelRepository, + LoggedInShopUserProviderInterface $loggedInShopUserProvider, + ShopUserInterface $user, + CustomerInterface $customer, + OrderInterface $cart, + OrderRepositoryInterface $cartRepository + ): void { + $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); + + $loggedInShopUserProvider->isUserLoggedIn()->willReturn(true); + $loggedInShopUserProvider->provide()->willReturn($user); + + $user->getCustomer()->willReturn($customer); + $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn($cart); + $cart->setTokenValue('ORDERTOKEN')->shouldBeCalled(); + + $cartRepository->add(Argument::any())->shouldNotBeCalled(); + + $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); + } function it_throws_an_exception_if_channel_is_not_found( ChannelRepositoryInterface $channelRepository ): void { diff --git a/src/Handler/Cart/PickupCartHandler.php b/src/Handler/Cart/PickupCartHandler.php index 9f3f3efd0..2b191643e 100644 --- a/src/Handler/Cart/PickupCartHandler.php +++ b/src/Handler/Cart/PickupCartHandler.php @@ -4,12 +4,14 @@ namespace Sylius\ShopApiPlugin\Handler\Cart; +use Sylius\Bundle\CoreBundle\EventListener\UserCartRecalculationListener; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; +use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface; use Webmozart\Assert\Assert; final class PickupCartHandler @@ -23,11 +25,19 @@ final class PickupCartHandler /** @var ChannelRepositoryInterface */ private $channelRepository; - public function __construct(FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, ChannelRepositoryInterface $channelRepository) - { + /** @var LoggedInShopUserProviderInterface */ + private $loggedInShopUserProvider; + + public function __construct( + FactoryInterface $cartFactory, + OrderRepositoryInterface $cartRepository, + ChannelRepositoryInterface $channelRepository, + LoggedInShopUserProviderInterface $loggedInShopUserProvider + ) { $this->cartFactory = $cartFactory; $this->cartRepository = $cartRepository; $this->channelRepository = $channelRepository; + $this->loggedInShopUserProvider = $loggedInShopUserProvider; } /** @param PickupCart $pickupCart */ @@ -38,6 +48,12 @@ public function handle(PickupCart $pickupCart) Assert::notNull($channel, sprintf('Channel with %s code has not been found.', $pickupCart->channelCode())); + if ($this->loggedInShopUserProvider->isUserLoggedIn()) { + $this->handleUserCart($pickupCart->orderToken(), $channel); + + return; + } + /** @var OrderInterface $cart */ $cart = $this->cartFactory->createNew(); $cart->setChannel($channel); @@ -47,4 +63,21 @@ public function handle(PickupCart $pickupCart) $this->cartRepository->add($cart); } + + /** + * If the user is logged in we do not need to create a cart. + * + * The reason: The `UserCartRecalculationListener` is called for every authenticated request. In the case the cart does not exist it creates one. + * + * @see UserCartRecalculationListener + */ + private function handleUserCart(string $tokenValue, ChannelInterface $channel): void + { + $shopUser = $this->loggedInShopUserProvider->provide(); + $customer = $shopUser->getCustomer(); + + /** @var OrderInterface $cart */ + $cart = $this->cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel]); + $cart->setTokenValue($tokenValue); + } } diff --git a/src/Resources/config/services/handler/cart.xml b/src/Resources/config/services/handler/cart.xml index abe5c1697..8984a10be 100644 --- a/src/Resources/config/services/handler/cart.xml +++ b/src/Resources/config/services/handler/cart.xml @@ -15,6 +15,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(); From ab9431290c774c538c45837e3fe041cc7ee62c73 Mon Sep 17 00:00:00 2001 From: mamazu Date: Sat, 16 Feb 2019 18:54:57 +0100 Subject: [PATCH 2/4] Codestyle --- spec/Handler/Cart/PickupCartHandlerSpec.php | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/Handler/Cart/PickupCartHandlerSpec.php b/spec/Handler/Cart/PickupCartHandlerSpec.php index bcc79f52d..17a24c02f 100644 --- a/spec/Handler/Cart/PickupCartHandlerSpec.php +++ b/spec/Handler/Cart/PickupCartHandlerSpec.php @@ -87,6 +87,7 @@ function it_handles_cart_pickup_for_a_logged_in_user( $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); } + function it_throws_an_exception_if_channel_is_not_found( ChannelRepositoryInterface $channelRepository ): void { From 42ae06822c2748b46aa63bdc3307bc6b22e5f700 Mon Sep 17 00:00:00 2001 From: mamazu Date: Sun, 17 Feb 2019 13:43:51 +0100 Subject: [PATCH 3/4] Refactored it --- spec/Handler/Cart/PickupCartHandlerSpec.php | 43 ++-------- .../Cart/PickupLoggedInCartHandlerSpec.php | 84 +++++++++++++++++++ src/Command/PickupLoggedInCart.php | 9 ++ ...{PickupAction.php => PickupCartAction.php} | 16 +++- src/Handler/Cart/PickupCartHandler.php | 35 +------- .../Cart/PickupLoggedInCartHandler.php | 51 +++++++++++ src/Request/PickupLoggedInCartRequest.php | 16 ++++ .../config/services/actions/cart.xml | 3 +- .../config/services/handler/cart.xml | 9 +- 9 files changed, 194 insertions(+), 72 deletions(-) create mode 100644 spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php create mode 100644 src/Command/PickupLoggedInCart.php rename src/Controller/Cart/{PickupAction.php => PickupCartAction.php} (75%) create mode 100644 src/Handler/Cart/PickupLoggedInCartHandler.php create mode 100644 src/Request/PickupLoggedInCartRequest.php diff --git a/spec/Handler/Cart/PickupCartHandlerSpec.php b/spec/Handler/Cart/PickupCartHandlerSpec.php index 17a24c02f..9976ce131 100644 --- a/spec/Handler/Cart/PickupCartHandlerSpec.php +++ b/spec/Handler/Cart/PickupCartHandlerSpec.php @@ -5,42 +5,41 @@ namespace spec\Sylius\ShopApiPlugin\Handler\Cart; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; -use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\OrderInterface; -use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Currency\Model\CurrencyInterface; use Sylius\Component\Locale\Model\LocaleInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; -use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface; +use Sylius\ShopApiPlugin\Handler\Cart\PickupCartHandler; final class PickupCartHandlerSpec extends ObjectBehavior { function let( FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, - ChannelRepositoryInterface $channelRepository, - LoggedInShopUserProviderInterface $loggedInShopUserProvider + ChannelRepositoryInterface $channelRepository ): void { $this->beConstructedWith( $cartFactory, $cartRepository, - $channelRepository, - $loggedInShopUserProvider + $channelRepository ); } + function it_is_initializable(): void + { + $this->shouldHaveType(PickupCartHandler::class); + } + function it_handles_cart_pickup_for_not_logged_in_user( ChannelInterface $channel, CurrencyInterface $currency, ChannelRepositoryInterface $channelRepository, FactoryInterface $cartFactory, LocaleInterface $locale, - LoggedInShopUserProviderInterface $loggedInShopUserProvider, OrderInterface $cart, OrderRepositoryInterface $cartRepository ): void { @@ -57,37 +56,11 @@ function it_handles_cart_pickup_for_not_logged_in_user( $cart->setCurrencyCode('EUR')->shouldBeCalled(); $cart->setLocaleCode('de_DE')->shouldBeCalled(); - $loggedInShopUserProvider->isUserLoggedIn()->willReturn(false); - $cart->setCustomer(Argument::any())->shouldNotBeCalled(); - $cartRepository->add($cart)->shouldBeCalledOnce(); $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); } - function it_handles_cart_pickup_for_a_logged_in_user( - ChannelInterface $channel, - ChannelRepositoryInterface $channelRepository, - LoggedInShopUserProviderInterface $loggedInShopUserProvider, - ShopUserInterface $user, - CustomerInterface $customer, - OrderInterface $cart, - OrderRepositoryInterface $cartRepository - ): void { - $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); - - $loggedInShopUserProvider->isUserLoggedIn()->willReturn(true); - $loggedInShopUserProvider->provide()->willReturn($user); - - $user->getCustomer()->willReturn($customer); - $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn($cart); - $cart->setTokenValue('ORDERTOKEN')->shouldBeCalled(); - - $cartRepository->add(Argument::any())->shouldNotBeCalled(); - - $this->handle(new PickupCart('ORDERTOKEN', 'CHANNEL_CODE')); - } - function it_throws_an_exception_if_channel_is_not_found( ChannelRepositoryInterface $channelRepository ): void { diff --git a/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php b/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php new file mode 100644 index 000000000..c2c623210 --- /dev/null +++ b/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php @@ -0,0 +1,84 @@ +beConstructedWith( + $cartRepository, + $channelRepository, + $loggedInShopUserProvider + ); + } + + function it_is_initializable(): void + { + $this->shouldHaveType(PickupLoggedInCartHandler::class); + } + + function it_handles_cart_pickup_for_a_logged_in_user( + ChannelInterface $channel, + ChannelRepositoryInterface $channelRepository, + LoggedInShopUserProviderInterface $loggedInShopUserProvider, + ShopUserInterface $user, + CustomerInterface $customer, + OrderInterface $cart, + OrderRepositoryInterface $cartRepository + ): void { + $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); + + $loggedInShopUserProvider->provide()->willReturn($user); + $user->getCustomer()->willReturn($customer); + + $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn($cart); + $cart->setTokenValue('ORDERTOKEN')->shouldBeCalled(); + + $this->handle(new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')); + } + + function it_throws_an_exception_if_channel_is_not_found( + ChannelRepositoryInterface $channelRepository + ): void { + $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn(null); + + $this->shouldThrow(\InvalidArgumentException::class) + ->during('handle', [new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')]); + } + + function it_throws_an_exception_when_no_order_was_found( + ChannelInterface $channel, + ChannelRepositoryInterface $channelRepository, + LoggedInShopUserProviderInterface $loggedInShopUserProvider, + ShopUserInterface $user, + CustomerInterface $customer, + OrderRepositoryInterface $cartRepository + ): void { + $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); + + $loggedInShopUserProvider->provide()->willReturn($user); + $user->getCustomer()->willReturn($customer); + + $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn(null); + + $this->shouldThrow(\InvalidArgumentException::class) + ->during('handle', [new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')]); + } +} diff --git a/src/Command/PickupLoggedInCart.php b/src/Command/PickupLoggedInCart.php new file mode 100644 index 000000000..a7dbc47e4 --- /dev/null +++ b/src/Command/PickupLoggedInCart.php @@ -0,0 +1,9 @@ +validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; + $this->loggedInShopUserProvider = $loggedInShopUserProvider; } public function __invoke(Request $request): Response { $pickupRequest = new PickupCartRequest($request); + if ($this->loggedInShopUserProvider->isUserLoggedIn()) { + $pickupRequest = new PickupLoggedInCartRequest($request); + } $validationResults = $this->validator->validate($pickupRequest); @@ -66,6 +76,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 2b191643e..00f5c41a7 100644 --- a/src/Handler/Cart/PickupCartHandler.php +++ b/src/Handler/Cart/PickupCartHandler.php @@ -4,14 +4,12 @@ namespace Sylius\ShopApiPlugin\Handler\Cart; -use Sylius\Bundle\CoreBundle\EventListener\UserCartRecalculationListener; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\ShopApiPlugin\Command\PickupCart; -use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface; use Webmozart\Assert\Assert; final class PickupCartHandler @@ -25,35 +23,23 @@ final class PickupCartHandler /** @var ChannelRepositoryInterface */ private $channelRepository; - /** @var LoggedInShopUserProviderInterface */ - private $loggedInShopUserProvider; - public function __construct( FactoryInterface $cartFactory, OrderRepositoryInterface $cartRepository, - ChannelRepositoryInterface $channelRepository, - LoggedInShopUserProviderInterface $loggedInShopUserProvider + ChannelRepositoryInterface $channelRepository ) { $this->cartFactory = $cartFactory; $this->cartRepository = $cartRepository; $this->channelRepository = $channelRepository; - $this->loggedInShopUserProvider = $loggedInShopUserProvider; } - /** @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())); - if ($this->loggedInShopUserProvider->isUserLoggedIn()) { - $this->handleUserCart($pickupCart->orderToken(), $channel); - - return; - } - /** @var OrderInterface $cart */ $cart = $this->cartFactory->createNew(); $cart->setChannel($channel); @@ -63,21 +49,4 @@ public function handle(PickupCart $pickupCart) $this->cartRepository->add($cart); } - - /** - * If the user is logged in we do not need to create a cart. - * - * The reason: The `UserCartRecalculationListener` is called for every authenticated request. In the case the cart does not exist it creates one. - * - * @see UserCartRecalculationListener - */ - private function handleUserCart(string $tokenValue, ChannelInterface $channel): void - { - $shopUser = $this->loggedInShopUserProvider->provide(); - $customer = $shopUser->getCustomer(); - - /** @var OrderInterface $cart */ - $cart = $this->cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel]); - $cart->setTokenValue($tokenValue); - } } diff --git a/src/Handler/Cart/PickupLoggedInCartHandler.php b/src/Handler/Cart/PickupLoggedInCartHandler.php new file mode 100644 index 000000000..06f6d70d6 --- /dev/null +++ b/src/Handler/Cart/PickupLoggedInCartHandler.php @@ -0,0 +1,51 @@ +cartRepository = $cartRepository; + $this->channelRepository = $channelRepository; + $this->loggedInShopUserProvider = $loggedInShopUserProvider; + } + + public function handle(PickupLoggedInCart $pickupLoggedInCart) + { + /** @var ChannelInterface|null $channel */ + $channel = $this->channelRepository->findOneByCode($pickupLoggedInCart->channelCode()); + Assert::notNull($channel, sprintf('Channel with %s code has not been found.', $pickupLoggedInCart->channelCode())); + + $shopUser = $this->loggedInShopUserProvider->provide(); + $customer = $shopUser->getCustomer(); + + /** @var OrderInterface|null $cart */ + $cart = $this->cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel]); + Assert::notNull($cart, 'The cart was not created by the event listener'); + + $cart->setTokenValue($pickupLoggedInCart->orderToken()); + } +} diff --git a/src/Request/PickupLoggedInCartRequest.php b/src/Request/PickupLoggedInCartRequest.php new file mode 100644 index 000000000..3f68e2b96 --- /dev/null +++ b/src/Request/PickupLoggedInCartRequest.php @@ -0,0 +1,16 @@ +token, $this->channel); + } +} diff --git a/src/Resources/config/services/actions/cart.xml b/src/Resources/config/services/actions/cart.xml index 3ea87ffff..a09039a42 100644 --- a/src/Resources/config/services/actions/cart.xml +++ b/src/Resources/config/services/actions/cart.xml @@ -30,10 +30,11 @@ + diff --git a/src/Resources/config/services/handler/cart.xml b/src/Resources/config/services/handler/cart.xml index 8984a10be..03e43d0d4 100644 --- a/src/Resources/config/services/handler/cart.xml +++ b/src/Resources/config/services/handler/cart.xml @@ -15,10 +15,17 @@ - + + + + + + + From 164b3d205033d111fbe1a957f14cbde47b7bfaba Mon Sep 17 00:00:00 2001 From: mamazu Date: Fri, 22 Feb 2019 18:58:25 +0100 Subject: [PATCH 4/4] Reverted to simpler fix The easier way would be to fix the sylius cart context. For more information see (https://github.com/Sylius/ShopApiPlugin/pull/401) --- .../Cart/PickupLoggedInCartHandlerSpec.php | 84 ------------------- src/Command/PickupLoggedInCart.php | 9 -- src/Controller/Cart/PickupCartAction.php | 10 --- .../Cart/PickupLoggedInCartHandler.php | 51 ----------- src/Request/PickupLoggedInCartRequest.php | 16 ---- src/Resources/config/services.xml | 5 ++ .../config/services/actions/cart.xml | 1 - .../config/services/handler/cart.xml | 8 -- 8 files changed, 5 insertions(+), 179 deletions(-) delete mode 100644 spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php delete mode 100644 src/Command/PickupLoggedInCart.php delete mode 100644 src/Handler/Cart/PickupLoggedInCartHandler.php delete mode 100644 src/Request/PickupLoggedInCartRequest.php diff --git a/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php b/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php deleted file mode 100644 index c2c623210..000000000 --- a/spec/Handler/Cart/PickupLoggedInCartHandlerSpec.php +++ /dev/null @@ -1,84 +0,0 @@ -beConstructedWith( - $cartRepository, - $channelRepository, - $loggedInShopUserProvider - ); - } - - function it_is_initializable(): void - { - $this->shouldHaveType(PickupLoggedInCartHandler::class); - } - - function it_handles_cart_pickup_for_a_logged_in_user( - ChannelInterface $channel, - ChannelRepositoryInterface $channelRepository, - LoggedInShopUserProviderInterface $loggedInShopUserProvider, - ShopUserInterface $user, - CustomerInterface $customer, - OrderInterface $cart, - OrderRepositoryInterface $cartRepository - ): void { - $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); - - $loggedInShopUserProvider->provide()->willReturn($user); - $user->getCustomer()->willReturn($customer); - - $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn($cart); - $cart->setTokenValue('ORDERTOKEN')->shouldBeCalled(); - - $this->handle(new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')); - } - - function it_throws_an_exception_if_channel_is_not_found( - ChannelRepositoryInterface $channelRepository - ): void { - $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn(null); - - $this->shouldThrow(\InvalidArgumentException::class) - ->during('handle', [new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')]); - } - - function it_throws_an_exception_when_no_order_was_found( - ChannelInterface $channel, - ChannelRepositoryInterface $channelRepository, - LoggedInShopUserProviderInterface $loggedInShopUserProvider, - ShopUserInterface $user, - CustomerInterface $customer, - OrderRepositoryInterface $cartRepository - ): void { - $channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel); - - $loggedInShopUserProvider->provide()->willReturn($user); - $user->getCustomer()->willReturn($customer); - - $cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel])->willReturn(null); - - $this->shouldThrow(\InvalidArgumentException::class) - ->during('handle', [new PickupLoggedInCart('ORDERTOKEN', 'CHANNEL_CODE')]); - } -} diff --git a/src/Command/PickupLoggedInCart.php b/src/Command/PickupLoggedInCart.php deleted file mode 100644 index a7dbc47e4..000000000 --- a/src/Command/PickupLoggedInCart.php +++ /dev/null @@ -1,9 +0,0 @@ -validator = $validator; $this->validationErrorViewFactory = $validationErrorViewFactory; $this->cartQuery = $cartQuery; - $this->loggedInShopUserProvider = $loggedInShopUserProvider; } public function __invoke(Request $request): Response { $pickupRequest = new PickupCartRequest($request); - if ($this->loggedInShopUserProvider->isUserLoggedIn()) { - $pickupRequest = new PickupLoggedInCartRequest($request); - } $validationResults = $this->validator->validate($pickupRequest); diff --git a/src/Handler/Cart/PickupLoggedInCartHandler.php b/src/Handler/Cart/PickupLoggedInCartHandler.php deleted file mode 100644 index 06f6d70d6..000000000 --- a/src/Handler/Cart/PickupLoggedInCartHandler.php +++ /dev/null @@ -1,51 +0,0 @@ -cartRepository = $cartRepository; - $this->channelRepository = $channelRepository; - $this->loggedInShopUserProvider = $loggedInShopUserProvider; - } - - public function handle(PickupLoggedInCart $pickupLoggedInCart) - { - /** @var ChannelInterface|null $channel */ - $channel = $this->channelRepository->findOneByCode($pickupLoggedInCart->channelCode()); - Assert::notNull($channel, sprintf('Channel with %s code has not been found.', $pickupLoggedInCart->channelCode())); - - $shopUser = $this->loggedInShopUserProvider->provide(); - $customer = $shopUser->getCustomer(); - - /** @var OrderInterface|null $cart */ - $cart = $this->cartRepository->findOneBy(['customer' => $customer, 'channel' => $channel]); - Assert::notNull($cart, 'The cart was not created by the event listener'); - - $cart->setTokenValue($pickupLoggedInCart->orderToken()); - } -} diff --git a/src/Request/PickupLoggedInCartRequest.php b/src/Request/PickupLoggedInCartRequest.php deleted file mode 100644 index 3f68e2b96..000000000 --- a/src/Request/PickupLoggedInCartRequest.php +++ /dev/null @@ -1,16 +0,0 @@ -token, $this->channel); - } -} 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 a09039a42..205524c43 100644 --- a/src/Resources/config/services/actions/cart.xml +++ b/src/Resources/config/services/actions/cart.xml @@ -34,7 +34,6 @@ > - diff --git a/src/Resources/config/services/handler/cart.xml b/src/Resources/config/services/handler/cart.xml index 03e43d0d4..abe5c1697 100644 --- a/src/Resources/config/services/handler/cart.xml +++ b/src/Resources/config/services/handler/cart.xml @@ -18,14 +18,6 @@ - - - - - - -