Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3acf405

Browse files
authored
ENGCOM-4273: Fix The requested qty is not available" should be received instead of Internal server error #368
2 parents 7afe974 + e3932f5 commit 3acf405

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ 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+
__(
83+
'Could not add the product with SKU %sku to the shopping cart: %message',
84+
['sku' => $sku, 'message' => $e->getMessage()]
85+
)
86+
);
87+
}
7988

8089
if (is_string($result)) {
8190
throw new GraphQlInputException(__($result));
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+
}

0 commit comments

Comments
 (0)