@@ -56,6 +56,11 @@ class CarrierTest extends TestCase
56
56
*/
57
57
private $ logs = [];
58
58
59
+ /**
60
+ * @var \Magento\Ups\Model\UpsAuth|MockObject
61
+ */
62
+ private $ upsAuthMock ;
63
+
59
64
/**
60
65
* @inheritDoc
61
66
*/
@@ -71,7 +76,11 @@ function (string $message) {
71
76
$ this ->logs [] = $ message ;
72
77
}
73
78
);
74
- $ this ->carrier = Bootstrap::getObjectManager ()->create (Carrier::class, ['logger ' => $ this ->loggerMock ]);
79
+ $ this ->upsAuthMock = $ this ->getMockBuilder (\Magento \Ups \Model \UpsAuth::class)
80
+ ->disableOriginalConstructor ()
81
+ ->getMock ();
82
+ $ this ->carrier = Bootstrap::getObjectManager ()->create (Carrier::class, ['logger ' => $ this ->loggerMock ,
83
+ 'upsAuth ' => $ this ->upsAuthMock ]);
75
84
}
76
85
77
86
/**
@@ -135,18 +144,15 @@ public function testCollectFreeRates()
135
144
new Response (
136
145
200 ,
137
146
[],
138
- file_get_contents (__DIR__ . "/../_files/ups_rates_response_option9 .json " )
147
+ file_get_contents (__DIR__ . "/../_files/ups_rates_response_option5 .json " )
139
148
)
140
149
]
141
150
);
142
- $ upsAuthMock = $ this ->getMockBuilder (\Magento \Ups \Model \UpsAuth::class)
143
- ->disableOriginalConstructor ()
144
- ->getMock ();
145
- $ upsAuthMock ->method ('getAccessToken ' )
146
- ->willReturn ('abcdefghijklmnop ' );
147
151
152
+ $ this ->upsAuthMock ->method ('getAccessToken ' )
153
+ ->willReturn ('abcdefghijklmnop ' );
148
154
$ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
149
- $ this ->assertEquals ('19.19 ' , $ rates [0 ]->getPrice ());
155
+ $ this ->assertEquals ('115.01 ' , $ rates [0 ]->getPrice ());
150
156
$ this ->assertEquals ('03 ' , $ rates [0 ]->getMethod ());
151
157
}
152
158
@@ -176,12 +182,12 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
176
182
RateRequest::class,
177
183
[
178
184
'data ' => [
179
- 'dest_country ' => 'GB ' ,
180
- 'dest_postal ' => '01104 ' ,
185
+ 'dest_country ' => 'US ' ,
186
+ 'dest_postal ' => '90001 ' ,
181
187
'product ' => '11 ' ,
182
188
'action ' => 'Rate ' ,
183
189
'unit_measure ' => 'KGS ' ,
184
- 'base_currency ' => new DataObject (['code ' => 'GBP ' ])
190
+ 'base_currency ' => new DataObject (['code ' => 'USD ' ])
185
191
]
186
192
]
187
193
);
@@ -191,7 +197,7 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
191
197
new Response (
192
198
200 ,
193
199
[],
194
- file_get_contents (__DIR__ . "/../_files/ups_rates_response_option $ responseId.xml " )
200
+ file_get_contents (__DIR__ . "/../_files/ups_rates_response_option $ responseId.json " )
195
201
)
196
202
]
197
203
);
@@ -200,14 +206,16 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
200
206
$ this ->config ->setValue ('carriers/ups/include_taxes ' , $ tax , 'store ' );
201
207
$ this ->config ->setValue ('carriers/ups/allowed_methods ' , $ method , 'store ' );
202
208
209
+ $ this ->upsAuthMock ->method ('getAccessToken ' )
210
+ ->willReturn ('abcdefghijklmnop ' );
203
211
$ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
204
212
$ this ->assertEquals ($ price , $ rates [0 ]->getPrice ());
205
213
$ this ->assertEquals ($ method , $ rates [0 ]->getMethod ());
206
214
207
215
$ requestFound = false ;
208
216
foreach ($ this ->logs as $ log ) {
209
- if (mb_stripos ($ log , 'RatingServiceSelectionRequest ' ) &&
210
- mb_stripos ($ log , 'RatingServiceSelectionResponse ' )
217
+ if (mb_stripos ($ log , 'RateRequest ' ) &&
218
+ mb_stripos ($ log , 'RateResponse ' )
211
219
) {
212
220
$ requestFound = true ;
213
221
break ;
@@ -246,6 +254,8 @@ public function testCollectRatesWithoutAnyAllowedMethods(): void
246
254
]
247
255
);
248
256
$ this ->config ->setValue ('carriers/ups/allowed_methods ' , '' , 'store ' );
257
+ $ this ->upsAuthMock ->method ('getAccessToken ' )
258
+ ->willReturn ('abcdefghijklmnop ' );
249
259
$ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
250
260
$ this ->assertInstanceOf (Error::class, current ($ rates ));
251
261
$ this ->assertEquals (current ($ rates )['carrier_title ' ], $ this ->carrier ->getConfigData ('title ' ));
@@ -260,14 +270,10 @@ public function testCollectRatesWithoutAnyAllowedMethods(): void
260
270
public function collectRatesDataProvider ()
261
271
{
262
272
return [
263
- [0 , 0 , 1 , '11 ' , 6.45 ],
264
- [0 , 0 , 2 , '65 ' , 29.59 ],
265
- [0 , 1 , 3 , '11 ' , 7.74 ],
266
- [0 , 1 , 4 , '65 ' , 29.59 ],
267
- [1 , 0 , 5 , '11 ' , 9.35 ],
268
- [1 , 0 , 6 , '65 ' , 41.61 ],
269
- [1 , 1 , 7 , '11 ' , 11.22 ],
270
- [1 , 1 , 8 , '65 ' , 41.61 ],
273
+ [0 , 0 , 1 , '03 ' , 136.09 ],
274
+ [0 , 1 , 2 , '03 ' , 136.09 ],
275
+ [1 , 0 , 3 , '03 ' , 92.12 ],
276
+ [1 , 1 , 4 , '03 ' , 92.12 ],
271
277
];
272
278
}
273
279
@@ -289,14 +295,12 @@ public function collectRatesDataProvider()
289
295
public function testRequestToShipment (): void
290
296
{
291
297
//phpcs:disable Magento2.Functions.DiscouragedFunction
292
- $ expectedShipmentRequest = file_get_contents (__DIR__ . '/../_files/ShipmentConfirmRequest.xml ' );
293
- $ shipmentResponse = file_get_contents (__DIR__ . '/../_files/ShipmentConfirmResponse.xml ' );
294
- $ acceptResponse = file_get_contents (__DIR__ . '/../_files/ShipmentAcceptResponse.xml ' );
298
+ $ expectedShipmentRequest = str_replace ("\n" ,"" ,file_get_contents (__DIR__ . '/../_files/ShipmentConfirmRequest.json ' ));
299
+ $ shipmentResponse = file_get_contents (__DIR__ . '/../_files/ShipmentConfirmResponse.json ' );
295
300
//phpcs:enable Magento2.Functions.DiscouragedFunction
296
301
$ this ->httpClient ->nextResponses (
297
302
[
298
- new Response (200 , [], $ shipmentResponse ),
299
- new Response (200 , [], $ acceptResponse )
303
+ new Response (200 , [], $ shipmentResponse )
300
304
]
301
305
);
302
306
$ this ->httpClient ->clearRequests ();
@@ -348,24 +352,18 @@ public function testRequestToShipment(): void
348
352
349
353
$ requests = $ this ->httpClient ->getRequests ();
350
354
$ this ->assertNotEmpty ($ requests );
351
- $ shipmentRequest = $ this -> extractShipmentRequest ( $ requests [0 ]->getBody () );
355
+ $ shipmentRequest = $ requests [0 ]->getBody ();
352
356
$ this ->assertEquals (
353
- $ this -> formatXml ( $ expectedShipmentRequest) ,
354
- $ this -> formatXml ( $ shipmentRequest)
357
+ $ expectedShipmentRequest ,
358
+ $ shipmentRequest
355
359
);
356
-
357
360
$ this ->assertEmpty ($ result ->getErrors ());
358
361
$ this ->assertNotEmpty ($ result ->getInfo ());
359
362
$ this ->assertEquals (
360
- '1Z207W886698856557 ' ,
363
+ '1ZXXXXXXXXXXXXXXXX ' ,
361
364
$ result ->getInfo ()[0 ]['tracking_number ' ],
362
365
'Tracking Number must match. '
363
366
);
364
- $ this ->assertEquals (
365
- '2V467W886398839541 ' ,
366
- $ result ->getInfo ()[1 ]['tracking_number ' ],
367
- 'Tracking Number must match. '
368
- );
369
367
$ this ->httpClient ->clearRequests ();
370
368
}
371
369
@@ -411,37 +409,4 @@ public function testGetRatesWithHttpException(): void
411
409
412
410
$ this ->assertEquals ($ error , $ resultRate );
413
411
}
414
-
415
- /**
416
- * Extracts shipment request.
417
- *
418
- * @param string $requestBody
419
- * @return string
420
- */
421
- private function extractShipmentRequest (string $ requestBody ): string
422
- {
423
- $ resultXml = '' ;
424
- $ pattern = '%(<\?xml version="1.0"\?>\n<ShipmentConfirmRequest)(.*)$%im ' ;
425
- if (preg_match ($ pattern , $ requestBody , $ result )) {
426
- $ resultXml = array_shift ($ result );
427
- }
428
-
429
- return $ resultXml ;
430
- }
431
-
432
- /**
433
- * Format XML string.
434
- *
435
- * @param string $xmlString
436
- * @return string
437
- */
438
- private function formatXml (string $ xmlString ): string
439
- {
440
- $ xmlDocument = new \DOMDocument ('1.0 ' );
441
- $ xmlDocument ->preserveWhiteSpace = false ;
442
- $ xmlDocument ->formatOutput = true ;
443
- $ xmlDocument ->loadXML ($ xmlString );
444
-
445
- return $ xmlDocument ->saveXML ();
446
- }
447
412
}
0 commit comments