Skip to content

Commit 09a91f3

Browse files
committed
GraphQL-37: [Cart Operations] Manage Cart Items
-- Refactoring
1 parent 3522857 commit 09a91f3

File tree

6 files changed

+462
-12
lines changed

6 files changed

+462
-12
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
17+
use Magento\Quote\Api\CartItemRepositoryInterface;
1818
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1919

2020
/**
@@ -28,20 +28,20 @@ class RemoveItemFromCart implements ResolverInterface
2828
private $getCartForUser;
2929

3030
/**
31-
* @var GuestCartItemRepositoryInterface
31+
* @var CartItemRepositoryInterface
3232
*/
33-
private $guestCartItemRepository;
33+
private $cartItemRepository;
3434

3535
/**
3636
* @param GetCartForUser $getCartForUser
37-
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
37+
* @param CartItemRepositoryInterface $cartItemRepository
3838
*/
3939
public function __construct(
4040
GetCartForUser $getCartForUser,
41-
GuestCartItemRepositoryInterface $guestCartItemRepository
41+
CartItemRepositoryInterface $cartItemRepository
4242
) {
4343
$this->getCartForUser = $getCartForUser;
44-
$this->guestCartItemRepository = $guestCartItemRepository;
44+
$this->cartItemRepository = $cartItemRepository;
4545
}
4646

4747
/**
@@ -50,19 +50,19 @@ public function __construct(
5050
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5151
{
5252
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
53-
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
53+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
5454
}
5555
$maskedCartId = $args['input']['cart_id'];
5656

5757
if (!isset($args['input']['cart_item_id']) || empty($args['input']['cart_item_id'])) {
58-
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing'));
58+
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing.'));
5959
}
6060
$itemId = $args['input']['cart_item_id'];
6161

6262
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6363

6464
try {
65-
$this->guestCartItemRepository->deleteById($maskedCartId, $itemId);
65+
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
6666
} catch (NoSuchEntityException $e) {
6767
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
6868
} catch (LocalizedException $e) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public function __construct(
5151
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5252
{
5353
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
54-
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
54+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
5555
}
5656
$maskedCartId = $args['input']['cart_id'];
5757

5858
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
5959
|| !is_array($args['input']['cart_items'])
6060
) {
61-
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
61+
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
6262
}
6363
$cartItems = $args['input']['cart_items'];
6464

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,50 @@ public function testRemoveItemFromAnotherCustomerCart()
187187
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
188188
}
189189

190+
/**
191+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
192+
* @param string $input
193+
* @param string $message
194+
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
195+
*/
196+
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
197+
{
198+
$query = <<<QUERY
199+
mutation {
200+
removeItemFromCart(
201+
input: {
202+
{$input}
203+
}
204+
) {
205+
cart {
206+
items {
207+
qty
208+
}
209+
}
210+
}
211+
}
212+
QUERY;
213+
$this->expectExceptionMessage($message);
214+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
215+
}
216+
217+
/**
218+
* @return array
219+
*/
220+
public function dataProviderUpdateWithMissedRequiredParameters(): array
221+
{
222+
return [
223+
'missed_cart_id' => [
224+
'cart_item_id: 1',
225+
'Required parameter "cart_id" is missing.'
226+
],
227+
'missed_cart_item_id' => [
228+
'cart_id: "test"',
229+
'Required parameter "cart_item_id" is missing.'
230+
],
231+
];
232+
}
233+
190234
/**
191235
* @param string $maskedQuoteId
192236
* @param int $itemId

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,87 @@ public function testUpdateItemInAnotherCustomerCart()
214214
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
215215
}
216216

217+
/**
218+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
219+
* @expectedException \Exception
220+
* @expectedExceptionMessage Required parameter "cart_id" is missing.
221+
*/
222+
public function testUpdateWithMissedCartItemId()
223+
{
224+
$query = <<<QUERY
225+
mutation {
226+
updateCartItems(input: {
227+
cart_items: [
228+
{
229+
cart_item_id: 1
230+
quantity: 2
231+
}
232+
]
233+
}) {
234+
cart {
235+
items {
236+
id
237+
qty
238+
}
239+
}
240+
}
241+
}
242+
QUERY;
243+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
244+
}
245+
246+
/**
247+
* @param string $input
248+
* @param string $message
249+
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
250+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
251+
*/
252+
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
253+
{
254+
$quote = $this->quoteFactory->create();
255+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
256+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
257+
258+
$query = <<<QUERY
259+
mutation {
260+
updateCartItems(input: {
261+
cart_id: "{$maskedQuoteId}"
262+
{$input}
263+
}) {
264+
cart {
265+
items {
266+
id
267+
qty
268+
}
269+
}
270+
}
271+
}
272+
QUERY;
273+
$this->expectExceptionMessage($message);
274+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
275+
}
276+
277+
/**
278+
* @return array
279+
*/
280+
public function dataProviderUpdateWithMissedRequiredParameters(): array
281+
{
282+
return [
283+
'missed_cart_items' => [
284+
'',
285+
'Required parameter "cart_items" is missing.'
286+
],
287+
'missed_cart_item_id' => [
288+
'cart_items: [{ quantity: 2 }]',
289+
'Required parameter "cart_item_id" for "cart_items" is missing.'
290+
],
291+
'missed_cart_item_qty' => [
292+
'cart_items: [{ cart_item_id: 1 }]',
293+
'Required parameter "quantity" for "cart_items" is missing.'
294+
],
295+
];
296+
}
297+
217298
/**
218299
* @param string $maskedQuoteId
219300
* @param int $itemId

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ public function testRemoveItemFromCart()
6767
}
6868

6969
/**
70+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
7071
* @expectedException \Exception
7172
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
7273
*/
7374
public function testRemoveItemFromNonExistentCart()
7475
{
75-
$query = $this->prepareMutationQuery('non_existent_masked_id', 1);
76+
$quote = $this->quoteFactory->create();
77+
$this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id');
78+
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
7679

80+
$query = $this->prepareMutationQuery('non_existent_masked_id', $itemId);
7781
$this->graphQlQuery($query);
7882
}
7983

@@ -139,6 +143,49 @@ public function testRemoveItemFromCustomerCart()
139143
$this->graphQlQuery($query);
140144
}
141145

146+
/**
147+
* @param string $input
148+
* @param string $message
149+
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
150+
*/
151+
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
152+
{
153+
$query = <<<QUERY
154+
mutation {
155+
removeItemFromCart(
156+
input: {
157+
{$input}
158+
}
159+
) {
160+
cart {
161+
items {
162+
qty
163+
}
164+
}
165+
}
166+
}
167+
QUERY;
168+
$this->expectExceptionMessage($message);
169+
$this->graphQlQuery($query);
170+
}
171+
172+
/**
173+
* @return array
174+
*/
175+
public function dataProviderUpdateWithMissedRequiredParameters(): array
176+
{
177+
return [
178+
'missed_cart_id' => [
179+
'cart_item_id: 1',
180+
'Required parameter "cart_id" is missing.'
181+
],
182+
'missed_cart_item_id' => [
183+
'cart_id: "test"',
184+
'Required parameter "cart_item_id" is missing.'
185+
],
186+
];
187+
}
188+
142189
/**
143190
* @param string $maskedQuoteId
144191
* @param int $itemId

0 commit comments

Comments
 (0)