@@ -56,6 +56,11 @@ class CarrierTest extends TestCase
5656 */
5757 private $ logs = [];
5858
59+ /**
60+ * @var \Magento\Ups\Model\UpsAuth|MockObject
61+ */
62+ private $ upsAuthMock ;
63+
5964 /**
6065 * @inheritDoc
6166 */
@@ -71,7 +76,11 @@ function (string $message) {
7176 $ this ->logs [] = $ message ;
7277 }
7378 );
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 ]);
7584 }
7685
7786 /**
@@ -135,18 +144,15 @@ public function testCollectFreeRates()
135144 new Response (
136145 200 ,
137146 [],
138- file_get_contents (__DIR__ . "/../_files/ups_rates_response_option9 .json " )
147+ file_get_contents (__DIR__ . "/../_files/ups_rates_response_option5 .json " )
139148 )
140149 ]
141150 );
142- $ upsAuthMock = $ this ->getMockBuilder (\Magento \Ups \Model \UpsAuth::class)
143- ->disableOriginalConstructor ()
144- ->getMock ();
145- $ upsAuthMock ->method ('getAccessToken ' )
146- ->willReturn ('abcdefghijklmnop ' );
147151
152+ $ this ->upsAuthMock ->method ('getAccessToken ' )
153+ ->willReturn ('abcdefghijklmnop ' );
148154 $ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
149- $ this ->assertEquals ('19.19 ' , $ rates [0 ]->getPrice ());
155+ $ this ->assertEquals ('115.01 ' , $ rates [0 ]->getPrice ());
150156 $ this ->assertEquals ('03 ' , $ rates [0 ]->getMethod ());
151157 }
152158
@@ -176,12 +182,12 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
176182 RateRequest::class,
177183 [
178184 'data ' => [
179- 'dest_country ' => 'GB ' ,
180- 'dest_postal ' => '01104 ' ,
185+ 'dest_country ' => 'US ' ,
186+ 'dest_postal ' => '90001 ' ,
181187 'product ' => '11 ' ,
182188 'action ' => 'Rate ' ,
183189 'unit_measure ' => 'KGS ' ,
184- 'base_currency ' => new DataObject (['code ' => 'GBP ' ])
190+ 'base_currency ' => new DataObject (['code ' => 'USD ' ])
185191 ]
186192 ]
187193 );
@@ -191,7 +197,7 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
191197 new Response (
192198 200 ,
193199 [],
194- file_get_contents (__DIR__ . "/../_files/ups_rates_response_option $ responseId.xml " )
200+ file_get_contents (__DIR__ . "/../_files/ups_rates_response_option $ responseId.json " )
195201 )
196202 ]
197203 );
@@ -200,14 +206,16 @@ public function testCollectRates(int $negotiable, int $tax, int $responseId, str
200206 $ this ->config ->setValue ('carriers/ups/include_taxes ' , $ tax , 'store ' );
201207 $ this ->config ->setValue ('carriers/ups/allowed_methods ' , $ method , 'store ' );
202208
209+ $ this ->upsAuthMock ->method ('getAccessToken ' )
210+ ->willReturn ('abcdefghijklmnop ' );
203211 $ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
204212 $ this ->assertEquals ($ price , $ rates [0 ]->getPrice ());
205213 $ this ->assertEquals ($ method , $ rates [0 ]->getMethod ());
206214
207215 $ requestFound = false ;
208216 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 ' )
211219 ) {
212220 $ requestFound = true ;
213221 break ;
@@ -246,6 +254,8 @@ public function testCollectRatesWithoutAnyAllowedMethods(): void
246254 ]
247255 );
248256 $ this ->config ->setValue ('carriers/ups/allowed_methods ' , '' , 'store ' );
257+ $ this ->upsAuthMock ->method ('getAccessToken ' )
258+ ->willReturn ('abcdefghijklmnop ' );
249259 $ rates = $ this ->carrier ->collectRates ($ request )->getAllRates ();
250260 $ this ->assertInstanceOf (Error::class, current ($ rates ));
251261 $ this ->assertEquals (current ($ rates )['carrier_title ' ], $ this ->carrier ->getConfigData ('title ' ));
@@ -260,14 +270,10 @@ public function testCollectRatesWithoutAnyAllowedMethods(): void
260270 public function collectRatesDataProvider ()
261271 {
262272 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 ],
271277 ];
272278 }
273279
@@ -289,14 +295,12 @@ public function collectRatesDataProvider()
289295 public function testRequestToShipment (): void
290296 {
291297 //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 ' );
295300 //phpcs:enable Magento2.Functions.DiscouragedFunction
296301 $ this ->httpClient ->nextResponses (
297302 [
298- new Response (200 , [], $ shipmentResponse ),
299- new Response (200 , [], $ acceptResponse )
303+ new Response (200 , [], $ shipmentResponse )
300304 ]
301305 );
302306 $ this ->httpClient ->clearRequests ();
@@ -348,24 +352,18 @@ public function testRequestToShipment(): void
348352
349353 $ requests = $ this ->httpClient ->getRequests ();
350354 $ this ->assertNotEmpty ($ requests );
351- $ shipmentRequest = $ this -> extractShipmentRequest ( $ requests [0 ]->getBody () );
355+ $ shipmentRequest = $ requests [0 ]->getBody ();
352356 $ this ->assertEquals (
353- $ this -> formatXml ( $ expectedShipmentRequest) ,
354- $ this -> formatXml ( $ shipmentRequest)
357+ $ expectedShipmentRequest ,
358+ $ shipmentRequest
355359 );
356-
357360 $ this ->assertEmpty ($ result ->getErrors ());
358361 $ this ->assertNotEmpty ($ result ->getInfo ());
359362 $ this ->assertEquals (
360- '1Z207W886698856557 ' ,
363+ '1ZXXXXXXXXXXXXXXXX ' ,
361364 $ result ->getInfo ()[0 ]['tracking_number ' ],
362365 'Tracking Number must match. '
363366 );
364- $ this ->assertEquals (
365- '2V467W886398839541 ' ,
366- $ result ->getInfo ()[1 ]['tracking_number ' ],
367- 'Tracking Number must match. '
368- );
369367 $ this ->httpClient ->clearRequests ();
370368 }
371369
@@ -411,37 +409,4 @@ public function testGetRatesWithHttpException(): void
411409
412410 $ this ->assertEquals ($ error , $ resultRate );
413411 }
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- }
447412}
0 commit comments