@@ -39,6 +39,9 @@ class SetShippingMethodOnCartTest extends GraphQlAbstract
39
39
*/
40
40
private $ quoteIdToMaskedId ;
41
41
42
+ /**
43
+ * @inheritdoc
44
+ */
42
45
protected function setUp ()
43
46
{
44
47
$ objectManager = Bootstrap::getObjectManager ();
@@ -51,49 +54,146 @@ protected function setUp()
51
54
/**
52
55
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
53
56
*/
54
- public function testSetShippingOnCart ()
57
+ public function testSetShippingMethodOnCart ()
55
58
{
59
+ $ shippingCarrierCode = 'flatrate ' ;
60
+ $ shippingMethodCode = 'flatrate ' ;
56
61
$ this ->quoteResource ->load (
57
62
$ this ->quote ,
58
63
'test_order_1 ' ,
59
64
'reserved_order_id '
60
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
+ }
61
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
+ );
62
123
$ shippingAddress = $ this ->quote ->getShippingAddress ();
63
124
$ shippingAddressId = $ shippingAddress ->getId ();
64
125
$ 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
66
182
mutation {
67
183
setShippingMethodsOnCart(input:
68
184
{
69
185
cart_id: " $ maskedQuoteId",
70
186
shipping_methods: [
71
187
{
72
- shipping_method_code: "flatrate "
73
- shipping_carrier_code: "flatrate "
188
+ shipping_method_code: " $ shippingMethodCode "
189
+ shipping_carrier_code: " $ shippingCarrierCode "
74
190
cart_address_id: $ shippingAddressId
75
191
}
76
192
]}) {
77
193
78
194
cart {
79
195
cart_id,
80
196
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
-
97
197
selected_shipping_method {
98
198
code
99
199
label
@@ -104,17 +204,21 @@ public function testSetShippingOnCart()
104
204
}
105
205
106
206
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
+ {
107
218
108
219
$ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ('customer@example.com ' , 'password ' );
109
220
$ 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 ' ]);
115
221
116
- // TODO: check shipping method code and description
222
+ return $ this -> graphQlQuery ( $ query , [], '' , $ headerMap );
117
223
}
118
-
119
- // TODO: cover all other cases
120
224
}
0 commit comments