Skip to content

Commit

Permalink
issue #485 [Test coverage] testReSetShippingMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
vovsky committed Mar 16, 2019
1 parent 3666e03 commit 5d9a64e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
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) {
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

0 comments on commit 5d9a64e

Please sign in to comment.