Skip to content

Commit 79b324f

Browse files
committed
Test coverage of Adding configurable product to shopping cart #381
1 parent 16036cd commit 79b324f

File tree

1 file changed

+153
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)