Skip to content

Commit 424af68

Browse files
ENGCOM-3474: Added API-functional tests coverage for setting shipping method for cart #240
- Merge Pull Request magento/graphql-ce#240 from magento/graphql-ce:shipping-methods-tests - Merged commits: 1. 936a223 2. c3b4a80 3. c713b67 4. bae69d6 5. 5cadbd0
2 parents c49da73 + 5cadbd0 commit 424af68

File tree

1 file changed

+252
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)