Skip to content

Commit 3cc39cd

Browse files
committed
GraphQL-350: "The requested qty is not available" should be received instead of Internal server error
1 parent 320848d commit 3cc39cd

File tree

4 files changed

+94
-119
lines changed

4 files changed

+94
-119
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ public function execute(Quote $cart, array $cartItemData): void
7575
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
7676
}
7777

78-
$result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
78+
try {
79+
$result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
80+
} catch (\Exception $e) {
81+
throw new GraphQlInputException(
82+
__('Shopping cart error with SKU \'%sku\': %message', ['sku' => $sku, 'message' => $e->getMessage()])
83+
);
84+
}
7985

8086
if (is_string($result)) {
8187
throw new GraphQlInputException(__($result));

app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7979
$currentUserId = $context->getUserId();
8080
$cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId);
8181

82-
try {
83-
$this->addProductsToCart->execute($cart, $cartItems);
84-
} catch (\Exception $exception) {
85-
throw new GraphQlInputException(__($exception->getMessage()));
86-
}
87-
82+
$this->addProductsToCart->execute($cart, $cartItems);
8883
$cartData = $this->extractDataFromCart->execute($cart);
8984

9085
return [
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\Quote\Model\Quote;
13+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
14+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
15+
16+
class AddSimpleProductToCartTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var QuoteResource
20+
*/
21+
private $quoteResource;
22+
23+
/**
24+
* @var Quote
25+
*/
26+
private $quote;
27+
28+
/**
29+
* @var QuoteIdToMaskedQuoteIdInterface
30+
*/
31+
private $quoteIdToMaskedId;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$objectManager = Bootstrap::getObjectManager();
39+
$this->quoteResource = $objectManager->get(QuoteResource::class);
40+
$this->quote = $objectManager->create(Quote::class);
41+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
42+
}
43+
44+
/**
45+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
46+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47+
* @expectedException \Exception
48+
* @expectedExceptionMessage The requested qty is not available
49+
*/
50+
public function testAddProductIfQuantityIsNotAvailable()
51+
{
52+
$sku = 'simple';
53+
$qty = 200;
54+
55+
$this->quoteResource->load(
56+
$this->quote,
57+
'test_order_1',
58+
'reserved_order_id'
59+
);
60+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
61+
62+
$query = <<<QUERY
63+
mutation {
64+
addSimpleProductsToCart(
65+
input: {
66+
cart_id: "{$maskedQuoteId}",
67+
cartItems: [
68+
{
69+
data: {
70+
qty: $qty
71+
sku: "$sku"
72+
}
73+
}
74+
]
75+
}
76+
) {
77+
cart {
78+
cart_id
79+
}
80+
}
81+
}
82+
QUERY;
83+
84+
$this->graphQlQuery($query);
85+
}
86+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/addConfigurableProductsToCartTest.php

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)