Skip to content

Commit 3f9abc6

Browse files
committed
magento/graphql-ce#283: [Shipping methods] Support of FedEx shipping method
1 parent 212a533 commit 3f9abc6

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\FedEx;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\QuoteFactory;
12+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
13+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for setting "FedEx" shipping method on cart
19+
*/
20+
class SetFedExShippingMethodsOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* Defines carrier code for "FedEx" shipping method
24+
*/
25+
const CARRIER_CODE = 'fedex';
26+
27+
/**
28+
* Defines method code for the "Ground" FedEx shipping
29+
*/
30+
const CARRIER_METHOD_CODE_GROUND = 'FEDEX_GROUND';
31+
32+
/**
33+
* @var QuoteFactory
34+
*/
35+
private $quoteFactory;
36+
37+
/**
38+
* @var CustomerTokenServiceInterface
39+
*/
40+
private $customerTokenService;
41+
42+
/**
43+
* @var QuoteResource
44+
*/
45+
private $quoteResource;
46+
47+
/**
48+
* @var QuoteIdToMaskedQuoteIdInterface
49+
*/
50+
private $quoteIdToMaskedId;
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp()
56+
{
57+
$objectManager = Bootstrap::getObjectManager();
58+
$this->quoteResource = $objectManager->get(QuoteResource::class);
59+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
60+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
61+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
62+
}
63+
64+
/**
65+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
66+
* @magentoApiDataFixture Magento/FedEx/_files/enable_fedex_shipping_method.php
67+
*/
68+
public function testSetFedExShippingMethod()
69+
{
70+
$quote = $this->quoteFactory->create();
71+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
72+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
73+
$shippingAddressId = (int)$quote->getShippingAddress()->getId();
74+
75+
$query = $this->getAddFedExShippingMethodQuery(
76+
$maskedQuoteId,
77+
$shippingAddressId,
78+
self::CARRIER_CODE,
79+
self::CARRIER_METHOD_CODE_GROUND
80+
);
81+
82+
$response = $this->sendRequestWithToken($query);
83+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
84+
$expectedResult = [
85+
'carrier_code' => self::CARRIER_CODE,
86+
'method_code' => self::CARRIER_METHOD_CODE_GROUND,
87+
'label' => 'Federal Express - Ground',
88+
];
89+
self::assertEquals($addressesInformation[0]['selected_shipping_method'], $expectedResult);
90+
}
91+
92+
/**
93+
* Generates query for setting the specified shipping method on cart
94+
*
95+
* @param int $shippingAddressId
96+
* @param string $maskedQuoteId
97+
* @param string $carrierCode
98+
* @param string $methodCode
99+
* @return string
100+
*/
101+
private function getAddFedExShippingMethodQuery(
102+
string $maskedQuoteId,
103+
int $shippingAddressId,
104+
string $carrierCode,
105+
string $methodCode
106+
): string {
107+
return <<<QUERY
108+
mutation {
109+
setShippingMethodsOnCart(input: {
110+
cart_id: "$maskedQuoteId"
111+
shipping_methods: [
112+
{
113+
cart_address_id: $shippingAddressId
114+
carrier_code: "$carrierCode"
115+
method_code: "$methodCode"
116+
}
117+
]
118+
}) {
119+
cart {
120+
shipping_addresses {
121+
selected_shipping_method {
122+
carrier_code
123+
method_code
124+
label
125+
}
126+
}
127+
}
128+
}
129+
}
130+
QUERY;
131+
}
132+
133+
/**
134+
* Sends a GraphQL request with using a bearer token
135+
*
136+
* @param string $query
137+
* @return array
138+
* @throws \Magento\Framework\Exception\AuthenticationException
139+
*/
140+
private function sendRequestWithToken(string $query): array
141+
{
142+
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password');
143+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
144+
145+
return $this->graphQlQuery($query, [], '', $headerMap);
146+
}
147+
}

0 commit comments

Comments
 (0)