Skip to content

Commit 58831be

Browse files
committed
GraphQL-424: Test coverage: Set OfflineShipping methods on Cart
1 parent 005e798 commit 58831be

File tree

3 files changed

+69
-106
lines changed

3 files changed

+69
-106
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/OfflineShipping/SetOfflineShippingMethodsOnCartTest.php

Lines changed: 67 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,52 @@ protected function setUp()
5353

5454
/**
5555
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
56+
* @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php
5657
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
57-
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
58-
* @dataProvider offlineShippingMethodDataProvider()
59-
* @param string $carrier
60-
* @param string $method
58+
*
59+
* @param string $carrierCode
60+
* @param string $methodCode
6161
* @param float $amount
6262
* @param string $label
63+
* @dataProvider offlineShippingMethodDataProvider
6364
*/
64-
public function testSetOfflineShippingMethod(string $carrier, string $method, float $amount, string $label)
65+
public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label)
6566
{
66-
$this->setShippingMethodAndCheckResponse(
67-
$carrier,
68-
$method,
69-
$amount,
70-
$label
67+
$quote = $this->quoteFactory->create();
68+
$this->quoteResource->load(
69+
$quote,
70+
'test_order_1',
71+
'reserved_order_id'
72+
);
73+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
74+
$shippingAddressId = (int)$quote->getShippingAddress()->getId();
75+
76+
$query = $this->getQuery(
77+
$maskedQuoteId,
78+
$shippingAddressId,
79+
$carrierCode,
80+
$methodCode
7181
);
82+
83+
$response = $this->sendRequestWithToken($query);
84+
85+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
86+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $carrierCode);
87+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $methodCode);
88+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $amount);
89+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $label);
90+
}
91+
92+
/**
93+
* @return array
94+
*/
95+
public function offlineShippingMethodDataProvider()
96+
{
97+
return [
98+
'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'],
99+
'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'],
100+
'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'],
101+
];
72102
}
73103

74104
/**
@@ -89,23 +119,21 @@ public function testSetShippingMethodTwiceInOneRequest()
89119

90120
$query = <<<QUERY
91121
mutation {
92-
setShippingMethodsOnCart(input:
93-
{
94-
cart_id: "$maskedQuoteId",
95-
shipping_methods: [
122+
setShippingMethodsOnCart(input: {
123+
cart_id: "$maskedQuoteId"
124+
shipping_methods: [
96125
{
97126
cart_address_id: $shippingAddressId
98-
method_code: "flatrate"
99127
carrier_code: "flatrate"
128+
method_code: "flatrate"
100129
}
101130
{
102131
cart_address_id: $shippingAddressId
103-
method_code: "freeshipping"
104132
carrier_code: "freeshipping"
133+
method_code: "freeshipping"
105134
}
106-
]
107-
}) {
108-
135+
]
136+
}) {
109137
cart {
110138
shipping_addresses {
111139
selected_shipping_method {
@@ -115,98 +143,41 @@ public function testSetShippingMethodTwiceInOneRequest()
115143
amount
116144
}
117145
}
118-
}
146+
}
119147
}
120148
}
121149
QUERY;
122-
123150
self::expectExceptionMessage('You cannot specify multiple shipping methods.');
124151
$this->sendRequestWithToken($query);
125152
}
126153

127-
/**
128-
* Data provider for base offline shipping methods
129-
*
130-
* @return array
131-
*/
132-
public function offlineShippingMethodDataProvider()
133-
{
134-
return [
135-
['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'],
136-
['tablerate', 'bestway', 10, 'Best Way - Table Rate'],
137-
['freeshipping', 'freeshipping', 0, 'Free Shipping - Free']
138-
];
139-
}
140-
141-
/**
142-
* Send request for setting the requested shipping method and check the output
143-
*
144-
* @param string $shippingCarrierCode
145-
* @param string $shippingMethodCode
146-
* @param float $shippingAmount
147-
* @param string $shippingLabel
148-
* @throws \Magento\Framework\Exception\AuthenticationException
149-
* @throws \Magento\Framework\Exception\NoSuchEntityException
150-
*/
151-
private function setShippingMethodAndCheckResponse(
152-
string $shippingCarrierCode,
153-
string $shippingMethodCode,
154-
float $shippingAmount,
155-
string $shippingLabel
156-
) {
157-
$quote = $this->quoteFactory->create();
158-
$this->quoteResource->load(
159-
$quote,
160-
'test_order_1',
161-
'reserved_order_id'
162-
);
163-
$shippingAddress = $quote->getShippingAddress();
164-
$shippingAddressId = $shippingAddress->getId();
165-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
166-
167-
$query = $this->getQuery(
168-
$maskedQuoteId,
169-
$shippingMethodCode,
170-
$shippingCarrierCode,
171-
$shippingAddressId
172-
);
173-
174-
$response = $this->sendRequestWithToken($query);
175-
176-
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
177-
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $shippingCarrierCode);
178-
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $shippingMethodCode);
179-
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $shippingAmount);
180-
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $shippingLabel);
181-
}
182-
183154
/**
184155
* Generates query for setting the specified shipping method on cart
185156
*
157+
* @param int $shippingAddressId
186158
* @param string $maskedQuoteId
187-
* @param string $shippingMethodCode
188-
* @param string $shippingCarrierCode
189-
* @param string $shippingAddressId
159+
* @param string $carrierCode
160+
* @param string $methodCode
190161
* @return string
191162
*/
192163
private function getQuery(
193164
string $maskedQuoteId,
194-
string $shippingMethodCode,
195-
string $shippingCarrierCode,
196-
string $shippingAddressId
197-
) : string {
165+
int $shippingAddressId,
166+
string $carrierCode,
167+
string $methodCode
168+
): string {
198169
return <<<QUERY
199170
mutation {
200-
setShippingMethodsOnCart(input:
201-
{
202-
cart_id: "$maskedQuoteId",
203-
shipping_methods: [{
171+
setShippingMethodsOnCart(input: {
172+
cart_id: "$maskedQuoteId"
173+
shipping_methods: [
174+
{
204175
cart_address_id: $shippingAddressId
205-
method_code: "$shippingMethodCode"
206-
carrier_code: "$shippingCarrierCode"
207-
}]
208-
}) {
209-
176+
carrier_code: "$carrierCode"
177+
method_code: "$methodCode"
178+
}
179+
]
180+
}) {
210181
cart {
211182
shipping_addresses {
212183
selected_shipping_method {
@@ -216,9 +187,9 @@ private function getQuery(
216187
amount
217188
}
218189
}
219-
}
190+
}
220191
}
221-
}
192+
}
222193
QUERY;
223194
}
224195

@@ -236,4 +207,4 @@ private function sendRequestWithToken(string $query): array
236207

237208
return $this->graphQlQuery($query, [], '', $headerMap);
238209
}
239-
}
210+
}

dev/tests/integration/testsuite/Magento/Checkout/_files/enable_all_shipping_methods.php renamed to dev/tests/integration/testsuite/Magento/OfflineShipping/_files/enable_offline_shipping_methods.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212

1313
$objectManager = Bootstrap::getObjectManager();
14-
/** @var Writer $configWriter */
15-
$configWriter = $objectManager->create(WriterInterface::class);
14+
/** @var Writer $configWriter */
15+
$configWriter = $objectManager->get(WriterInterface::class);
1616

1717
$configWriter->save('carriers/flatrate/active', 1);
1818
$configWriter->save('carriers/tablerate/active', 1);
1919
$configWriter->save('carriers/freeshipping/active', 1);
20-
$configWriter->save('carriers/ups/active', 1);
21-
$configWriter->save('carriers/usps/active', 1);
22-
$configWriter->save('carriers/fedex/active', 1);
23-
$configWriter->save('carriers/dhl/active', 1);
2420

2521
$scopeConfig = $objectManager->get(ScopeConfigInterface::class);
2622
$scopeConfig->clean();

dev/tests/integration/testsuite/Magento/Checkout/_files/enable_all_shipping_methods_rollback.php renamed to dev/tests/integration/testsuite/Magento/OfflineShipping/_files/enable_offline_shipping_methods_rollback.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,3 @@
1616
$configWriter->delete('carriers/flatrate/active');
1717
$configWriter->delete('carriers/tablerate/active');
1818
$configWriter->delete('carriers/freeshipping/active');
19-
$configWriter->delete('carriers/ups/active');
20-
$configWriter->delete('carriers/usps/active');
21-
$configWriter->delete('carriers/fedex/active');
22-
$configWriter->delete('carriers/dhl/active');

0 commit comments

Comments
 (0)