@@ -130,14 +130,6 @@ protected function setUp()
130
130
131
131
$ this ->scope = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
132
132
133
- $ xmlElFactory = $ this ->getXmlFactory ();
134
- $ rateFactory = $ this ->getRateFactory ();
135
- $ rateMethodFactory = $ this ->getRateMethodFactory ();
136
- $ httpClientFactory = $ this ->getHttpClientFactory ();
137
- $ configReader = $ this ->getConfigReader ();
138
- $ readFactory = $ this ->getReadFactory ();
139
- $ storeManager = $ this ->getStoreManager ();
140
-
141
133
$ this ->error = $ this ->getMockBuilder (Error::class)
142
134
->setMethods (['setCarrier ' , 'setCarrierTitle ' , 'setErrorMessage ' ])
143
135
->getMock ();
@@ -148,8 +140,6 @@ protected function setUp()
148
140
$ this ->errorFactory ->method ('create ' )
149
141
->willReturn ($ this ->error );
150
142
151
- $ carrierHelper = $ this ->getCarrierHelper ();
152
-
153
143
$ this ->xmlValidator = $ this ->getMockBuilder (XmlValidator::class)
154
144
->disableOriginalConstructor ()
155
145
->getMock ();
@@ -174,17 +164,17 @@ protected function setUp()
174
164
Carrier::class,
175
165
[
176
166
'scopeConfig ' => $ this ->scope ,
177
- 'xmlSecurity ' => new Security (),
178
- 'logger ' => $ this ->logger ,
179
- 'xmlElFactory ' => $ xmlElFactory ,
180
- 'rateFactory ' => $ rateFactory ,
181
167
'rateErrorFactory ' => $ this ->errorFactory ,
182
- 'rateMethodFactory ' => $ rateMethodFactory ,
183
- 'httpClientFactory ' => $ httpClientFactory ,
184
- 'readFactory ' => $ readFactory ,
185
- 'storeManager ' => $ storeManager ,
186
- 'configReader ' => $ configReader ,
187
- 'carrierHelper ' => $ carrierHelper ,
168
+ 'logger ' => $ this ->logger ,
169
+ 'xmlSecurity ' => new Security (),
170
+ 'xmlElFactory ' => $ this ->getXmlFactory (),
171
+ 'rateFactory ' => $ this ->getRateFactory (),
172
+ 'rateMethodFactory ' => $ this ->getRateMethodFactory (),
173
+ 'carrierHelper ' => $ this ->getCarrierHelper (),
174
+ 'configReader ' => $ this ->getConfigReader (),
175
+ 'storeManager ' => $ this ->getStoreManager (),
176
+ 'readFactory ' => $ this ->getReadFactory (),
177
+ 'httpClientFactory ' => $ this ->getHttpClientFactory (),
188
178
'data ' => ['id ' => 'dhl ' , 'store ' => '1 ' ],
189
179
'xmlValidator ' => $ this ->xmlValidator ,
190
180
'coreDate ' => $ this ->coreDateMock ,
@@ -212,15 +202,15 @@ public function scopeConfigGetValue($path)
212
202
'carriers/dhl/content_type ' => 'N ' ,
213
203
'carriers/dhl/nondoc_methods ' => '1,3,4,8,P,Q,E,F,H,J,M,V,Y ' ,
214
204
'carriers/dhl/showmethod ' => 1 ,
215
- 'carriers/dhl/title ' => 'dhl Title ' ,
205
+ 'carriers/dhl/title ' => 'DHL Title ' ,
216
206
'carriers/dhl/specificerrmsg ' => 'dhl error message ' ,
217
207
'carriers/dhl/unit_of_measure ' => 'K ' ,
218
208
'carriers/dhl/size ' => '1 ' ,
219
209
'carriers/dhl/height ' => '1.6 ' ,
220
210
'carriers/dhl/width ' => '1.6 ' ,
221
211
'carriers/dhl/depth ' => '1.6 ' ,
222
212
'carriers/dhl/debug ' => 1 ,
223
- 'shipping/origin/country_id ' => 'GB ' ,
213
+ 'shipping/origin/country_id ' => 'GB '
224
214
];
225
215
return isset ($ pathMap [$ path ]) ? $ pathMap [$ path ] : null ;
226
216
}
@@ -276,22 +266,29 @@ protected function _invokePrepareShippingLabelContent(\SimpleXMLElement $xml)
276
266
return $ method ->invoke ($ model , $ xml );
277
267
}
278
268
269
+ /**
270
+ * Tests that valid rates are returned when sending a quotes request.
271
+ */
279
272
public function testCollectRates ()
280
273
{
274
+ $ requestData = require __DIR__ . '/_files/dhl_quote_request_data.php ' ;
275
+ $ responseXml = file_get_contents (__DIR__ . '/_files/dhl_quote_response.xml ' );
276
+
281
277
$ this ->scope ->method ('getValue ' )
282
278
->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
283
279
284
280
$ this ->scope ->method ('isSetFlag ' )
285
281
->willReturn (true );
286
282
287
283
$ this ->httpResponse ->method ('getBody ' )
288
- ->willReturn (file_get_contents ( __DIR__ . ' /_files/success_dhl_response_rates.xml ' ) );
284
+ ->willReturn ($ responseXml );
289
285
290
- /** @var RateRequest $request */
291
- $ request = $ this ->objectManager ->getObject (
292
- RateRequest::class,
293
- require __DIR__ . '/_files/rates_request_data_dhl.php '
294
- );
286
+ $ this ->coreDateMock ->method ('date ' )
287
+ ->willReturnCallback (function () {
288
+ return date (\DATE_RFC3339 );
289
+ });
290
+
291
+ $ request = $ this ->objectManager ->getObject (RateRequest::class, $ requestData );
295
292
296
293
$ reflectionClass = new \ReflectionObject ($ this ->httpClient );
297
294
$ rawPostData = $ reflectionClass ->getProperty ('raw_post_data ' );
@@ -301,13 +298,27 @@ public function testCollectRates()
301
298
->method ('debug ' )
302
299
->with ($ this ->stringContains ('<SiteID>****</SiteID><Password>****</Password> ' ));
303
300
304
- self ::assertNotEmpty ($ this ->model ->collectRates ($ request )->getAllRates ());
305
- self ::assertContains ('<Weight>18.223</Weight> ' , $ rawPostData ->getValue ($ this ->httpClient ));
306
- self ::assertContains ('<Height>0.630</Height> ' , $ rawPostData ->getValue ($ this ->httpClient ));
307
- self ::assertContains ('<Width>0.630</Width> ' , $ rawPostData ->getValue ($ this ->httpClient ));
308
- self ::assertContains ('<Depth>0.630</Depth> ' , $ rawPostData ->getValue ($ this ->httpClient ));
301
+ $ expectedRates = require __DIR__ . '/_files/dhl_quote_response_rates.php ' ;
302
+ $ actualRates = $ this ->model ->collectRates ($ request )->getAllRates ();
303
+
304
+ self ::assertEquals (count ($ expectedRates ), count ($ actualRates ));
305
+
306
+ foreach ($ actualRates as $ i => $ actualRate ) {
307
+ $ actualRate = $ actualRate ->getData ();
308
+ unset($ actualRate ['method_title ' ]);
309
+ self ::assertEquals ($ expectedRates [$ i ], $ actualRate );
310
+ }
311
+
312
+ $ requestXml = $ rawPostData ->getValue ($ this ->httpClient );
313
+ self ::assertContains ('<Weight>18.223</Weight> ' , $ requestXml );
314
+ self ::assertContains ('<Height>0.630</Height> ' , $ requestXml );
315
+ self ::assertContains ('<Width>0.630</Width> ' , $ requestXml );
316
+ self ::assertContains ('<Depth>0.630</Depth> ' , $ requestXml );
309
317
}
310
318
319
+ /**
320
+ * Tests that an error is returned when attempting to collect rates for an inactive shipping method.
321
+ */
311
322
public function testCollectRatesErrorMessage ()
312
323
{
313
324
$ this ->scope ->method ('getValue ' )
@@ -325,16 +336,6 @@ public function testCollectRatesErrorMessage()
325
336
$ this ->assertSame ($ this ->error , $ this ->model ->collectRates ($ request ));
326
337
}
327
338
328
- public function testCollectRatesFail ()
329
- {
330
- $ this ->scope ->expects ($ this ->once ())->method ('isSetFlag ' )->willReturn (true );
331
-
332
- $ request = new RateRequest ();
333
- $ request ->setPackageWeight (1 );
334
-
335
- $ this ->assertFalse (false , $ this ->model ->collectRates ($ request ));
336
- }
337
-
338
339
/**
339
340
* Test request to shipment sends valid xml values.
340
341
*
@@ -520,34 +521,104 @@ public function dhlProductsDataProvider() : array
520
521
}
521
522
522
523
/**
523
- * Tests that the built message reference string is of the appropriate format.
524
+ * Tests that the built MessageReference string is of the appropriate format.
525
+ *
526
+ * @dataProvider buildMessageReferenceDataProvider
527
+ * @param $servicePrefix
528
+ * @throws \ReflectionException
529
+ */
530
+ public function testBuildMessageReference ($ servicePrefix )
531
+ {
532
+ $ method = new \ReflectionMethod ($ this ->model , 'buildMessageReference ' );
533
+ $ method ->setAccessible (true );
534
+
535
+ $ messageReference = $ method ->invoke ($ this ->model , $ servicePrefix );
536
+ $ this ->assertGreaterThanOrEqual (28 , strlen ($ messageReference ));
537
+ $ this ->assertLessThanOrEqual (32 , strlen ($ messageReference ));
538
+ }
539
+
540
+ /**
541
+ * @return array
542
+ */
543
+ public function buildMessageReferenceDataProvider ()
544
+ {
545
+ return [
546
+ 'quote_prefix ' => ['QUOT ' ],
547
+ 'shipval_prefix ' => ['SHIP ' ],
548
+ 'tracking_prefix ' => ['TRCK ' ]
549
+ ];
550
+ }
551
+
552
+ /**
553
+ * Tests that an exception is thrown when an invalid service prefix is provided.
524
554
*
525
555
* @expectedException \Magento\Framework\Exception\LocalizedException
526
556
* @expectedExceptionMessage Invalid service prefix
527
- * @throws \ReflectionException
528
557
*/
529
- public function testBuildMessageReference ()
558
+ public function testBuildMessageReferenceInvalidPrefix ()
530
559
{
531
560
$ method = new \ReflectionMethod ($ this ->model , 'buildMessageReference ' );
532
561
$ method ->setAccessible (true );
533
562
534
- $ constPrefixQuote = new \ReflectionClassConstant ($ this ->model , 'SERVICE_PREFIX_QUOTE ' );
535
- $ constPrefixShipval = new \ReflectionClassConstant ($ this ->model , 'SERVICE_PREFIX_SHIPVAL ' );
536
- $ constPrefixTracking = new \ReflectionClassConstant ($ this ->model , 'SERVICE_PREFIX_TRACKING ' );
563
+ $ method ->invoke ($ this ->model , 'INVALID ' );
564
+ }
537
565
538
- $ msgRefQuote = $ method ->invoke ($ this ->model , $ constPrefixQuote ->getValue ());
539
- self ::assertGreaterThanOrEqual (28 , strlen ($ msgRefQuote ));
540
- self ::assertLessThanOrEqual (32 , strlen ($ msgRefQuote ));
566
+ /**
567
+ * Tests that the built software name string is of the appropriate format.
568
+ *
569
+ * @dataProvider buildSoftwareNameDataProvider
570
+ * @param $productName
571
+ * @throws \ReflectionException
572
+ */
573
+ public function testBuildSoftwareName ($ productName )
574
+ {
575
+ $ method = new \ReflectionMethod ($ this ->model , 'buildSoftwareName ' );
576
+ $ method ->setAccessible (true );
541
577
542
- $ msgRefShip = $ method ->invoke ($ this ->model , $ constPrefixShipval ->getValue ());
543
- self ::assertGreaterThanOrEqual (28 , strlen ($ msgRefShip ));
544
- self ::assertLessThanOrEqual (32 , strlen ($ msgRefShip ));
578
+ $ this ->productMetadataMock ->method ('getName ' )->willReturn ($ productName );
545
579
546
- $ msgRefTrack = $ method ->invoke ($ this ->model , $ constPrefixTracking -> getValue () );
547
- self :: assertGreaterThanOrEqual ( 28 , strlen ($ msgRefTrack ));
548
- self :: assertLessThanOrEqual ( 32 , strlen ( $ msgRefTrack ));
580
+ $ softwareName = $ method ->invoke ($ this ->model );
581
+ $ this -> assertLessThanOrEqual ( 30 , strlen ($ softwareName ));
582
+ }
549
583
550
- $ method ->invoke ($ this ->model , 'TEST ' );
584
+ /**
585
+ * @return array
586
+ */
587
+ public function buildSoftwareNameDataProvider ()
588
+ {
589
+ return [
590
+ 'valid_length ' => ['Magento ' ],
591
+ 'exceeds_length ' => ['Product_Name_Longer_Than_30_Char ' ]
592
+ ];
593
+ }
594
+
595
+ /**
596
+ * Tests that the built software version string is of the appropriate format.
597
+ *
598
+ * @dataProvider buildSoftwareVersionProvider
599
+ * @param $productVersion
600
+ * @throws \ReflectionException
601
+ */
602
+ public function testBuildSoftwareVersion ($ productVersion )
603
+ {
604
+ $ method = new \ReflectionMethod ($ this ->model , 'buildSoftwareVersion ' );
605
+ $ method ->setAccessible (true );
606
+
607
+ $ this ->productMetadataMock ->method ('getVersion ' )->willReturn ($ productVersion );
608
+
609
+ $ softwareVersion = $ method ->invoke ($ this ->model );
610
+ $ this ->assertLessThanOrEqual (10 , strlen ($ softwareVersion ));
611
+ }
612
+
613
+ /**
614
+ * @return array
615
+ */
616
+ public function buildSoftwareVersionProvider ()
617
+ {
618
+ return [
619
+ 'valid_length ' => ['2.3.1 ' ],
620
+ 'exceeds_length ' => ['dev-MC-1000 ' ]
621
+ ];
551
622
}
552
623
553
624
/**
@@ -608,14 +679,18 @@ private function getRateMethodFactory(): MockObject
608
679
->disableOriginalConstructor ()
609
680
->setMethods (['create ' ])
610
681
->getMock ();
611
- $ rateMethod = $ this ->getMockBuilder (Method::class)
612
- ->disableOriginalConstructor ()
613
- ->setMethods (['setPrice ' ])
614
- ->getMock ();
615
- $ rateMethod ->method ('setPrice ' )
616
- ->willReturnSelf ();
682
+
617
683
$ rateMethodFactory ->method ('create ' )
618
- ->willReturn ($ rateMethod );
684
+ ->willReturnCallback (function () {
685
+ $ rateMethod = $ this ->getMockBuilder (Method::class)
686
+ ->disableOriginalConstructor ()
687
+ ->setMethods (['setPrice ' ])
688
+ ->getMock ();
689
+ $ rateMethod ->method ('setPrice ' )
690
+ ->willReturnSelf ();
691
+
692
+ return $ rateMethod ;
693
+ });
619
694
620
695
return $ rateMethodFactory ;
621
696
}
0 commit comments