@@ -442,17 +442,8 @@ private function getExpectedLabelRequestXml(
442442 */
443443 public function testCollectRates ()
444444 {
445- $ requestData = $ this ->getRequestData ();
446- //phpcs:disable Magento2.Functions.DiscouragedFunction
447- $ response = new Response (
448- 200 ,
449- [],
450- file_get_contents (__DIR__ . '/../_files/dhl_quote_response.xml ' )
451- );
452- //phpcs:enable Magento2.Functions.DiscouragedFunction
453- $ this ->httpClient ->nextResponses (array_fill (0 , Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1 , $ response ));
454- /** @var RateRequest $request */
455- $ request = Bootstrap::getObjectManager ()->create (RateRequest::class, $ requestData );
445+ $ this ->setNextResponse (__DIR__ . '/../_files/dhl_quote_response.xml ' );
446+ $ request = $ this ->createRequest ();
456447 $ expectedRates = [
457448 ['carrier ' => 'dhl ' , 'carrier_title ' => 'DHL Title ' , 'cost ' => 45.85 , 'method ' => 'E ' , 'price ' => 45.85 ],
458449 ['carrier ' => 'dhl ' , 'carrier_title ' => 'DHL Title ' , 'cost ' => 35.26 , 'method ' => 'Q ' , 'price ' => 35.26 ],
@@ -487,11 +478,9 @@ public function testCollectRates()
487478 */
488479 public function testCollectRatesWithoutDimensions (?string $ size , ?string $ height , ?string $ width , ?string $ depth )
489480 {
490- $ requestData = $ this ->getRequestData ();
491481 $ this ->setDhlConfig (['size ' => $ size , 'height ' => $ height , 'width ' => $ width , 'depth ' => $ depth ]);
492482
493- /** @var RateRequest $request */
494- $ request = Bootstrap::getObjectManager ()->create (RateRequest::class, $ requestData );
483+ $ request = $ this ->createRequest ();
495484 $ this ->dhlCarrier = Bootstrap::getObjectManager ()->create (Carrier::class);
496485 $ this ->dhlCarrier ->collectRates ($ request )->getAllRates ();
497486
@@ -511,15 +500,13 @@ public function testCollectRatesWithoutDimensions(?string $size, ?string $height
511500 public function testGetRatesWithHttpException (): void
512501 {
513502 $ this ->setDhlConfig (['showmethod ' => 1 ]);
514- $ requestData = $ this ->getRequestData ();
515503 $ deferredResponse = $ this ->getMockBuilder (HttpResponseDeferredInterface::class)
516504 ->onlyMethods (['get ' ])
517505 ->getMockForAbstractClass ();
518506 $ exception = new HttpException ('Exception message ' );
519507 $ deferredResponse ->method ('get ' )->willThrowException ($ exception );
520508 $ this ->httpClient ->setDeferredResponseMock ($ deferredResponse );
521- /** @var RateRequest $request */
522- $ request = Bootstrap::getObjectManager ()->create (RateRequest::class, $ requestData );
509+ $ request = $ this ->createRequest ();
523510 $ this ->dhlCarrier = Bootstrap::getObjectManager ()->create (Carrier::class);
524511 $ resultRate = $ this ->dhlCarrier ->collectRates ($ request )->getAllRates ()[0 ];
525512 $ error = Bootstrap::getObjectManager ()->get (Error::class);
@@ -563,6 +550,77 @@ private function setDhlConfig(array $params)
563550 }
564551 }
565552
553+ /**
554+ * Tests that the free rate is returned when sending a quotes request
555+ *
556+ * @param array $addRequestData
557+ * @param bool $freeShippingExpects
558+ * @magentoConfigFixture default_store carriers/dhl/active 1
559+ * @magentoConfigFixture default_store carriers/dhl/id some ID
560+ * @magentoConfigFixture default_store carriers/dhl/shipment_days Mon,Tue,Wed,Thu,Fri,Sat
561+ * @magentoConfigFixture default_store carriers/dhl/intl_shipment_days Mon,Tue,Wed,Thu,Fri,Sat
562+ * @magentoConfigFixture default_store carriers/dhl/allowed_methods IE
563+ * @magentoConfigFixture default_store carriers/dhl/international_service IE
564+ * @magentoConfigFixture default_store carriers/dhl/gateway_url https://xmlpi-ea.dhl.com/XMLShippingServlet
565+ * @magentoConfigFixture default_store carriers/dhl/id some ID
566+ * @magentoConfigFixture default_store carriers/dhl/password some password
567+ * @magentoConfigFixture default_store carriers/dhl/content_type N
568+ * @magentoConfigFixture default_store carriers/dhl/nondoc_methods 1,3,4,8,P,Q,E,F,H,J,M,V,Y
569+ * @magentoConfigFixture default_store carriers/dhl/showmethod' => 1,
570+ * @magentoConfigFixture default_store carriers/dhl/title DHL Title
571+ * @magentoConfigFixture default_store carriers/dhl/specificerrmsg dhl error message
572+ * @magentoConfigFixture default_store carriers/dhl/unit_of_measure K
573+ * @magentoConfigFixture default_store carriers/dhl/size 1
574+ * @magentoConfigFixture default_store carriers/dhl/height 1.6
575+ * @magentoConfigFixture default_store carriers/dhl/width 1.6
576+ * @magentoConfigFixture default_store carriers/dhl/depth 1.6
577+ * @magentoConfigFixture default_store carriers/dhl/debug 1
578+ * @magentoConfigFixture default_store carriers/dhl/free_method_nondoc P
579+ * @magentoConfigFixture default_store carriers/dhl/free_shipping_enable 1
580+ * @magentoConfigFixture default_store carriers/dhl/free_shipping_subtotal 25
581+ * @magentoConfigFixture default_store shipping/origin/country_id GB
582+ * @magentoAppIsolation enabled
583+ * @dataProvider collectRatesWithFreeShippingDataProvider
584+ */
585+ public function testCollectRatesWithFreeShipping (array $ addRequestData , bool $ freeShippingExpects ): void
586+ {
587+ $ this ->setNextResponse (__DIR__ . '/../_files/dhl_quote_response.xml ' );
588+ $ request = $ this ->createRequest ($ addRequestData );
589+
590+ $ actualRates = $ this ->dhlCarrier ->collectRates ($ request )->getAllRates ();
591+ $ freeRateExists = false ;
592+ foreach ($ actualRates as $ actualRate ) {
593+ $ actualRate = $ actualRate ->getData ();
594+ if ($ actualRate ['method ' ] === 'P ' && (float )$ actualRate ['price ' ] === 0.0 ) {
595+ $ freeRateExists = true ;
596+ break ;
597+ }
598+ }
599+
600+ self ::assertEquals ($ freeShippingExpects , $ freeRateExists );
601+ }
602+
603+ /**
604+ * @return array
605+ */
606+ public function collectRatesWithFreeShippingDataProvider (): array
607+ {
608+ return [
609+ [
610+ ['package_value ' => 25 , 'package_value_with_discount ' => 22 ],
611+ false
612+ ],
613+ [
614+ ['package_value ' => 25 , 'package_value_with_discount ' => 25 ],
615+ true
616+ ],
617+ [
618+ ['package_value ' => 28 , 'package_value_with_discount ' => 25 ],
619+ true
620+ ],
621+ ];
622+ }
623+
566624 /**
567625 * Returns request data.
568626 *
@@ -571,47 +629,80 @@ private function setDhlConfig(array $params)
571629 private function getRequestData (): array
572630 {
573631 return [
574- 'data ' => [
575- 'dest_country_id ' => 'DE ' ,
576- 'dest_region_id ' => '82 ' ,
577- 'dest_region_code ' => 'BER ' ,
578- 'dest_street ' => 'Turmstraße 17 ' ,
579- 'dest_city ' => 'Berlin ' ,
580- 'dest_postcode ' => '10559 ' ,
581- 'dest_postal ' => '10559 ' ,
582- 'package_value ' => '5 ' ,
583- 'package_value_with_discount ' => '5 ' ,
584- 'package_weight ' => '8.2657 ' ,
585- 'package_qty ' => '1 ' ,
586- 'package_physical_value ' => '5 ' ,
587- 'free_method_weight ' => '5 ' ,
588- 'store_id ' => '1 ' ,
589- 'website_id ' => '1 ' ,
590- 'free_shipping ' => '0 ' ,
591- 'limit_carrier ' => null ,
592- 'base_subtotal_incl_tax ' => '5 ' ,
593- 'orig_country_id ' => 'US ' ,
594- 'orig_region_id ' => '12 ' ,
595- 'orig_city ' => 'Fremont ' ,
596- 'orig_postcode ' => '94538 ' ,
597- 'dhl_id ' => 'MAGEN_8501 ' ,
598- 'dhl_password ' => 'QR2GO1U74X ' ,
599- 'dhl_account ' => '799909537 ' ,
600- 'dhl_shipping_intl_key ' => '54233F2B2C4E5C4B4C5E5A59565530554B405641475D5659 ' ,
601- 'girth ' => null ,
602- 'height ' => null ,
603- 'length ' => null ,
604- 'width ' => null ,
605- 'weight ' => 1 ,
606- 'dhl_shipment_type ' => 'P ' ,
607- 'dhl_duitable ' => 0 ,
608- 'dhl_duty_payment_type ' => 'R ' ,
609- 'dhl_content_desc ' => 'Big Box ' ,
610- 'limit_method ' => 'IE ' ,
611- 'ship_date ' => '2014-01-09 ' ,
612- 'action ' => 'RateEstimate ' ,
613- 'all_items ' => [],
614- ]
632+ 'dest_country_id ' => 'DE ' ,
633+ 'dest_region_id ' => '82 ' ,
634+ 'dest_region_code ' => 'BER ' ,
635+ 'dest_street ' => 'Turmstraße 17 ' ,
636+ 'dest_city ' => 'Berlin ' ,
637+ 'dest_postcode ' => '10559 ' ,
638+ 'dest_postal ' => '10559 ' ,
639+ 'package_value ' => '5 ' ,
640+ 'package_value_with_discount ' => '5 ' ,
641+ 'package_weight ' => '8.2657 ' ,
642+ 'package_qty ' => '1 ' ,
643+ 'package_physical_value ' => '5 ' ,
644+ 'free_method_weight ' => '5 ' ,
645+ 'store_id ' => '1 ' ,
646+ 'website_id ' => '1 ' ,
647+ 'free_shipping ' => '0 ' ,
648+ 'limit_carrier ' => null ,
649+ 'base_subtotal_incl_tax ' => '5 ' ,
650+ 'orig_country_id ' => 'US ' ,
651+ 'orig_region_id ' => '12 ' ,
652+ 'orig_city ' => 'Fremont ' ,
653+ 'orig_postcode ' => '94538 ' ,
654+ 'dhl_id ' => 'MAGEN_8501 ' ,
655+ 'dhl_password ' => 'QR2GO1U74X ' ,
656+ 'dhl_account ' => '799909537 ' ,
657+ 'dhl_shipping_intl_key ' => '54233F2B2C4E5C4B4C5E5A59565530554B405641475D5659 ' ,
658+ 'girth ' => null ,
659+ 'height ' => null ,
660+ 'length ' => null ,
661+ 'width ' => null ,
662+ 'weight ' => 1 ,
663+ 'dhl_shipment_type ' => 'P ' ,
664+ 'dhl_duitable ' => 0 ,
665+ 'dhl_duty_payment_type ' => 'R ' ,
666+ 'dhl_content_desc ' => 'Big Box ' ,
667+ 'limit_method ' => 'IE ' ,
668+ 'ship_date ' => '2014-01-09 ' ,
669+ 'action ' => 'RateEstimate ' ,
670+ 'all_items ' => [],
615671 ];
616672 }
673+
674+ /**
675+ * Set next response content from file
676+ *
677+ * @param string $file
678+ */
679+ private function setNextResponse (string $ file ): void
680+ {
681+ //phpcs:disable Magento2.Functions.DiscouragedFunction
682+ $ response = new Response (
683+ 200 ,
684+ [],
685+ file_get_contents ($ file )
686+ );
687+ //phpcs:enable Magento2.Functions.DiscouragedFunction
688+ $ this ->httpClient ->nextResponses (
689+ array_fill (0 , Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1 , $ response )
690+ );
691+ }
692+
693+ /**
694+ * Create Rate Request
695+ *
696+ * @param array $addRequestData
697+ * @return RateRequest
698+ */
699+ private function createRequest (array $ addRequestData = []): RateRequest
700+ {
701+ $ requestData = $ this ->getRequestData ();
702+ if (!empty ($ addRequestData )) {
703+ $ requestData = array_merge ($ requestData , $ addRequestData );
704+ }
705+
706+ return Bootstrap::getObjectManager ()->create (RateRequest::class, ['data ' => $ requestData ]);
707+ }
617708}
0 commit comments