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

Test coverage: Set OfflineShipping methods on Cart #442

Merged
merged 8 commits into from
Mar 13, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\OfflineShipping;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for setting offline shipping methods on cart
*/
class SetOfflineShippingOnCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

/**
* @var Quote
*/
private $quote;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->create(QuoteResource::class);
$this->quote = $objectManager->create(Quote::class);
rogyar marked this conversation as resolved.
Show resolved Hide resolved
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
*/
public function testSetFlatrateOnCart()
rogyar marked this conversation as resolved.
Show resolved Hide resolved
{
$this->setShippingMethodAndCheckResponse(
'flatrate',
'flatrate',
'10',
'Flat Rate - Fixed'
);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
*/
public function testSetTableRatesOnCart()
{
$this->setShippingMethodAndCheckResponse(
'tablerate',
'bestway',
'10',
'Best Way - Table Rate'
);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
*/
public function testSetFreeShippingOnCart()
{
$this->setShippingMethodAndCheckResponse(
'freeshipping',
'freeshipping',
'0',
'Free Shipping - Free'
);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
*/
public function testSetUpsOnCart()
rogyar marked this conversation as resolved.
Show resolved Hide resolved
{
$this->setShippingMethodAndCheckResponse(
'ups',
'GND',
'15.61',
'United Parcel Service - Ground'
);
}

/**
* Send request for setting the requested shipping method and check the output
*
* @param string $shippingCarrierCode
* @param string $shippingMethodCode
* @param string $shippingAmount
* @param string $shippingLabel
* @throws \Magento\Framework\Exception\AuthenticationException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function setShippingMethodAndCheckResponse(
string $shippingCarrierCode,
string $shippingMethodCode,
string $shippingAmount,
rogyar marked this conversation as resolved.
Show resolved Hide resolved
string $shippingLabel
) {
$this->quoteResource->load(
$this->quote,
'test_order_1',
'reserved_order_id'
);
$shippingAddress = $this->quote->getShippingAddress();
$shippingAddressId = $shippingAddress->getId();
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());

$query = $this->getQuery(
$maskedQuoteId,
$shippingMethodCode,
$shippingCarrierCode,
$shippingAddressId
);

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

$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $shippingCarrierCode);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $shippingMethodCode);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $shippingAmount);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $shippingLabel);
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to add 'Displayed Error Message' and Sort fields to the scheme as part of available_shipping_methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this case is out of scope of the current test since the current test covers setting shipping addresses but not getting available shipping addresses.
I would recommend creating a separate issue for getAvailableShippingMethods coverage.

}

/**
* Generates query for setting the specified shipping method on cart
*
* @param string $maskedQuoteId
* @param string $shippingMethodCode
* @param string $shippingCarrierCode
* @param string $shippingAddressId
* @return string
*/
private function getQuery(
string $maskedQuoteId,
string $shippingMethodCode,
string $shippingCarrierCode,
string $shippingAddressId
) : string {
return <<<QUERY
mutation {
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_methods: [{
rogyar marked this conversation as resolved.
Show resolved Hide resolved
cart_address_id: $shippingAddressId
method_code: "$shippingMethodCode"
carrier_code: "$shippingCarrierCode"
}]
}) {

cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
label
amount
}
}
}
}
}

QUERY;
}

/**
* Sends a GraphQL request with using a bearer token
*
* @param string $query
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function sendRequestWithToken(string $query): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];

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