Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

issue #485 [Test coverage] testReSetShippingMethod #496

Merged
merged 14 commits into from
Mar 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Quote\Model\Quote;

/**
* Test for setting shipping methods on cart for customer
Expand Down Expand Up @@ -116,6 +117,33 @@ public function testSetMultipleShippingMethods()
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/423');
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
*/
public function testReSetShippingMethod()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1');
$methodCode = 'flatrate';
$carrierCode = 'flatrate';
$quote = $this->getQuoteByReversedQuoteId('test_order_1');
$shippingAddressId = $quote->getShippingAddress()->getId();

$query = $this->prepareMutationQuery($maskedQuoteId, $methodCode, $carrierCode, $shippingAddressId);
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
foreach ($response['setShippingMethodsOnCart']['cart']['shipping_addresses'] as $address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use foreach that means we have a few shipping addresses for Cart.
Need to check why don't we have an exception Multishipping is not supported with this preconditions
Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to change shipping_addresses field of Cart type to shipping_address
and to throw Multishipping is not supported exception in \Magento\QuoteGraphQl\Model\Resolver\ShippingAddresses resolver if there is more than one shipping address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to change shipping_addresses field of Cart type to shipping_address
and to throw Multishipping is not supported exception in \Magento\QuoteGraphQl\Model\Resolver\ShippingAddresses resolver if there is more than one shipping address?

self::assertArrayHasKey('address_id', $address);
if ($address['address_id'] == $shippingAddressId) {
self::assertArrayHasKey('selected_shipping_method', $address);
self::assertEquals($methodCode, $address['selected_shipping_method']['method_code']);
self::assertEquals($carrierCode, $address['selected_shipping_method']['carrier_code']);
}
}
}

/**
* @param string $maskedQuoteId
* @param string $shippingMethodCode
Expand All @@ -135,18 +163,15 @@ private function prepareMutationQuery(
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_addresses: [{
shipping_methods: [{
cart_address_id: $shippingAddressId
shipping_method: {
method_code: "$shippingMethodCode"
carrier_code: "$shippingCarrierCode"
}
method_code: "$shippingMethodCode"
carrier_code: "$shippingCarrierCode"
}]
}) {

cart {
cart_id,
shipping_addresses {
address_id
selected_shipping_method {
carrier_code
method_code
Expand Down Expand Up @@ -174,6 +199,18 @@ private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): str
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $reversedQuoteId
* @return Quote
*/
private function getQuoteByReversedQuoteId(string $reversedQuoteId): Quote
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');

return $quote;
}

/**
* @param string $reversedQuoteId
* @param int $customerId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\GraphQl\Quote\Guest;

use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
Expand Down Expand Up @@ -109,6 +110,34 @@ public function testSetMultipleShippingMethods()
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_shipping_method.php
*/
public function testReSetShippingMethod()
{
$maskedQuoteId = $this->unAssignCustomerFromQuoteAndShippingAddress('test_order_1');
$methodCode = 'flatrate';
$carrierCode = 'flatrate';
$quote = $this->getQuoteByReversedQuoteId('test_order_1');
$shippingAddress = $quote->getShippingAddress();
$shippingAddressId = $shippingAddress->getId();
$query = $this->prepareMutationQuery($maskedQuoteId, $methodCode, $carrierCode, $shippingAddressId);

$response = $this->graphQlQuery($query);

self::assertArrayHasKey('setShippingMethodsOnCart', $response);
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
foreach ($response['setShippingMethodsOnCart']['cart']['shipping_addresses'] as $address) {
self::assertArrayHasKey('address_id', $address);
if ($address['address_id'] == $shippingAddressId) {
self::assertArrayHasKey('selected_shipping_method', $address);
self::assertEquals($methodCode, $address['selected_shipping_method']['method_code']);
self::assertEquals($carrierCode, $address['selected_shipping_method']['carrier_code']);
}
}
}

/**
* @param string $maskedQuoteId
* @param string $shippingMethodCode
Expand All @@ -128,16 +157,15 @@ private function prepareMutationQuery(
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_addresses: [{
shipping_methods: [{
cart_address_id: $shippingAddressId
shipping_method: {
method_code: "$shippingMethodCode"
carrier_code: "$shippingCarrierCode"
}
method_code: "$shippingMethodCode"
carrier_code: "$shippingCarrierCode"
}]
}) {
cart {
shipping_addresses {
address_id
selected_shipping_method {
carrier_code
method_code
Expand All @@ -152,6 +180,40 @@ private function prepareMutationQuery(
QUERY;
}

/**
* @param string $reversedQuoteId
* @param int $customerId
* @return string
*/
private function unAssignCustomerFromQuoteAndShippingAddress(
string $reversedQuoteId
): string {
/** @var Quote $quote */
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
$quote->setCustomerId(0);

$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCustomerId(0);
$shippingAddress->setCustomerAddressId(0);

$this->quoteResource->save($quote);

return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $reversedQuoteId
* @return Quote
*/
private function getQuoteByReversedQuoteId(string $reversedQuoteId): Quote
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');

return $quote;
}

/**
* @param string $reversedQuoteId
* @return string
Expand Down