Skip to content

Commit

Permalink
ENGCOM-4612: Add API Test For Selected Shipping Method functionality #…
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav authored Mar 29, 2019
2 parents a9a72b6 + 261a7e2 commit 419106a
Show file tree
Hide file tree
Showing 18 changed files with 893 additions and 118 deletions.
21 changes: 11 additions & 10 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;

/**
Expand Down Expand Up @@ -44,14 +44,14 @@ public function __construct(
/**
* Get quote address
*
* @param CartInterface $cart
* @param int $quoteAddressId
* @param int|null $customerId
* @return AddressInterface
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
public function execute(CartInterface $cart, int $quoteAddressId, ?int $customerId): AddressInterface
{
$quoteAddress = $this->quoteAddressFactory->create();

Expand All @@ -62,14 +62,15 @@ public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
);
}

$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();

/* Guest cart, allow operations */
if (!$quoteAddressCustomerId && null === $customerId) {
return $quoteAddress;
// TODO: GetQuoteAddress::execute should depend only on AddressInterface contract
// https://github.com/magento/graphql-ce/issues/550
if ($quoteAddress->getQuoteId() !== $cart->getId()) {
throw new GraphQlNoSuchEntityException(
__('Cart does not contain address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
);
}

if ($quoteAddressCustomerId !== $customerId) {
if ((int)$quoteAddress->getCustomerId() !== (int)$customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot use cart address with ID "%cart_address_id"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}
$methodCode = $shippingMethodInput['method_code'];

$quoteAddress = $this->getQuoteAddress->execute($cartAddressId, $context->getUserId());

$quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId());
$this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class GetSelectedShippingMethodTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = $this->getQuery($maskedQuoteId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);

$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);

self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);

self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
*/
public function testGetSelectedShippingMethodFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);

$shippingAddress = current($response['cart']['shipping_addresses']);
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);

self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);
self::assertNull($shippingAddress['selected_shipping_method']['label']);
self::assertNull($shippingAddress['selected_shipping_method']['amount']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testGetGetSelectedShippingMethodOfNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
label
amount
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,27 @@ public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress()

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testSetBillingAddressOnNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
items {
id
mutation {
setBillingAddressOnCart(
input: {
cart_id: "$maskedQuoteId"
billing_address: {
customer_address_id: 1
}
}
) {
cart {
billing_address {
city
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,9 @@ public function testSetNonExistentPaymentMethod()
public function testSetPaymentOnNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = <<<QUERY
{
cart(cart_id: "$maskedQuoteId") {
items {
id
}
}
}
QUERY;
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;

$query = $this->getQuery($maskedQuoteId, $methodCode);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\GraphQl\Quote;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\QuoteFactory;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function __construct(
*
* @param string $reversedOrderId
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
public function execute(string $reversedOrderId): string
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote;

use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Quote\Model\QuoteFactory;

/**
* Get quote shipping address id by reserved order id
*/
class GetQuoteShippingAddressIdByReservedQuoteId
{
/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @param QuoteFactory $quoteFactory
* @param QuoteResource $quoteResource
*/
public function __construct(
QuoteFactory $quoteFactory,
QuoteResource $quoteResource
) {
$this->quoteFactory = $quoteFactory;
$this->quoteResource = $quoteResource;
}

/**
* Get quote shipping address id by reserved order id
*
* @param string $reversedOrderId
* @return int
*/
public function execute(string $reversedOrderId): int
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reversedOrderId, 'reserved_order_id');

return (int)$quote->getShippingAddress()->getId();
}
}
Loading

0 comments on commit 419106a

Please sign in to comment.