Skip to content

Commit

Permalink
GraphQL-293: When maxSaleQty is set and qty is more than maxSaleQty t…
Browse files Browse the repository at this point in the history
…he "The most you may purchase is %1." message should be sent instead of "Internal server error"
  • Loading branch information
naydav committed Feb 27, 2019
1 parent 373adf2 commit 4db921f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ public function __construct(
* @param array $cartItems
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $cartItems): void
{
foreach ($cartItems as $cartItemData) {
$this->addProductToCart->execute($cart, $cartItemData);
}

if ($cart->getData('has_error')) {
throw new GraphQlInputException(
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
);
}

$this->cartRepository->save($cart);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\CatalogInventory;

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

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

/**
* @var QuoteFactory
*/
private $quoteFactory;

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

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::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;

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5
* @expectedException \Exception
* @expectedExceptionMessage The most you may purchase is 5.
*/
public function testAddMoreProductsThatAllowed()
{
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');

$sku = 'custom-design-simple-product';
$qty = 7;

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
}

/**
* @return string
*/
public function getMaskedQuoteId() : string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $maskedQuoteId
* @param string $sku
* @param int $qty
* @return string
*/
public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string
{
return <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cartItems: [
{
data: {
qty: $qty
sku: "$sku"
}
}
]
}
) {
cart {
cart_id
items {
qty
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

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

class AddSimpleProductToCartTest extends GraphQlAbstract
{
Expand All @@ -22,30 +21,24 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
private $quoteResource;

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

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

/**
* @var Config
*/
private $config;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quote = $objectManager->create(Quote::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->config = $objectManager->get(Config::class);
}

/**
Expand All @@ -57,68 +50,33 @@ public function testAddSimpleProductsToCart()
$sku = 'simple';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);

$query = $this->geAddSimpleProducttQuery($maskedQuoteId, $sku, $qty);
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
$cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty'];

$this->assertEquals($qty, $cartQty);
}

/**
* @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;

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @expectedExceptionMessage The most you may purchase is 5.
*/
public function testAddMoreProductsThatAllowed()
{
$sku = 'custom-design-simple-product';
$qty = 7;
$maxQty = 5;

$this->config->saveConfig('cataloginventory/item_options/max_sale_qty', $maxQty, 'default', 0);
$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
$cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty'];
self::assertEquals($qty, $cartQty);
}

/**
* @return string
*/
public function getMaskedQuoteId() : string
{
$this->quoteResource->load(
$this->quote,
'test_order_1',
'reserved_order_id'
);
return $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $maskedQuoteId
* @param string $sku
* @param int $qty
*
* @return string
*/
public function getQueryAddSimpleProduct(string $maskedQuoteId, string $sku, int $qty) : string
public function geAddSimpleProducttQuery(string $maskedQuoteId, string $sku, int $qty) : string
{
return <<<QUERY
mutation {
Expand Down

0 comments on commit 4db921f

Please sign in to comment.