Skip to content

Commit a04f8f8

Browse files
committed
Test coverage: Set OfflineShipping methods on Cart
1 parent 75cf826 commit a04f8f8

File tree

1 file changed

+352
-0
lines changed

1 file changed

+352
-0
lines changed
Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
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\Quote;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\Quote;
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 offline shipping methods on cart
19+
*/
20+
class SetOfflineShippingOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var CustomerTokenServiceInterface
24+
*/
25+
private $customerTokenService;
26+
27+
/**
28+
* @var QuoteResource
29+
*/
30+
private $quoteResource;
31+
32+
/**
33+
* @var Quote
34+
*/
35+
private $quote;
36+
37+
/**
38+
* @var QuoteIdToMaskedQuoteIdInterface
39+
*/
40+
private $quoteIdToMaskedId;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->quoteResource = $objectManager->create(QuoteResource::class);
49+
$this->quote = $objectManager->create(Quote::class);
50+
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
51+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
52+
}
53+
54+
/**
55+
* Test for general routine of setting a shipping method on shopping cart
56+
*
57+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
58+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
59+
*/
60+
public function testSetShippingMethodOnCart()
61+
{
62+
$shippingCarrierCode = 'flatrate';
63+
$shippingMethodCode = 'flatrate';
64+
$this->quoteResource->load(
65+
$this->quote,
66+
'test_order_1',
67+
'reserved_order_id'
68+
);
69+
$shippingAddress = $this->quote->getShippingAddress();
70+
$shippingAddressId = $shippingAddress->getId();
71+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
72+
73+
$query = $this->prepareMutationQuery(
74+
$maskedQuoteId,
75+
$shippingMethodCode,
76+
$shippingCarrierCode,
77+
$shippingAddressId
78+
);
79+
80+
$response = $this->sendRequestWithToken($query);
81+
82+
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
83+
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
84+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
85+
self::assertCount(1, $addressesInformation);
86+
}
87+
88+
/**
89+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
90+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
91+
*/
92+
public function testSetFlatrateOnCart()
93+
{
94+
$this->setShippingMethodAndCheckResponse(
95+
'flatrate',
96+
'flatrate',
97+
'10',
98+
'Flat Rate - Fixed'
99+
);
100+
}
101+
102+
/**
103+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
104+
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
105+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
106+
*/
107+
public function testSetTableRatesOnCart()
108+
{
109+
$this->setShippingMethodAndCheckResponse(
110+
'tablerate',
111+
'bestway',
112+
'10',
113+
'Best Way - Table Rate'
114+
);
115+
}
116+
117+
/**
118+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
119+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
120+
*/
121+
public function testSetFreeShippingOnCart()
122+
{
123+
$this->setShippingMethodAndCheckResponse(
124+
'freeshipping',
125+
'freeshipping',
126+
'0',
127+
'Free Shipping - Free'
128+
);
129+
}
130+
131+
/**
132+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
133+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
134+
*/
135+
public function testSetUpsOnCart()
136+
{
137+
$this->setShippingMethodAndCheckResponse(
138+
'ups',
139+
'GND',
140+
'15.61',
141+
'United Parcel Service - Ground'
142+
);
143+
}
144+
145+
/**
146+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
147+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
148+
*/
149+
public function testSetShippingMethodWithWrongCartId()
150+
{
151+
$shippingCarrierCode = 'flatrate';
152+
$shippingMethodCode = 'flatrate';
153+
$shippingAddressId = '1';
154+
$maskedQuoteId = 'invalid';
155+
156+
$query = $this->prepareMutationQuery(
157+
$maskedQuoteId,
158+
$shippingMethodCode,
159+
$shippingCarrierCode,
160+
$shippingAddressId
161+
);
162+
163+
self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\"");
164+
$this->sendRequestWithToken($query);
165+
}
166+
167+
/**
168+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
169+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
170+
*/
171+
public function testSetNonExistingShippingMethod()
172+
{
173+
$shippingCarrierCode = 'non';
174+
$shippingMethodCode = 'existing';
175+
$this->quoteResource->load(
176+
$this->quote,
177+
'test_order_1',
178+
'reserved_order_id'
179+
);
180+
$shippingAddress = $this->quote->getShippingAddress();
181+
$shippingAddressId = $shippingAddress->getId();
182+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
183+
184+
$query = $this->prepareMutationQuery(
185+
$maskedQuoteId,
186+
$shippingMethodCode,
187+
$shippingCarrierCode,
188+
$shippingAddressId
189+
);
190+
191+
self::expectExceptionMessage("Carrier with such method not found: $shippingCarrierCode, $shippingMethodCode");
192+
$this->sendRequestWithToken($query);
193+
}
194+
195+
/**
196+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
197+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
198+
*/
199+
public function testSetShippingMethodWithNonExistingAddress()
200+
{
201+
$shippingCarrierCode = 'flatrate';
202+
$shippingMethodCode = 'flatrate';
203+
$this->quoteResource->load(
204+
$this->quote,
205+
'test_order_1',
206+
'reserved_order_id'
207+
);
208+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
209+
$shippingAddressId = '-20';
210+
211+
$query = $this->prepareMutationQuery(
212+
$maskedQuoteId,
213+
$shippingMethodCode,
214+
$shippingCarrierCode,
215+
$shippingAddressId
216+
);
217+
218+
self::expectExceptionMessage("Could not find a cart address with ID \"$shippingAddressId\"");
219+
$this->sendRequestWithToken($query);
220+
}
221+
222+
/**
223+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
224+
* @magentoApiDataFixture Magento/Checkout/_files/enable_all_shipping_methods.php
225+
*/
226+
public function testSetShippingMethodByGuestToCustomerCart()
227+
{
228+
$shippingCarrierCode = 'flatrate';
229+
$shippingMethodCode = 'flatrate';
230+
$this->quoteResource->load(
231+
$this->quote,
232+
'test_order_1',
233+
'reserved_order_id'
234+
);
235+
$shippingAddress = $this->quote->getShippingAddress();
236+
$shippingAddressId = $shippingAddress->getId();
237+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
238+
239+
$query = $this->prepareMutationQuery(
240+
$maskedQuoteId,
241+
$shippingMethodCode,
242+
$shippingCarrierCode,
243+
$shippingAddressId
244+
);
245+
246+
self::expectExceptionMessage(
247+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
248+
);
249+
250+
$this->graphQlQuery($query);
251+
}
252+
253+
/**
254+
* Send request for setting the requested shipping method and check the output
255+
*
256+
* @param string $shippingCarrierCode
257+
* @param string $shippingMethodCode
258+
* @param string $shippingAmount
259+
* @param string $shippingLabel
260+
* @throws \Magento\Framework\Exception\AuthenticationException
261+
* @throws \Magento\Framework\Exception\NoSuchEntityException
262+
*/
263+
private function setShippingMethodAndCheckResponse(
264+
string $shippingCarrierCode,
265+
string $shippingMethodCode,
266+
string $shippingAmount,
267+
string $shippingLabel
268+
) {
269+
$this->quoteResource->load(
270+
$this->quote,
271+
'test_order_1',
272+
'reserved_order_id'
273+
);
274+
$shippingAddress = $this->quote->getShippingAddress();
275+
$shippingAddressId = $shippingAddress->getId();
276+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
277+
278+
$query = $this->prepareMutationQuery(
279+
$maskedQuoteId,
280+
$shippingMethodCode,
281+
$shippingCarrierCode,
282+
$shippingAddressId
283+
);
284+
285+
$response = $this->sendRequestWithToken($query);
286+
287+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
288+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $shippingCarrierCode);
289+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $shippingMethodCode);
290+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $shippingAmount);
291+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $shippingLabel);
292+
}
293+
294+
/**
295+
* Generates query for setting the specified shipping method on cart
296+
*
297+
* @param string $maskedQuoteId
298+
* @param string $shippingMethodCode
299+
* @param string $shippingCarrierCode
300+
* @param string $shippingAddressId
301+
* @return string
302+
*/
303+
private function prepareMutationQuery(
304+
string $maskedQuoteId,
305+
string $shippingMethodCode,
306+
string $shippingCarrierCode,
307+
string $shippingAddressId
308+
) : string {
309+
return <<<QUERY
310+
mutation {
311+
setShippingMethodsOnCart(input:
312+
{
313+
cart_id: "$maskedQuoteId",
314+
shipping_methods: [{
315+
cart_address_id: $shippingAddressId
316+
method_code: "$shippingMethodCode"
317+
carrier_code: "$shippingCarrierCode"
318+
}]
319+
}) {
320+
321+
cart {
322+
shipping_addresses {
323+
selected_shipping_method {
324+
carrier_code
325+
method_code
326+
label
327+
amount
328+
}
329+
}
330+
}
331+
}
332+
}
333+
334+
QUERY;
335+
}
336+
337+
/**
338+
* Sends a GraphQL request with using a bearer token
339+
*
340+
* @param string $query
341+
* @return array
342+
* @throws \Magento\Framework\Exception\AuthenticationException
343+
*/
344+
private function sendRequestWithToken(string $query): array
345+
{
346+
347+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
348+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
349+
350+
return $this->graphQlQuery($query, [], '', $headerMap);
351+
}
352+
}

0 commit comments

Comments
 (0)