Skip to content

Commit

Permalink
Merge pull request #4115 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Apr 29, 2019
2 parents d1906d8 + 5899e77 commit 3ccf676
Show file tree
Hide file tree
Showing 18 changed files with 507 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function execute(QuoteAddress $address): array
}

$addressData = array_merge($addressData, [
'address_id' => $address->getId(),
'address_type' => $addressType,
'country' => [
'code' => $address->getCountryId(),
Expand Down
83 changes: 0 additions & 83 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@
*/
class SetShippingMethodsOnCart implements SetShippingMethodsOnCartInterface
{
/**
* @var GetQuoteAddress
*/
private $getQuoteAddress;

/**
* @var AssignShippingMethodToCart
*/
private $assignShippingMethodToCart;

/**
* @param GetQuoteAddress $getQuoteAddress
* @param AssignShippingMethodToCart $assignShippingMethodToCart
*/
public function __construct(
GetQuoteAddress $getQuoteAddress,
AssignShippingMethodToCart $assignShippingMethodToCart
) {
$this->getQuoteAddress = $getQuoteAddress;
$this->assignShippingMethodToCart = $assignShippingMethodToCart;
}

Expand All @@ -50,11 +42,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}
$shippingMethodInput = current($shippingMethodsInput);

if (!isset($shippingMethodInput['cart_address_id']) || empty($shippingMethodInput['cart_address_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing.'));
}
$cartAddressId = $shippingMethodInput['cart_address_id'];

if (!isset($shippingMethodInput['carrier_code']) || empty($shippingMethodInput['carrier_code'])) {
throw new GraphQlInputException(__('Required parameter "carrier_code" is missing.'));
}
Expand All @@ -65,7 +52,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}
$methodCode = $shippingMethodInput['method_code'];

$quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId());
$this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode);
$shippingAddress = $cart->getShippingAddress();
$this->assignShippingMethodToCart->execute($cart, $shippingAddress, $carrierCode, $methodCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,15 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}
$paymentMethodCode = $args['input']['payment_method']['code'];

$poNumber = isset($args['input']['payment_method']['purchase_order_number'])
&& empty($args['input']['payment_method']['purchase_order_number'])
? $args['input']['payment_method']['purchase_order_number']
: null;
$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
$additionalData = $args['input']['payment_method']['additional_data'] ?? [];

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
$payment = $this->paymentFactory->create([
'data' => [
PaymentInterface::KEY_METHOD => $paymentMethodCode,
PaymentInterface::KEY_PO_NUMBER => $poNumber,
PaymentInterface::KEY_ADDITIONAL_DATA => [],
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
]
]);

Expand Down
28 changes: 28 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Item;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
Expand Down Expand Up @@ -111,8 +112,35 @@ private function processCartItems(Quote $cart, array $items): void
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
} else {
$cartItem->setQty($qty);
$this->validateCartItem($cartItem);
$this->cartItemRepository->save($cartItem);
}
}
}

/**
* Validate cart item
*
* @param Item $cartItem
* @return void
* @throws GraphQlInputException
*/
private function validateCartItem(Item $cartItem): void
{
if ($cartItem->getHasError()) {
$errors = [];
foreach ($cartItem->getMessage(false) as $message) {
$errors[] = $message;
}

if (!empty($errors)) {
throw new GraphQlInputException(
__(
'Could not update the product with SKU %sku: %message',
['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))]
)
);
}
}
}
}
17 changes: 12 additions & 5 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ input SetShippingMethodsOnCartInput {
}

input ShippingMethodInput {
cart_address_id: Int!
carrier_code: String!
method_code: String!
}
Expand All @@ -131,9 +130,13 @@ input SetPaymentMethodOnCartInput {

input PaymentMethodInput {
code: String! @doc(description:"Payment method code")
additional_data: PaymentMethodAdditionalDataInput @doc(description:"Additional payment data")
purchase_order_number: String @doc(description:"Purchase order number")
}

input PaymentMethodAdditionalDataInput {
}

input SetGuestEmailOnCartInput {
cart_id: String!
email: String!
Expand Down Expand Up @@ -188,7 +191,6 @@ type Cart {
}

type CartAddress {
address_id: Int
firstname: String
lastname: String
company: String
Expand Down Expand Up @@ -231,13 +233,14 @@ type SelectedShippingMethod {
type AvailableShippingMethod {
carrier_code: String!
carrier_title: String!
method_code: String!
method_title: String!
method_code: String @doc(description: "Could be null if method is not available")
method_title: String @doc(description: "Could be null if method is not available")
error_message: String
amount: Float!
base_amount: Float!
base_amount: Float @doc(description: "Could be null if method is not available")
price_excl_tax: Float!
price_incl_tax: Float!
available: Boolean!
}

type AvailablePaymentMethod {
Expand All @@ -247,9 +250,13 @@ type AvailablePaymentMethod {

type SelectedPaymentMethod {
code: String @doc(description: "The payment method code")
additional_data: SelectedPaymentMethodAdditionalData @doc(description: "Additional payment data")
purchase_order_number: String @doc(description: "The purchase order number.")
}

type SelectedPaymentMethodAdditionalData {
}

enum AdressTypeEnum {
SHIPPING
BILLING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,31 @@ public function testAddSimpleProductToCartWithNegativeQty()
$this->graphQlMutation($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
*/
public function testAddProductIfQuantityIsDecimal()
{
$sku = 'simple_product';
$qty = 0.2;

$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $sku, $qty);

$this->expectExceptionMessage(
"Could not add the product with SKU {$sku} to the shopping cart: The fewest you may purchase is 1"
);
$this->graphQlMutation($query);
}

/**
* @param string $maskedQuoteId
* @param string $sku
* @param int $qty
* @param float $qty
* @return string
*/
private function getQuery(string $maskedQuoteId, string $sku, int $qty) : string
private function getQuery(string $maskedQuoteId, string $sku, float $qty) : string
{
return <<<QUERY
mutation {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\CatalogInventory;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for updating/removing shopping cart items
*/
class UpdateCartItemsTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @var GetQuoteItemIdByReservedQuoteIdAndSku
*/
private $getQuoteItemIdByReservedQuoteIdAndSku;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
GetQuoteItemIdByReservedQuoteIdAndSku::class
);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testUpdateCartItemDecimalQty()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');

$qty = 0.5;
$this->expectExceptionMessage(
"Could not update the product with SKU simple_product: The fewest you may purchase is 1"
);
$query = $this->getQuery($maskedQuoteId, $itemId, $qty);
$this->graphQlMutation($query);
}

/**
* @param string $maskedQuoteId
* @param int $itemId
* @param float $qty
* @return string
*/
private function getQuery(string $maskedQuoteId, int $itemId, float $qty): string
{
return <<<QUERY
mutation {
updateCartItems(input: {
cart_id: "{$maskedQuoteId}"
cart_items:[
{
cart_item_id: {$itemId}
quantity: {$qty}
}
]
}) {
cart {
items {
id
qty
}
}
}
}
QUERY;
}
}
Loading

0 comments on commit 3ccf676

Please sign in to comment.