Skip to content

Commit 86151ce

Browse files
committed
#813: [Test coverage] Update item qty for configurable product
1 parent deef245 commit 86151ce

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\GraphQl\ConfigurableProduct;
10+
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13+
use Magento\Framework\Exception\NoSuchEntityException as NoSuchEntityException;
14+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
15+
use Magento\Quote\Model\Quote\Item;
16+
use Magento\Quote\Model\QuoteFactory;
17+
use Magento\Quote\Model\QuoteIdMaskFactory;
18+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\TestCase\GraphQlAbstract;
21+
22+
/**
23+
* checks that qty of configurable product is updated in cart
24+
*/
25+
class UpdateConfigurableCartItemsTest extends GraphQlAbstract
26+
{
27+
/**
28+
* @var QuoteIdMaskFactory
29+
*/
30+
protected $quoteIdMaskFactory;
31+
32+
/**
33+
* @var GetMaskedQuoteIdByReservedOrderId
34+
*/
35+
private $getMaskedQuoteIdByReservedOrderId;
36+
37+
/**
38+
* @var ProductRepositoryInterface
39+
*/
40+
private $productRepository;
41+
42+
/**
43+
* @var QuoteFactory
44+
*/
45+
private $quoteFactory;
46+
47+
/**
48+
* @var QuoteResource
49+
*/
50+
private $quoteResource;
51+
52+
/**
53+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
54+
*/
55+
public function testUpdateConfigurableCartItemQuantity()
56+
{
57+
$reservedOrderId = 'test_cart_with_configurable';
58+
$maskedQuoteId = $this->getMaskedQuoteId($reservedOrderId);
59+
$productSku = 'simple_10';
60+
$newQuantity = 123;
61+
$quoteItem = $this->getQuoteItemBySku($productSku, $reservedOrderId);
62+
63+
$query = $this->getQuery($maskedQuoteId, (int)$quoteItem->getId(), $newQuantity);
64+
$response = $this->graphQlMutation($query);
65+
66+
self::assertArrayHasKey('updateCartItems', $response);
67+
self::assertArrayHasKey('quantity', $response['updateCartItems']['cart']['items']['0']);
68+
self::assertEquals($newQuantity, $response['updateCartItems']['cart']['items']['0']['quantity']);
69+
}
70+
71+
/**
72+
* @inheritdoc
73+
*/
74+
protected function setUp()
75+
{
76+
$objectManager = Bootstrap::getObjectManager();
77+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
78+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
79+
$this->quoteResource = $objectManager->get(QuoteResource::class);
80+
$this->productRepository = $objectManager->get(ProductRepositoryInterface::class);
81+
$this->quoteIdMaskFactory = Bootstrap::getObjectManager()->get(QuoteIdMaskFactory::class);
82+
}
83+
84+
/**
85+
* @param string $maskedQuoteId
86+
* @param int $quoteItemId
87+
* @param int $newQuantity
88+
* @return string
89+
*/
90+
private function getQuery(string $maskedQuoteId, int $quoteItemId, int $newQuantity): string
91+
{
92+
return <<<QUERY
93+
mutation {
94+
updateCartItems(input: {
95+
cart_id:"$maskedQuoteId"
96+
cart_items: [
97+
{
98+
cart_item_id: $quoteItemId
99+
quantity: $newQuantity
100+
}
101+
]
102+
}) {
103+
cart {
104+
items {
105+
quantity
106+
}
107+
}
108+
}
109+
}
110+
QUERY;
111+
}
112+
113+
/**
114+
* Returns quote item by product SKU
115+
*
116+
* @param string $sku
117+
* @return Item|bool
118+
* @throws NoSuchEntityException
119+
*/
120+
private function getQuoteItemBySku(string $sku, string $reservedOrderId)
121+
{
122+
$quote = $this->quoteFactory->create();
123+
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');
124+
$item = false;
125+
foreach ($quote->getAllItems() as $quoteItem) {
126+
if ($quoteItem->getSku() == $sku && $quoteItem->getProductType() == Configurable::TYPE_CODE &&
127+
!$quoteItem->getParentItemId()) {
128+
$item = $quoteItem;
129+
break;
130+
}
131+
}
132+
133+
return $item;
134+
}
135+
136+
/**
137+
* @param $reservedOrderId
138+
* @return string
139+
* @throws NoSuchEntityException
140+
*/
141+
private function getMaskedQuoteId(string $reservedOrderId): string
142+
{
143+
$quote = $this->quoteFactory->create();
144+
$this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id');
145+
$quoteIdMask = $this->quoteIdMaskFactory->create();
146+
$quoteIdMask->setQuoteId($quote->getId())
147+
->save();
148+
149+
return $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
150+
}
151+
}

0 commit comments

Comments
 (0)