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

Commit 75e95e9

Browse files
committed
API-functional test for setting shipping methods
1 parent b0e029d commit 75e95e9

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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 shipping methods on cart
19+
*/
20+
class SetShippingMethodsOnCartTest 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+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
56+
*/
57+
public function testSetShippingMethodOnCart()
58+
{
59+
$shippingCarrierCode = 'flatrate';
60+
$shippingMethodCode = 'flatrate';
61+
$this->quoteResource->load(
62+
$this->quote,
63+
'test_order_1',
64+
'reserved_order_id'
65+
);
66+
$shippingAddress = $this->quote->getShippingAddress();
67+
$shippingAddressId = $shippingAddress->getId();
68+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
69+
70+
$query = $this->prepareMutationQuery(
71+
$maskedQuoteId,
72+
$shippingMethodCode,
73+
$shippingCarrierCode,
74+
$shippingAddressId
75+
);
76+
77+
$response = $this->sendRequestWithToken($query);
78+
79+
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
80+
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
81+
self::assertEquals($maskedQuoteId, $response['setShippingMethodsOnCart']['cart']['cart_id']);
82+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
83+
self::assertCount(1, $addressesInformation);
84+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $shippingCarrierCode);
85+
self::assertEquals(
86+
$addressesInformation[0]['selected_shipping_method']['method_code'], $shippingMethodCode);
87+
}
88+
89+
/**
90+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
91+
*/
92+
public function testSetShippingMethodWithWrongCartId()
93+
{
94+
$shippingCarrierCode = 'flatrate';
95+
$shippingMethodCode = 'flatrate';
96+
$shippingAddressId = '1';
97+
$maskedQuoteId = 'invalid';
98+
99+
$query = $this->prepareMutationQuery(
100+
$maskedQuoteId,
101+
$shippingMethodCode,
102+
$shippingCarrierCode,
103+
$shippingAddressId
104+
);
105+
106+
self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\"");
107+
$this->sendRequestWithToken($query);
108+
}
109+
110+
/**
111+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
112+
*/
113+
public function testSetNonExistingShippingMethod()
114+
{
115+
$shippingCarrierCode = 'non';
116+
$shippingMethodCode = 'existing';
117+
$this->quoteResource->load(
118+
$this->quote,
119+
'test_order_1',
120+
'reserved_order_id'
121+
);
122+
$shippingAddress = $this->quote->getShippingAddress();
123+
$shippingAddressId = $shippingAddress->getId();
124+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
125+
126+
$query = $this->prepareMutationQuery(
127+
$maskedQuoteId,
128+
$shippingMethodCode,
129+
$shippingCarrierCode,
130+
$shippingAddressId
131+
);
132+
133+
self::expectExceptionMessage("Carrier with such method not found: $shippingCarrierCode, $shippingMethodCode");
134+
$this->sendRequestWithToken($query);
135+
}
136+
137+
/**
138+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
139+
*/
140+
public function testSetShippingMethodWithNonExistingAddress()
141+
{
142+
$shippingCarrierCode = 'flatrate';
143+
$shippingMethodCode = 'flatrate';
144+
$this->quoteResource->load(
145+
$this->quote,
146+
'test_order_1',
147+
'reserved_order_id'
148+
);
149+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
150+
$shippingAddressId = '-20';
151+
152+
$query = $this->prepareMutationQuery(
153+
$maskedQuoteId,
154+
$shippingMethodCode,
155+
$shippingCarrierCode,
156+
$shippingAddressId
157+
);
158+
159+
self::expectExceptionMessage('The shipping address is missing. Set the address and try again.');
160+
$this->sendRequestWithToken($query);
161+
}
162+
163+
/**
164+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
165+
*/
166+
public function testSetShippingMethodByGuestToCustomerCart()
167+
{
168+
$shippingCarrierCode = 'flatrate';
169+
$shippingMethodCode = 'flatrate';
170+
$this->quoteResource->load(
171+
$this->quote,
172+
'test_order_1',
173+
'reserved_order_id'
174+
);
175+
$shippingAddress = $this->quote->getShippingAddress();
176+
$shippingAddressId = $shippingAddress->getId();
177+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
178+
179+
$query = $this->prepareMutationQuery(
180+
$maskedQuoteId,
181+
$shippingMethodCode,
182+
$shippingCarrierCode,
183+
$shippingAddressId
184+
);
185+
186+
self::expectExceptionMessage(
187+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
188+
);
189+
190+
$this->graphQlQuery($query);
191+
}
192+
193+
/**
194+
* Generates query for setting the specified shipping method on cart
195+
*
196+
* @param string $maskedQuoteId
197+
* @param string $shippingMethodCode
198+
* @param string $shippingCarrierCode
199+
* @param string $shippingAddressId
200+
* @return string
201+
*/
202+
private function prepareMutationQuery(
203+
string $maskedQuoteId,
204+
string $shippingMethodCode,
205+
string $shippingCarrierCode,
206+
string $shippingAddressId
207+
) : string {
208+
return <<<QUERY
209+
mutation {
210+
setShippingMethodsOnCart(input:
211+
{
212+
cart_id: "$maskedQuoteId",
213+
shipping_addresses: [{
214+
cart_address_id: $shippingAddressId
215+
shipping_method: {
216+
method_code: "$shippingMethodCode"
217+
carrier_code: "$shippingCarrierCode"
218+
}
219+
}]
220+
}) {
221+
222+
cart {
223+
cart_id,
224+
shipping_addresses {
225+
selected_shipping_method {
226+
carrier_code
227+
method_code
228+
label
229+
}
230+
}
231+
}
232+
}
233+
}
234+
235+
QUERY;
236+
}
237+
238+
/**
239+
* Sends a GraphQL request with using a bearer token
240+
*
241+
* @param string $query
242+
* @return array
243+
* @throws \Magento\Framework\Exception\AuthenticationException
244+
*/
245+
private function sendRequestWithToken(string $query): array
246+
{
247+
248+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
249+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
250+
251+
return $this->graphQlQuery($query, [], '', $headerMap);
252+
}
253+
}

0 commit comments

Comments
 (0)