Skip to content

Commit c3b4a80

Browse files
committed
Covered most cases except authorisation
1 parent 936a223 commit c3b4a80

File tree

1 file changed

+132
-28
lines changed

1 file changed

+132
-28
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingMethodOnCartTest.php

Lines changed: 132 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class SetShippingMethodOnCartTest extends GraphQlAbstract
3939
*/
4040
private $quoteIdToMaskedId;
4141

42+
/**
43+
* @inheritdoc
44+
*/
4245
protected function setUp()
4346
{
4447
$objectManager = Bootstrap::getObjectManager();
@@ -51,49 +54,146 @@ protected function setUp()
5154
/**
5255
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
5356
*/
54-
public function testSetShippingOnCart()
57+
public function testSetShippingMethodOnCart()
5558
{
59+
$shippingCarrierCode = 'flatrate';
60+
$shippingMethodCode = 'flatrate';
5661
$this->quoteResource->load(
5762
$this->quote,
5863
'test_order_1',
5964
'reserved_order_id'
6065
);
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+
}
61110

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+
);
62123
$shippingAddress = $this->quote->getShippingAddress();
63124
$shippingAddressId = $shippingAddress->getId();
64125
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
65-
$query = <<<QUERY
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+
// TODO: TBD - add check for guest with attempt to set shipping method to the customer's shopping cart
165+
166+
/**
167+
* Generates query for setting the specified shipping method on cart
168+
*
169+
* @param string $maskedQuoteId
170+
* @param string $shippingMethodCode
171+
* @param string $shippingCarrierCode
172+
* @param string $shippingAddressId
173+
* @return string
174+
*/
175+
private function prepareMutationQuery(
176+
string $maskedQuoteId,
177+
string $shippingMethodCode,
178+
string $shippingCarrierCode,
179+
string $shippingAddressId
180+
) : string {
181+
return <<<QUERY
66182
mutation {
67183
setShippingMethodsOnCart(input:
68184
{
69185
cart_id: "$maskedQuoteId",
70186
shipping_methods: [
71187
{
72-
shipping_method_code: "flatrate"
73-
shipping_carrier_code: "flatrate"
188+
shipping_method_code: "$shippingMethodCode"
189+
shipping_carrier_code: "$shippingCarrierCode"
74190
cart_address_id: $shippingAddressId
75191
}
76192
]}) {
77193
78194
cart {
79195
cart_id,
80196
addresses {
81-
firstname
82-
lastname
83-
company
84-
address_type
85-
city
86-
street
87-
region {
88-
code
89-
label
90-
}
91-
postcode
92-
country {
93-
code
94-
label
95-
}
96-
97197
selected_shipping_method {
98198
code
99199
label
@@ -104,17 +204,21 @@ public function testSetShippingOnCart()
104204
}
105205
106206
QUERY;
207+
}
208+
209+
/**
210+
* Sends a GraphQL request with using a bearer token
211+
*
212+
* @param string $query
213+
* @return array
214+
* @throws \Magento\Framework\Exception\AuthenticationException
215+
*/
216+
private function sendRequestWithToken(string $query): array
217+
{
107218

108219
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
109220
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
110-
$response = $this->graphQlQuery($query, [], '', $headerMap);
111-
112-
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
113-
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
114-
self::assertEquals($maskedQuoteId, $response['setShippingMethodsOnCart']['cart']['cart_id']);
115221

116-
// TODO: check shipping method code and description
222+
return $this->graphQlQuery($query, [], '', $headerMap);
117223
}
118-
119-
// TODO: cover all other cases
120224
}

0 commit comments

Comments
 (0)