Skip to content

Commit

Permalink
GraphQL-37: [Cart Operations] Manage Cart Items
Browse files Browse the repository at this point in the history
-- Refactoring
  • Loading branch information
naydav committed Mar 6, 2019
1 parent 3522857 commit 09a91f3
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
Expand All @@ -28,20 +28,20 @@ class RemoveItemFromCart implements ResolverInterface
private $getCartForUser;

/**
* @var GuestCartItemRepositoryInterface
* @var CartItemRepositoryInterface
*/
private $guestCartItemRepository;
private $cartItemRepository;

/**
* @param GetCartForUser $getCartForUser
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
* @param CartItemRepositoryInterface $cartItemRepository
*/
public function __construct(
GetCartForUser $getCartForUser,
GuestCartItemRepositoryInterface $guestCartItemRepository
CartItemRepositoryInterface $cartItemRepository
) {
$this->getCartForUser = $getCartForUser;
$this->guestCartItemRepository = $guestCartItemRepository;
$this->cartItemRepository = $cartItemRepository;
}

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

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

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

try {
$this->guestCartItemRepository->deleteById($maskedCartId, $itemId);
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public function __construct(
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
}
$maskedCartId = $args['input']['cart_id'];

if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
|| !is_array($args['input']['cart_items'])
) {
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
}
$cartItems = $args['input']['cart_items'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,50 @@ public function testRemoveItemFromAnotherCustomerCart()
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @param string $input
* @param string $message
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
*/
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
{
$query = <<<QUERY
mutation {
removeItemFromCart(
input: {
{$input}
}
) {
cart {
items {
qty
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @return array
*/
public function dataProviderUpdateWithMissedRequiredParameters(): array
{
return [
'missed_cart_id' => [
'cart_item_id: 1',
'Required parameter "cart_id" is missing.'
],
'missed_cart_item_id' => [
'cart_id: "test"',
'Required parameter "cart_item_id" is missing.'
],
];
}

/**
* @param string $maskedQuoteId
* @param int $itemId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,87 @@ public function testUpdateItemInAnotherCustomerCart()
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Required parameter "cart_id" is missing.
*/
public function testUpdateWithMissedCartItemId()
{
$query = <<<QUERY
mutation {
updateCartItems(input: {
cart_items: [
{
cart_item_id: 1
quantity: 2
}
]
}) {
cart {
items {
id
qty
}
}
}
}
QUERY;
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @param string $input
* @param string $message
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());

$query = <<<QUERY
mutation {
updateCartItems(input: {
cart_id: "{$maskedQuoteId}"
{$input}
}) {
cart {
items {
id
qty
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @return array
*/
public function dataProviderUpdateWithMissedRequiredParameters(): array
{
return [
'missed_cart_items' => [
'',
'Required parameter "cart_items" is missing.'
],
'missed_cart_item_id' => [
'cart_items: [{ quantity: 2 }]',
'Required parameter "cart_item_id" for "cart_items" is missing.'
],
'missed_cart_item_qty' => [
'cart_items: [{ cart_item_id: 1 }]',
'Required parameter "quantity" for "cart_items" is missing.'
],
];
}

/**
* @param string $maskedQuoteId
* @param int $itemId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ public function testRemoveItemFromCart()
}

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

$query = $this->prepareMutationQuery('non_existent_masked_id', $itemId);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -139,6 +143,49 @@ public function testRemoveItemFromCustomerCart()
$this->graphQlQuery($query);
}

/**
* @param string $input
* @param string $message
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
*/
public function testUpdateWithMissedItemRequiredParameters(string $input, string $message)
{
$query = <<<QUERY
mutation {
removeItemFromCart(
input: {
{$input}
}
) {
cart {
items {
qty
}
}
}
}
QUERY;
$this->expectExceptionMessage($message);
$this->graphQlQuery($query);
}

/**
* @return array
*/
public function dataProviderUpdateWithMissedRequiredParameters(): array
{
return [
'missed_cart_id' => [
'cart_item_id: 1',
'Required parameter "cart_id" is missing.'
],
'missed_cart_item_id' => [
'cart_id: "test"',
'Required parameter "cart_item_id" is missing.'
],
];
}

/**
* @param string $maskedQuoteId
* @param int $itemId
Expand Down
Loading

0 comments on commit 09a91f3

Please sign in to comment.