From a3d809c4f9920888acadfc7f16849849041f4fb2 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Wed, 13 Feb 2019 15:01:04 +0200 Subject: [PATCH 1/4] The requested qty is not available" should be received instead of Internal server error --- .../Model/Resolver/AddSimpleProductsToCart.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php index f4335b262c85..481fb8a2839e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php @@ -79,7 +79,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $currentUserId = $context->getUserId(); $cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId); - $this->addProductsToCart->execute($cart, $cartItems); + try { + $this->addProductsToCart->execute($cart, $cartItems); + } catch (\Exception $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } + $cartData = $this->extractDataFromCart->execute($cart); return [ From 320848dd317d304f51ca725e03a88fbc2e6d87fd Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 14 Feb 2019 16:53:14 +0200 Subject: [PATCH 2/4] The requested qty is not available" should be received instead of Internal server error --- .../addConfigurableProductsToCartTest.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php new file mode 100644 index 000000000000..6629820766a1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php @@ -0,0 +1,112 @@ +quoteResource = $objectManager->create(QuoteResource::class); + $this->quote = $objectManager->create(Quote::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php + * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php + */ + public function testAddConfigurableProductsToCart() + { + $variantSku = 'simple_41'; + $qty = 200; + $expectedMessage = 'GraphQL response contains errors: The requested qty is not available +'; + $this->quoteResource->load( + $this->quote, + 'test_order_with_simple_product_without_address', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $query = $this->prepareAddConfigurableProductsToCartQuery($maskedQuoteId, $variantSku, $qty); + + try { + $this->graphQlQuery($query); + } catch (\Exception $exception) { + $this->assertEquals( + $expectedMessage, + $exception->getMessage() + ); + } + } + + /** + * @param string $maskedQuoteId + * @param string $variantSku + * @param int $qty + * + * @return string + */ + public function prepareAddConfigurableProductsToCartQuery(string $maskedQuoteId, string $variantSku, int $qty) + { + return << Date: Thu, 14 Feb 2019 16:58:42 -0600 Subject: [PATCH 3/4] GraphQL-350: "The requested qty is not available" should be received instead of Internal server error --- .../Model/Cart/AddSimpleProductToCart.php | 8 +- .../Resolver/AddSimpleProductsToCart.php | 7 +- .../Quote/AddSimpleProductToCartTest.php | 86 ++++++++++++++ .../addConfigurableProductsToCartTest.php | 112 ------------------ 4 files changed, 94 insertions(+), 119 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php delete mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 2303d2aa14f0..6c65e42c292f 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -75,7 +75,13 @@ public function execute(Quote $cart, array $cartItemData): void throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku])); } - $result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions)); + try { + $result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions)); + } catch (\Exception $e) { + throw new GraphQlInputException( + __('Shopping cart error with SKU \'%sku\': %message', ['sku' => $sku, 'message' => $e->getMessage()]) + ); + } if (is_string($result)) { throw new GraphQlInputException(__($result)); diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php index 481fb8a2839e..f4335b262c85 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php @@ -79,12 +79,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $currentUserId = $context->getUserId(); $cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId); - try { - $this->addProductsToCart->execute($cart, $cartItems); - } catch (\Exception $exception) { - throw new GraphQlInputException(__($exception->getMessage())); - } - + $this->addProductsToCart->execute($cart, $cartItems); $cartData = $this->extractDataFromCart->execute($cart); return [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php new file mode 100644 index 000000000000..4cbc614e1b8d --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php @@ -0,0 +1,86 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quote = $objectManager->create(Quote::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/products.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + * @expectedException \Exception + * @expectedExceptionMessage The requested qty is not available + */ + public function testAddProductIfQuantityIsNotAvailable() + { + $sku = 'simple'; + $qty = 200; + + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = <<graphQlQuery($query); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php deleted file mode 100644 index 6629820766a1..000000000000 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php +++ /dev/null @@ -1,112 +0,0 @@ -quoteResource = $objectManager->create(QuoteResource::class); - $this->quote = $objectManager->create(Quote::class); - $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); - } - - /** - * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php - * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php - */ - public function testAddConfigurableProductsToCart() - { - $variantSku = 'simple_41'; - $qty = 200; - $expectedMessage = 'GraphQL response contains errors: The requested qty is not available -'; - $this->quoteResource->load( - $this->quote, - 'test_order_with_simple_product_without_address', - 'reserved_order_id' - ); - $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); - $query = $this->prepareAddConfigurableProductsToCartQuery($maskedQuoteId, $variantSku, $qty); - - try { - $this->graphQlQuery($query); - } catch (\Exception $exception) { - $this->assertEquals( - $expectedMessage, - $exception->getMessage() - ); - } - } - - /** - * @param string $maskedQuoteId - * @param string $variantSku - * @param int $qty - * - * @return string - */ - public function prepareAddConfigurableProductsToCartQuery(string $maskedQuoteId, string $variantSku, int $qty) - { - return << Date: Thu, 14 Feb 2019 17:06:55 -0600 Subject: [PATCH 4/4] GraphQL-350: "The requested qty is not available" should be received instead of Internal server error --- .../QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 6c65e42c292f..1b32866ed883 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -79,7 +79,10 @@ public function execute(Quote $cart, array $cartItemData): void $result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions)); } catch (\Exception $e) { throw new GraphQlInputException( - __('Shopping cart error with SKU \'%sku\': %message', ['sku' => $sku, 'message' => $e->getMessage()]) + __( + 'Could not add the product with SKU %sku to the shopping cart: %message', + ['sku' => $sku, 'message' => $e->getMessage()] + ) ); }