Skip to content

Commit e3b46cf

Browse files
authored
ENGCOM-4418: Test coverage of Adding configurable product to shopping cart #381 #403
2 parents 062662d + 6d8b831 commit e3b46cf

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 AddConfigurableProductToCartTest 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/multiple_mixed_products.php
46+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47+
*/
48+
public function testAddConfigurableProductToCart()
49+
{
50+
$variantSku = 'simple_41';
51+
$qty = 2;
52+
53+
$maskedQuoteId = $this->getMaskedQuoteId();
54+
55+
$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);
56+
57+
$response = $this->graphQlQuery($query);
58+
$cartItems = $response['addConfigurableProductsToCart']['cart']['items'];
59+
self::assertEquals($qty, $cartItems[0]['qty']);
60+
self::assertEquals($variantSku, $cartItems[0]['product']['sku']);
61+
}
62+
63+
64+
/**
65+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products.php
66+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
67+
* @expectedException \Exception
68+
* @expectedExceptionMessage The requested qty is not available
69+
*/
70+
public function testAddProductIfQuantityIsNotAvailable()
71+
{
72+
$variantSku = 'simple_41';
73+
$qty = 200;
74+
75+
$maskedQuoteId = $this->getMaskedQuoteId();
76+
$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);
77+
78+
$this->graphQlQuery($query);
79+
}
80+
81+
/**
82+
* @magentoApiDataFixture Magento/Framework/Search/_files/product_configurable.php
83+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
84+
* @expectedException \Exception
85+
* @expectedExceptionMessage Product that you are trying to add is not available.
86+
*/
87+
public function testAddOutOfStockProduct()
88+
{
89+
$variantSku = 'simple_1010';
90+
$qty = 1;
91+
$maskedQuoteId = $this->getMaskedQuoteId();
92+
$query = $this->getAddConfigurableProductMutationQuery($maskedQuoteId, $variantSku, $qty);
93+
94+
$this->graphQlQuery($query);
95+
}
96+
97+
/**
98+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
99+
* @return string
100+
* @throws \Magento\Framework\Exception\NoSuchEntityException
101+
*/
102+
private function getMaskedQuoteId()
103+
{
104+
$this->quoteResource->load(
105+
$this->quote,
106+
'test_order_1',
107+
'reserved_order_id'
108+
);
109+
return $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
110+
}
111+
112+
/**
113+
* @param string $maskedQuoteId
114+
* @param string $sku
115+
* @param int $qty
116+
*
117+
* @return string
118+
*/
119+
private function getAddConfigurableProductMutationQuery(string $maskedQuoteId, string $variantSku, int $qty) : string
120+
{
121+
return <<<QUERY
122+
mutation {
123+
addConfigurableProductsToCart(
124+
input: {
125+
cart_id: "{$maskedQuoteId}"
126+
cartItems: [
127+
{
128+
variant_sku: "{$variantSku}"
129+
data: {
130+
qty: {$qty}
131+
sku: "{$variantSku}"
132+
}
133+
}
134+
]
135+
}
136+
) {
137+
cart {
138+
items {
139+
id
140+
qty
141+
product {
142+
name
143+
sku
144+
}
145+
... on ConfigurableCartItem {
146+
configurable_options {
147+
option_label
148+
}
149+
}
150+
}
151+
}
152+
}
153+
}
154+
QUERY;
155+
}
156+
}

0 commit comments

Comments
 (0)