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

[Test coverage] add to cart simple/virtual products with custom options #395

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,27 @@

namespace Magento\GraphQl\CatalogInventory;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
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;

/**
* Add simple product to cart testcases related to inventory
*/
class AddProductToCartTest extends GraphQlAbstract
{
/**
* @var QuoteResource
*/
private $quoteResource;

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

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

/**
* @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);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
Expand All @@ -51,11 +40,10 @@ public function testAddProductIfQuantityIsNotAvailable()
{
$sku = 'simple';
$qty = 200;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
self::fail('Should be "The requested qty is not available" error message.');
}

/**
Expand All @@ -71,22 +59,26 @@ public function testAddMoreProductsThatAllowed()

$sku = 'custom-design-simple-product';
$qty = 7;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
self::fail('Should be "The most you may purchase is 5." error message.');
}

/**
* @return string
* @magentoApiDataFixture Magento/Catalog/_files/products.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @expectedException \Exception
* @expectedExceptionMessage Please enter a number greater than 0 in this field.
*/
public function getMaskedQuoteId() : string
public function testAddSimpleProductToCartWithNegativeQty()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$sku = 'simple';
$qty = -2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
$this->graphQlQuery($query);
}

/**
Expand All @@ -95,7 +87,7 @@ public function getMaskedQuoteId() : string
* @param int $qty
* @return string
*/
public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string
private function getQuery(string $maskedQuoteId, string $sku, int $qty) : string
{
return <<<QUERY
mutation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,29 @@
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;
namespace Magento\GraphQl\ConfigurableProduct;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
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;

/**
* Add configurable product to cart testcases
*/
class AddConfigurableProductToCartTest extends GraphQlAbstract
{
/**
* @var QuoteResource
*/
private $quoteResource;

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

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

/**
* @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);
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
Expand All @@ -49,12 +38,11 @@ public function testAddConfigurableProductToCart()
{
$variantSku = 'simple_41';
$qty = 2;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$maskedQuoteId = $this->getMaskedQuoteId();

$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);

$query = $this->getQuery($maskedQuoteId, $variantSku, $qty);
$response = $this->graphQlQuery($query);

$cartItems = $response['addConfigurableProductsToCart']['cart']['items'];
self::assertEquals($qty, $cartItems[0]['qty']);
self::assertEquals($variantSku, $cartItems[0]['product']['sku']);
Expand All @@ -70,10 +58,9 @@ public function testAddProductIfQuantityIsNotAvailable()
{
$variantSku = 'simple_41';
$qty = 200;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);

$query = $this->getQuery($maskedQuoteId, $variantSku, $qty);
$this->graphQlQuery($query);
}

Expand All @@ -87,35 +74,19 @@ public function testAddOutOfStockProduct()
{
$variantSku = 'simple_1010';
$qty = 1;
$maskedQuoteId = $this->getMaskedQuoteId();
$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

$query = $this->getQuery($maskedQuoteId, $variantSku, $qty);
$this->graphQlQuery($query);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getMaskedQuoteId()
{
$this->quoteResource->load(
$this->quote,
'test_order_1',
'reserved_order_id'
);
return $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
}

/**
* @param string $maskedQuoteId
* @param string $sku
* @param string $variantSku
* @param int $qty
*
* @return string
*/
private function getAddConfigurableProductMutationQuery(string $maskedQuoteId, string $variantSku, int $qty): string
private function getQuery(string $maskedQuoteId, string $variantSku, int $qty): string
{
return <<<QUERY
mutation {
Expand Down

This file was deleted.

Loading