5
5
*/
6
6
namespace Magento \Dhl \Test \Unit \Model ;
7
7
8
+ use Magento \Dhl \Model \Carrier ;
8
9
use Magento \Framework \App \Config \ScopeConfigInterface ;
9
10
use Magento \Framework \HTTP \ZendClient ;
10
11
use Magento \Framework \HTTP \ZendClientFactory ;
13
14
use Magento \Quote \Model \Quote \Address \RateRequest ;
14
15
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
15
16
use Magento \Dhl \Model \Validator \XmlValidator ;
17
+ use Magento \Store \Model \ScopeInterface ;
16
18
17
19
/**
18
20
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -30,7 +32,7 @@ class CarrierTest extends \PHPUnit\Framework\TestCase
30
32
private $ httpResponse ;
31
33
32
34
/**
33
- * @var \Magento\Dhl\Model\ Carrier
35
+ * @var Carrier
34
36
*/
35
37
private $ model ;
36
38
@@ -92,8 +94,6 @@ protected function setUp()
92
94
->getMock ();
93
95
94
96
$ this ->scope = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
95
- $ this ->scope ->method ('getValue ' )
96
- ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
97
97
98
98
// xml element factory
99
99
$ xmlElFactory = $ this ->getMockBuilder (
@@ -219,7 +219,7 @@ function ($data) {
219
219
->getMock ();
220
220
221
221
$ this ->model = $ this ->objectManager ->getObject (
222
- \ Magento \ Dhl \ Model \ Carrier::class,
222
+ Carrier::class,
223
223
[
224
224
'scopeConfig ' => $ this ->scope ,
225
225
'xmlSecurity ' => new Security (),
@@ -314,14 +314,17 @@ public function prepareShippingLabelContentExceptionDataProvider()
314
314
*/
315
315
protected function _invokePrepareShippingLabelContent (\SimpleXMLElement $ xml )
316
316
{
317
- $ model = $ this ->objectManager ->getObject (\ Magento \ Dhl \ Model \ Carrier::class);
317
+ $ model = $ this ->objectManager ->getObject (Carrier::class);
318
318
$ method = new \ReflectionMethod ($ model , '_prepareShippingLabelContent ' );
319
319
$ method ->setAccessible (true );
320
320
return $ method ->invoke ($ model , $ xml );
321
321
}
322
322
323
323
public function testCollectRates ()
324
324
{
325
+ $ this ->scope ->method ('getValue ' )
326
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
327
+
325
328
$ this ->scope ->method ('isSetFlag ' )
326
329
->willReturn (true );
327
330
@@ -348,6 +351,9 @@ public function testCollectRates()
348
351
349
352
public function testCollectRatesErrorMessage ()
350
353
{
354
+ $ this ->scope ->method ('getValue ' )
355
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
356
+
351
357
$ this ->scope ->expects ($ this ->once ())->method ('isSetFlag ' )->willReturn (false );
352
358
353
359
$ this ->error ->expects ($ this ->once ())->method ('setCarrier ' )->with ('dhl ' );
@@ -375,6 +381,9 @@ public function testCollectRatesFail()
375
381
*/
376
382
public function testRequestToShipment ()
377
383
{
384
+ $ this ->scope ->method ('getValue ' )
385
+ ->willReturnCallback ([$ this , 'scopeConfigGetValue ' ]);
386
+
378
387
$ this ->httpResponse ->method ('getBody ' )
379
388
->willReturn (utf8_encode (file_get_contents (__DIR__ . '/_files/response_shipping_label.xml ' )));
380
389
$ this ->xmlValidator ->expects ($ this ->any ())->method ('validate ' );
@@ -389,14 +398,14 @@ public function testRequestToShipment()
389
398
'weight_units ' => 'POUND ' ,
390
399
'weight ' => '0.454000000001 ' ,
391
400
'customs_value ' => '10.00 ' ,
392
- 'container ' => \ Magento \ Dhl \ Model \ Carrier::DHL_CONTENT_TYPE_NON_DOC ,
401
+ 'container ' => Carrier::DHL_CONTENT_TYPE_NON_DOC ,
393
402
],
394
403
'items ' => [
395
404
'item1 ' => [
396
405
'name ' => 'item_name ' ,
397
406
],
398
407
],
399
- ]
408
+ ],
400
409
];
401
410
402
411
$ order = $ this ->getMockBuilder (\Magento \Sales \Model \Order::class)
@@ -432,20 +441,75 @@ public function testRequestToShipment()
432
441
}
433
442
434
443
/**
435
- * Data provider to testRequestToShipment
444
+ * Test that shipping label request for origin country from AP region doesn't contain restricted fields.
436
445
*
437
- * @return array
446
+ * @return void
438
447
*/
439
- public function requestToShipmentDataProvider ()
448
+ public function testShippingLabelRequestForAsiaPacificRegion ()
440
449
{
441
- return [
442
- [
443
- 'GB '
450
+ $ this ->scope ->method ('getValue ' )
451
+ ->willReturnMap (
452
+ [
453
+ ['shipping/origin/country_id ' , ScopeInterface::SCOPE_STORE , null , 'SG ' ],
454
+ ['carriers/dhl/gateway_url ' , ScopeInterface::SCOPE_STORE , null , 'https://xmlpi-ea.dhl.com ' ],
455
+ ]
456
+ );
457
+
458
+ $ this ->httpResponse ->method ('getBody ' )
459
+ ->willReturn (utf8_encode (file_get_contents (__DIR__ . '/_files/response_shipping_label.xml ' )));
460
+
461
+ $ packages = [
462
+ 'package ' => [
463
+ 'params ' => [
464
+ 'width ' => '1 ' ,
465
+ 'length ' => '1 ' ,
466
+ 'height ' => '1 ' ,
467
+ 'dimension_units ' => 'INCH ' ,
468
+ 'weight_units ' => 'POUND ' ,
469
+ 'weight ' => '0.45 ' ,
470
+ 'customs_value ' => '10.00 ' ,
471
+ 'container ' => Carrier::DHL_CONTENT_TYPE_NON_DOC ,
472
+ ],
473
+ 'items ' => [
474
+ 'item1 ' => [
475
+ 'name ' => 'item_name ' ,
476
+ ],
477
+ ],
444
478
],
445
- [
446
- null
447
- ]
448
479
];
480
+
481
+ $ this ->request ->method ('getPackages ' )->willReturn ($ packages );
482
+ $ this ->request ->method ('getOrigCountryId ' )->willReturn ('SG ' );
483
+ $ this ->request ->method ('setPackages ' )->willReturnSelf ();
484
+ $ this ->request ->method ('setPackageWeight ' )->willReturnSelf ();
485
+ $ this ->request ->method ('setPackageValue ' )->willReturnSelf ();
486
+ $ this ->request ->method ('setValueWithDiscount ' )->willReturnSelf ();
487
+ $ this ->request ->method ('setPackageCustomsValue ' )->willReturnSelf ();
488
+
489
+ $ result = $ this ->model ->requestToShipment ($ this ->request );
490
+
491
+ $ reflectionClass = new \ReflectionObject ($ this ->httpClient );
492
+ $ rawPostData = $ reflectionClass ->getProperty ('raw_post_data ' );
493
+ $ rawPostData ->setAccessible (true );
494
+
495
+ $ this ->assertNotNull ($ result );
496
+ $ requestXml = $ rawPostData ->getValue ($ this ->httpClient );
497
+
498
+ $ this ->assertNotContains (
499
+ 'NewShipper ' ,
500
+ $ requestXml ,
501
+ 'NewShipper is restricted field for AP region '
502
+ );
503
+ $ this ->assertNotContains (
504
+ 'Division ' ,
505
+ $ requestXml ,
506
+ 'Division is restricted field for AP region '
507
+ );
508
+ $ this ->assertNotContains (
509
+ 'RegisteredAccount ' ,
510
+ $ requestXml ,
511
+ 'RegisteredAccount is restricted field for AP region '
512
+ );
449
513
}
450
514
451
515
/**
@@ -466,7 +530,7 @@ public function dhlProductsDataProvider() : array
466
530
{
467
531
return [
468
532
'doc ' => [
469
- 'docType ' => \ Magento \ Dhl \ Model \ Carrier::DHL_CONTENT_TYPE_DOC ,
533
+ 'docType ' => Carrier::DHL_CONTENT_TYPE_DOC ,
470
534
'products ' => [
471
535
'2 ' => 'Easy shop ' ,
472
536
'5 ' => 'Sprintline ' ,
@@ -488,10 +552,10 @@ public function dhlProductsDataProvider() : array
488
552
'S ' => 'Same day ' ,
489
553
'T ' => 'Express 12:00 ' ,
490
554
'X ' => 'Express envelope ' ,
491
- ]
555
+ ],
492
556
],
493
557
'non-doc ' => [
494
- 'docType ' => \ Magento \ Dhl \ Model \ Carrier::DHL_CONTENT_TYPE_NON_DOC ,
558
+ 'docType ' => Carrier::DHL_CONTENT_TYPE_NON_DOC ,
495
559
'products ' => [
496
560
'1 ' => 'Domestic express 12:00 ' ,
497
561
'3 ' => 'Easy shop ' ,
@@ -506,8 +570,8 @@ public function dhlProductsDataProvider() : array
506
570
'M ' => 'Express 10:30 ' ,
507
571
'V ' => 'Europack ' ,
508
572
'Y ' => 'Express 12:00 ' ,
509
- ]
510
- ]
573
+ ],
574
+ ],
511
575
];
512
576
}
513
577
}
0 commit comments