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

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 Mar 1, 2019
1 parent b1c6bc2 commit 1b5991d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,27 @@ public function testAddProductIfQuantityIsNotAvailable()
$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
self::fail('Should be "The requested qty is not available" error message.');
}

/**
* @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);
self::fail('Should be "The most you may purchase is 5." error message.');
}

/**
Expand Down Expand Up @@ -108,7 +113,6 @@ public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int
}
) {
cart {
cart_id
items {
qty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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;

Expand All @@ -21,9 +21,9 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
private $quoteResource;

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

/**
* @var QuoteIdToMaskedQuoteIdInterface
Expand All @@ -37,29 +37,48 @@ 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);
}

/**
* @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()
public function testAddSimpleProductToCart()
{
$sku = 'simple';
$qty = 200;
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteId();

$this->quoteResource->load(
$this->quote,
'test_order_1',
'reserved_order_id'
);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);

$query = <<<QUERY
self::assertEquals($qty, $response['addSimpleProductsToCart']['cart']['items'][0]['qty']);
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
}

/**
* @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: {
Expand All @@ -77,12 +96,13 @@ public function testAddProductIfQuantityIsNotAvailable()
cart {
items {
qty
product {
sku
}
}
}
}
}
QUERY;

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

0 comments on commit 1b5991d

Please sign in to comment.