8
8
namespace Magento \GraphQl \Ups ;
9
9
10
10
use Magento \GraphQl \Quote \GetMaskedQuoteIdByReservedOrderId ;
11
+ use Magento \GraphQl \Quote \GetQuoteShippingAddressIdByReservedQuoteId ;
11
12
use Magento \Integration \Api \CustomerTokenServiceInterface ;
12
- use Magento \Quote \Model \Quote ;
13
- use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
14
13
use Magento \TestFramework \Helper \Bootstrap ;
15
14
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
15
20
19
class SetUpsShippingMethodsOnCartTest extends GraphQlAbstract
21
20
{
22
21
/**
23
- * Defines carrier code for "UPS" shipping method
24
- */
25
- const CARRIER_CODE = 'ups ' ;
26
-
27
- /**
28
- * Defines method code for the "Ground" UPS shipping
22
+ * Defines carrier label for "UPS" shipping method
29
23
*/
30
- const CARRIER_METHOD_CODE_GROUND = 'GND ' ;
24
+ const CARRIER_LABEL = 'United Parcel Service ' ;
31
25
32
26
/**
33
- * @var Quote
27
+ * Defines carrier code for "UPS" shipping method
34
28
*/
35
- private $ quote ;
29
+ const CARRIER_CODE = ' ups ' ;
36
30
37
31
/**
38
32
* @var CustomerTokenServiceInterface
@@ -44,49 +38,161 @@ class SetUpsShippingMethodsOnCartTest extends GraphQlAbstract
44
38
*/
45
39
private $ getMaskedQuoteIdByReservedOrderId ;
46
40
41
+ /**
42
+ * @var GetQuoteShippingAddressIdByReservedQuoteId
43
+ */
44
+ private $ getQuoteShippingAddressIdByReservedQuoteId ;
45
+
47
46
/**
48
47
* @inheritdoc
49
48
*/
50
49
protected function setUp ()
51
50
{
52
51
$ objectManager = Bootstrap::getObjectManager ();
53
- $ this ->quote = $ objectManager ->create (Quote::class);
54
52
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
55
53
$ this ->getMaskedQuoteIdByReservedOrderId = $ objectManager ->get (GetMaskedQuoteIdByReservedOrderId::class);
54
+ $ this ->getQuoteShippingAddressIdByReservedQuoteId = $ objectManager ->get (GetQuoteShippingAddressIdByReservedQuoteId::class);
56
55
}
57
56
58
57
/**
59
58
* @magentoApiDataFixture Magento/Customer/_files/customer.php
60
- * @magentoApiDataFixture Magento/Catalog/_files/products .php
59
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple .php
61
60
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
62
61
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
63
62
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
64
63
* @magentoApiDataFixture Magento/Ups/_files/enable_ups_shipping_method.php
64
+ *
65
+ * @param string $carrierMethodCode
66
+ * @param string $carrierMethodLabel
67
+ * @dataProvider availableForCartShippingMethods
65
68
*/
66
- public function testSetUpsShippingMethod ( )
69
+ public function testSetAvailableForCartUpsShippingMethod ( string $ carrierMethodCode , string $ carrierMethodLabel )
67
70
{
68
71
$ quoteReservedId = 'test_quote ' ;
69
72
$ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ($ quoteReservedId );
70
- $ quote = $ this ->quote ->load ($ quoteReservedId , 'reserved_order_id ' );
71
- $ shippingAddressId = (int )$ quote ->getShippingAddress ()->getId ();
73
+ $ shippingAddressId = $ this ->getQuoteShippingAddressIdByReservedQuoteId ->execute ($ quoteReservedId );
72
74
73
- $ query = $ this ->getAddUpsShippingMethodQuery (
75
+ $ query = $ this ->getQuery (
74
76
$ maskedQuoteId ,
75
77
$ shippingAddressId ,
76
78
self ::CARRIER_CODE ,
77
- self :: CARRIER_METHOD_CODE_GROUND
79
+ $ carrierMethodCode
78
80
);
79
81
80
82
$ response = $ this ->sendRequestWithToken ($ query );
83
+
81
84
$ addressesInformation = $ response ['setShippingMethodsOnCart ' ]['cart ' ]['shipping_addresses ' ];
82
85
$ expectedResult = [
83
86
'carrier_code ' => self ::CARRIER_CODE ,
84
- 'method_code ' => self :: CARRIER_METHOD_CODE_GROUND ,
85
- 'label ' => ' United Parcel Service - Ground ' ,
87
+ 'method_code ' => $ carrierMethodCode ,
88
+ 'label ' => self :: CARRIER_LABEL . ' - ' . $ carrierMethodLabel ,
86
89
];
87
90
self ::assertEquals ($ addressesInformation [0 ]['selected_shipping_method ' ], $ expectedResult );
88
91
}
89
92
93
+ /**
94
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
95
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
96
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
97
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
98
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
99
+ * @magentoApiDataFixture Magento/Ups/_files/enable_ups_shipping_method.php
100
+ *
101
+ * @param string $carrierMethodCode
102
+ * @param string $carrierMethodLabel
103
+ * @dataProvider notAvailableForCartShippingMethods
104
+ */
105
+ public function testSetNotAvailableForCartUpsShippingMethod (string $ carrierMethodCode , string $ carrierMethodLabel )
106
+ {
107
+ $ quoteReservedId = 'test_quote ' ;
108
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ($ quoteReservedId );
109
+ $ shippingAddressId = $ this ->getQuoteShippingAddressIdByReservedQuoteId ->execute ($ quoteReservedId );
110
+
111
+ $ query = $ this ->getQuery (
112
+ $ maskedQuoteId ,
113
+ $ shippingAddressId ,
114
+ self ::CARRIER_CODE ,
115
+ $ carrierMethodCode
116
+ );
117
+
118
+ $ this ->expectExceptionMessage (
119
+ "GraphQL response contains errors: Carrier with such method not found: " . self ::CARRIER_CODE . ", " . $ carrierMethodCode
120
+ );
121
+
122
+ $ response = $ this ->sendRequestWithToken ($ query );
123
+
124
+ $ addressesInformation = $ response ['setShippingMethodsOnCart ' ]['cart ' ]['shipping_addresses ' ];
125
+ $ expectedResult = [
126
+ 'carrier_code ' => self ::CARRIER_CODE ,
127
+ 'method_code ' => $ carrierMethodCode ,
128
+ 'label ' => self ::CARRIER_LABEL . ' - ' . $ carrierMethodLabel ,
129
+ ];
130
+ self ::assertEquals ($ addressesInformation [0 ]['selected_shipping_method ' ], $ expectedResult );
131
+ }
132
+
133
+ /**
134
+ * @return array
135
+ */
136
+ public function availableForCartShippingMethods (): array
137
+ {
138
+ $ shippingMethods = ['1DM ' , '1DA ' , '2DA ' , '3DS ' , 'GND ' ];
139
+
140
+ return $ this ->filterShippingMethodsByCodes ($ shippingMethods );
141
+ }
142
+
143
+ /**
144
+ * @return array
145
+ */
146
+ public function notAvailableForCartShippingMethods (): array
147
+ {
148
+ $ shippingMethods = ['1DML ' , '1DAL ' , '1DAPI ' , '1DP ' , '1DPL ' , '2DM ' , '2DML ' , '2DAL ' , 'GNDCOM ' , 'GNDRES ' , 'STD ' , 'XPR ' , 'WXS ' , 'XPRL ' , 'XDM ' , 'XDML ' , 'XPD ' ];
149
+
150
+ return $ this ->filterShippingMethodsByCodes ($ shippingMethods );
151
+ }
152
+
153
+ /**
154
+ * @param array $filter
155
+ * @return array
156
+ */
157
+ private function filterShippingMethodsByCodes (array $ filter ):array
158
+ {
159
+ $ result = [];
160
+ foreach ($ this ->getAllUpsShippingMethods () as $ shippingMethod ) {
161
+ if (in_array ($ shippingMethod [0 ], $ filter )) {
162
+ $ result [] = $ shippingMethod ;
163
+ }
164
+ }
165
+ return $ result ;
166
+ }
167
+
168
+ private function getAllUpsShippingMethods ():array
169
+ {
170
+ return [
171
+ ['1DM ' , 'Next Day Air Early AM ' ],
172
+ ['1DML ' , 'Next Day Air Early AM Letter ' ],
173
+ ['1DA ' , 'Next Day Air ' ],
174
+ ['1DAL ' , 'Next Day Air Letter ' ],
175
+ ['1DAPI ' , 'Next Day Air Intra (Puerto Rico) ' ],
176
+ ['1DP ' , 'Next Day Air Saver ' ],
177
+ ['1DPL ' , 'Next Day Air Saver Letter ' ],
178
+ ['2DM ' , '2nd Day Air AM ' ],
179
+ ['2DML ' , '2nd Day Air AM Letter ' ],
180
+ ['2DA ' , '2nd Day Air ' ],
181
+ ['2DAL ' , '2nd Day Air Letter ' ],
182
+ ['3DS ' , '3 Day Select ' ],
183
+ ['GND ' , 'Ground ' ],
184
+ ['GNDCOM ' , 'Ground Commercial ' ],
185
+ ['GNDRES ' , 'Ground Residential ' ],
186
+ ['STD ' , 'Canada Standard ' ],
187
+ ['XPR ' , 'Worldwide Express ' ],
188
+ ['WXS ' , 'Worldwide Express Saver ' ],
189
+ ['XPRL ' , 'Worldwide Express Letter ' ],
190
+ ['XDM ' , 'Worldwide Express Plus ' ],
191
+ ['XDML ' , 'Worldwide Express Plus Letter ' ],
192
+ ['XPD ' , 'Worldwide Expedited ' ],
193
+ ];
194
+ }
195
+
90
196
/**
91
197
* Generates query for setting the specified shipping method on cart
92
198
*
@@ -96,7 +202,7 @@ public function testSetUpsShippingMethod()
96
202
* @param string $methodCode
97
203
* @return string
98
204
*/
99
- private function getAddUpsShippingMethodQuery (
205
+ private function getQuery (
100
206
string $ maskedQuoteId ,
101
207
int $ shippingAddressId ,
102
208
string $ carrierCode ,
0 commit comments