Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
ENGCOM-4273: Fix The requested qty is not available" should be receiv…
Browse files Browse the repository at this point in the history
…ed instead of Internal server error #368
  • Loading branch information
naydav authored Feb 14, 2019
2 parents 7afe974 + e3932f5 commit 3acf405
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ 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(
__(
'Could not add the product with SKU %sku to the shopping cart: %message',
['sku' => $sku, 'message' => $e->getMessage()]
)
);
}

if (is_string($result)) {
throw new GraphQlInputException(__($result));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;

class AddSimpleProductToCartTest extends GraphQlAbstract
{
/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @var Quote
*/
private $quote;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->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 = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cartItems: [
{
data: {
qty: $qty
sku: "$sku"
}
}
]
}
) {
cart {
cart_id
}
}
}
QUERY;

$this->graphQlQuery($query);
}
}

0 comments on commit 3acf405

Please sign in to comment.