Skip to content

Commit 62e79c3

Browse files
ENGCOM-4573: 281 - [Shipping methods] Support of UPS shipping method #505
- Merge Pull Request magento/graphql-ce#505 from magento/graphql-ce:281-shipping-methods-UPS - Merged commits: 1. 6f8d99c 2. 30a2a80 3. 73088cd
2 parents 677f1b6 + 73088cd commit 62e79c3

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-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\Ups;
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 "UPS" shipping method on cart
19+
*/
20+
class SetUpsShippingMethodsOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* Defines carrier code for "UPS" shipping method
24+
*/
25+
const CARRIER_CODE = 'ups';
26+
27+
/**
28+
* Defines method code for the "Ground" UPS shipping
29+
*/
30+
const CARRIER_METHOD_CODE_GROUND = 'GND';
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/Ups/_files/enable_ups_shipping_method.php
67+
*/
68+
public function testSetUpsShippingMethod()
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->getAddUpsShippingMethodQuery(
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' => 'United Parcel Service - 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 getAddUpsShippingMethodQuery(
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('customer@example.com', 'password');
143+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
144+
145+
return $this->graphQlQuery($query, [], '', $headerMap);
146+
}
147+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
use Magento\Framework\App\Config\Storage\Writer;
9+
use Magento\Framework\App\Config\Storage\WriterInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var Writer $configWriter */
15+
$configWriter = $objectManager->get(WriterInterface::class);
16+
17+
$configWriter->save('carriers/ups/active', 1);
18+
19+
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
20+
$scopeConfig->clean();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
use Magento\Framework\App\Config\Storage\Writer;
9+
use Magento\Framework\App\Config\Storage\WriterInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var Writer $configWriter */
14+
$configWriter = $objectManager->create(WriterInterface::class);
15+
16+
$configWriter->delete('carriers/ups/active');

0 commit comments

Comments
 (0)