Skip to content

Commit 91251ea

Browse files
committed
Fixed tests, decoupled logic for updating items
1 parent f81a633 commit 91251ea

File tree

5 files changed

+87
-46
lines changed

5 files changed

+87
-46
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,48 @@
1010
use Magento\Framework\DataObject;
1111
use Magento\Framework\DataObjectFactory;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1314
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
16+
use Magento\Quote\Api\CartItemRepositoryInterface;
17+
use Magento\Quote\Api\CartRepositoryInterface;
1418
use Magento\Quote\Model\Quote;
19+
use Magento\Quote\Model\Quote\Item;
1520

1621
/**
1722
* Update cart item
1823
*
1924
*/
2025
class UpdateCartItem
2126
{
27+
/**
28+
* @var CartRepositoryInterface
29+
*/
30+
private $quoteRepository;
31+
32+
/**
33+
* @var CartItemRepositoryInterface
34+
*/
35+
private $cartItemRepository;
36+
2237
/**
2338
* @var DataObjectFactory
2439
*/
2540
private $dataObjectFactory;
2641

2742
/**
2843
* @param DataObjectFactory $dataObjectFactory
44+
* @param CartItemRepositoryInterface $cartItemRepository
45+
* @param CartRepositoryInterface $quoteRepository
2946
*/
3047
public function __construct(
31-
DataObjectFactory $dataObjectFactory
48+
DataObjectFactory $dataObjectFactory,
49+
CartItemRepositoryInterface $cartItemRepository,
50+
CartRepositoryInterface $quoteRepository
3251
) {
3352
$this->dataObjectFactory = $dataObjectFactory;
53+
$this->cartItemRepository = $cartItemRepository;
54+
$this->quoteRepository = $quoteRepository;
3455
}
3556

3657
/**
@@ -39,12 +60,20 @@ public function __construct(
3960
* @param Quote $cart
4061
* @param int $cartItemId
4162
* @param float $qty
42-
* @param null $customizableOptionsData
63+
* @param array $customizableOptionsData
4364
* @return void
4465
* @throws GraphQlInputException
66+
* @throws GraphQlNoSuchEntityException
67+
* @throws NoSuchEntityException
4568
*/
4669
public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void
4770
{
71+
if (count($customizableOptionsData) === 0) { // Update only item's qty
72+
$this->updateItemQty($cartItemId, $cart, $qty);
73+
74+
return;
75+
}
76+
4877
$customizableOptions = [];
4978
foreach ($customizableOptionsData as $customizableOption) {
5079
$customizableOptions[$customizableOption['id']] = $customizableOption['value_string'];
@@ -77,6 +106,56 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi
77106
['message' => $result->getMessage(true)]
78107
));
79108
}
109+
110+
$this->quoteRepository->save($cart);
111+
}
112+
113+
/**
114+
* Updates item qty for the specified cart
115+
*
116+
* @param int $itemId
117+
* @param Quote $cart
118+
* @param float $qty
119+
* @throws GraphQlNoSuchEntityException
120+
* @throws NoSuchEntityException
121+
* @throws GraphQlNoSuchEntityException
122+
*/
123+
private function updateItemQty(int $itemId, Quote $cart, float $qty)
124+
{
125+
$cartItem = $cart->getItemById($itemId);
126+
if ($cartItem === false) {
127+
throw new GraphQlNoSuchEntityException(
128+
__('Could not find cart item with id: %1.', $itemId)
129+
);
130+
}
131+
$cartItem->setQty($qty);
132+
$this->validateCartItem($cartItem);
133+
$this->cartItemRepository->save($cartItem);
134+
}
135+
136+
/**
137+
* Validate cart item
138+
*
139+
* @param Item $cartItem
140+
* @return void
141+
* @throws GraphQlInputException
142+
*/
143+
private function validateCartItem(Item $cartItem): void
144+
{
145+
if ($cartItem->getHasError()) {
146+
$errors = [];
147+
foreach ($cartItem->getMessage(false) as $message) {
148+
$errors[] = $message;
149+
}
150+
if (!empty($errors)) {
151+
throw new GraphQlInputException(
152+
__(
153+
'Could not update the product with SKU %sku: %message',
154+
['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))]
155+
)
156+
);
157+
}
158+
}
80159
}
81160

82161
/**

app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\Quote\Api\CartItemRepositoryInterface;
18-
use Magento\Quote\Api\CartRepositoryInterface;
1918
use Magento\Quote\Model\Quote;
2019
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2120
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
@@ -25,11 +24,6 @@
2524
*/
2625
class UpdateCartItems implements ResolverInterface
2726
{
28-
/**
29-
* @var CartRepositoryInterface
30-
*/
31-
private $quoteRepository;
32-
3327
/**
3428
* @var UpdateCartItem
3529
*/
@@ -49,18 +43,15 @@ class UpdateCartItems implements ResolverInterface
4943
* @param GetCartForUser $getCartForUser
5044
* @param CartItemRepositoryInterface $cartItemRepository
5145
* @param UpdateCartItem $updateCartItem
52-
* @param CartRepositoryInterface $quoteRepository
5346
*/
5447
public function __construct(
5548
GetCartForUser $getCartForUser,
5649
CartItemRepositoryInterface $cartItemRepository,
57-
UpdateCartItem $updateCartItem,
58-
CartRepositoryInterface $quoteRepository
50+
UpdateCartItem $updateCartItem
5951
) {
6052
$this->getCartForUser = $getCartForUser;
6153
$this->cartItemRepository = $cartItemRepository;
6254
$this->updateCartItem = $updateCartItem;
63-
$this->quoteRepository = $quoteRepository;
6455
}
6556

6657
/**
@@ -112,46 +103,18 @@ private function processCartItems(Quote $cart, array $items): void
112103
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
113104
}
114105
$itemId = (int)$item['cart_item_id'];
106+
$customizableOptions = $item['customizable_options'] ?? [];
115107

116-
if (!isset($item['quantity'])) {
108+
if (count($customizableOptions) === 0 && !isset($item['quantity'])) {
117109
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
118110
}
119111
$quantity = (float)$item['quantity'];
120112

121113
if ($quantity <= 0.0) {
122114
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
123115
} else {
124-
$customizableOptions = $item['customizable_options'] ?? null;
125-
126-
if ($customizableOptions === null) { // Update only item's qty
127-
$this->updateItemQty($itemId, $cart, $quantity);
128-
} else { // Update customizable options (and QTY if changed)
129-
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
130-
$this->quoteRepository->save($cart);
131-
}
116+
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
132117
}
133118
}
134119
}
135-
136-
/**
137-
* Updates item qty for the specified cart
138-
*
139-
* @param int $itemId
140-
* @param Quote $cart
141-
* @param float $qty
142-
* @throws GraphQlNoSuchEntityException
143-
* @throws NoSuchEntityException
144-
* @throws GraphQlNoSuchEntityException
145-
*/
146-
private function updateItemQty(int $itemId, Quote $cart, float $qty)
147-
{
148-
$cartItem = $cart->getItemById($itemId);
149-
if ($cartItem === false) {
150-
throw new GraphQlNoSuchEntityException(
151-
__('Could not find cart item with id: %1.', $itemId)
152-
);
153-
}
154-
$cartItem->setQty($qty);
155-
$this->cartItemRepository->save($cartItem);
156-
}
157120
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/UpdateCartItemsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
285285
],
286286
'missed_cart_item_qty' => [
287287
'cart_items: [{ cart_item_id: 1 }]',
288-
'Field CartItemUpdateInput.quantity of required type Float! was not provided.'
288+
'Required parameter "quantity" for "cart_items" is missing.'
289289
],
290290
];
291291
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public function testOptionsSetChangedOnChangeOneOption()
159159
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php
160160
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
161161
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php
162-
* @group recent
163162
*/
164163
public function testOptionSetPersistsOnExtraOptionWithIncorrectId()
165164
{

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
236236
],
237237
'missed_cart_item_qty' => [
238238
'cart_items: [{ cart_item_id: 1 }]',
239-
'Field CartItemUpdateInput.quantity of required type Float! was not provided.'
239+
'Required parameter "quantity" for "cart_items" is missing.'
240240
],
241241
];
242242
}

0 commit comments

Comments
 (0)