From 75573a9e28cd3a893582572d08846b2d06437c47 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Wed, 13 Feb 2019 23:19:41 -0500 Subject: [PATCH] [Cart Operations] Remove item mutation Partial #37 --- .../Resolver/RemoveItemFromCartOutput.php | 73 ++++++ .../Magento/QuoteGraphQl/etc/schema.graphqls | 10 + .../GraphQl/Quote/RemoveItemFromCartTest.php | 227 ++++++++++++++++++ 3 files changed, 310 insertions(+) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCartOutput.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/RemoveItemFromCartTest.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCartOutput.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCartOutput.php new file mode 100644 index 0000000000000..658f670fd6825 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCartOutput.php @@ -0,0 +1,73 @@ +guestCartItemRepository = $guestCartItemRepository; + $this->guestCartRepository = $guestCartRepository; + $this->extractDataFromCart = $extractDataFromCart; + } + + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($args['input']['cart_id'])) { + throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); + } + if (!isset($args['input']['cart_item_id'])) { + throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing')); + } + $maskedCartId = $args['input']['cart_id']; + $itemId = $args['input']['cart_item_id']; + + try { + $this->guestCartItemRepository->deleteById($maskedCartId, $itemId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage())); + } + + $cart = $this->guestCartRepository->get($maskedCartId); + + $cartData = $this->extractDataFromCart->execute($cart); + + return ['cart' => array_merge(['cart_id' => $maskedCartId], $cartData)]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 2050a2d3ca790..b2e338bf4edf5 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -14,6 +14,7 @@ type Mutation { setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingAddressesOnCart") setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart") setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart") + removeItemFromCart(input: RemoveItemFromCartInput): RemoveItemFromCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\RemoveItemFromCartOutput") } input AddSimpleProductsToCartInput { @@ -206,6 +207,15 @@ type AddVirtualProductsToCartOutput { cart: Cart! } +input RemoveItemFromCartInput { + cart_id: String! + cart_item_id: String! +} + +type RemoveItemFromCartOutput { + cart: Cart! +} + type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") { customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions") } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/RemoveItemFromCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/RemoveItemFromCartTest.php new file mode 100644 index 0000000000000..9a949af5bc1e1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/RemoveItemFromCartTest.php @@ -0,0 +1,227 @@ +quoteResource = $objectManager->create(QuoteResource::class); + $this->quote = $objectManager->create(Quote::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + $this->productRepository = $objectManager->create(ProductRepositoryInterface::class); + $this->guestCartRepository = $objectManager->create(GuestCartRepositoryInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testGuestRemoveItemFromCart() + { + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $itemId = $this->quote->getItemByProduct($this->productRepository->get('simple'))->getId(); + + $query = <<graphQlQuery($query); + + $this->assertArrayHasKey('removeItemFromCart', $response); + $this->assertArrayHasKey('cart', $response['removeItemFromCart']); + + $responseCart = $response['removeItemFromCart']['cart']; + + $this->assertCount(0, $responseCart['items']); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ + public function testRemoveItemFromCart() + { + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $itemId = $this->quote->getItemByProduct($this->productRepository->get('simple'))->getId(); + + $this->quote->setCustomerId(1); + $this->quoteResource->save($this->quote); + + $headerMap = $this->getHeaderMap(); + + $query = <<graphQlQuery($query, [], '', $headerMap); + + $this->assertArrayHasKey('removeItemFromCart', $response); + $this->assertArrayHasKey('cart', $response['removeItemFromCart']); + + $responseCart = $response['removeItemFromCart']['cart']; + + $this->assertCount(0, $responseCart['items']); + } + + public function testRemoveItemFromCartNoSuchCartIdException() + { + $maskedCartId = 'nada'; + + $this->expectExceptionMessage('No such entity with cartId'); + + $query = <<graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + */ + public function testRemoveItemFromCartNoSuchCartItem() + { + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $itemId = 'nononono'; + + $this->expectExceptionMessage(sprintf('Cart doesn\'t contain the %s item.', $itemId)); + + $query = <<graphQlQuery($query); + } + + /** + * @param string $username + * @return array + */ + private function getHeaderMap(string $username = 'customer@example.com'): array + { + $password = 'password'; + /** @var CustomerTokenServiceInterface $customerTokenService */ + $customerTokenService = ObjectManager::getInstance() + ->get(CustomerTokenServiceInterface::class); + $customerToken = $customerTokenService->createCustomerAccessToken($username, $password); + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; + return $headerMap; + } +}