1818use Magento \ConfigurableProduct \Test \Fixture \Attribute as AttributeFixture ;
1919use Magento \ConfigurableProduct \Test \Fixture \Product as ConfigurableProductFixture ;
2020use Magento \Customer \Test \Fixture \Customer as CustomerFixture ;
21+ use Magento \Dhl \Model \Carrier ;
2122use Magento \Framework \Exception \LocalizedException ;
23+ use Magento \Framework \HTTP \AsyncClient \Response ;
24+ use Magento \Framework \HTTP \AsyncClientInterface ;
2225use Magento \Framework \ObjectManagerInterface ;
2326use Magento \Quote \Api \CartManagementInterface ;
2427use Magento \Quote \Api \CartRepositoryInterface ;
@@ -67,6 +70,16 @@ class PlaceOrderWithDhlUsCarrierTest extends TestCase
6770 */
6871 private ObjectManagerInterface $ objectManager ;
6972
73+ /**
74+ * @var AsyncClientInterface
75+ */
76+ private AsyncClientInterface $ httpClient ;
77+
78+ /**
79+ * @var string|null
80+ */
81+ private ?string $ selectedShippingMethod = null ;
82+
7083 /**
7184 * @return void
7285 * @throws LocalizedException
@@ -79,6 +92,7 @@ protected function setUp(): void
7992 $ this ->quoteRepository = $ this ->objectManager ->get (CartRepositoryInterface::class);
8093 $ this ->cartManagement = $ this ->objectManager ->get (CartManagementInterface::class);
8194 $ this ->orderRepository = $ this ->objectManager ->get (OrderRepositoryInterface::class);
95+ $ this ->httpClient = $ this ->objectManager ->get (AsyncClientInterface::class);
8296 }
8397
8498 #[
@@ -89,14 +103,15 @@ protected function setUp(): void
89103 Config('shipping/origin/postcode ' , '90034 ' , 'store ' , 'default ' ),
90104 Config('shipping/origin/city ' , 'los angeles ' , 'store ' , 'default ' ),
91105 Config('shipping/origin/street_line1 ' , '123 Warehouse Ave ' , 'store ' , 'default ' ),
92- // DHL carrier configuration (US)
106+ // DHL carrier configuration (US) with fake credentials and REST gateway
93107 Config('carriers/dhl/active ' , '1 ' , 'store ' , 'default ' ),
94108 Config('carriers/dhl/type ' , 'DHL_REST ' , 'store ' , 'default ' ),
95- Config('carriers/dhl/id ' , 'EvgeniyUSA ' , 'store ' , 'default ' ),
96- Config('carriers/dhl/api_key ' , 'apO9vB7nJ4mE3j ' , 'store ' , 'default ' ),
97- Config('carriers/dhl/password ' , 'okG43dHy7 ' , 'store ' , 'default ' ),
98- Config('carriers/dhl/api_secret ' , 'W#6aP!4hB@6iE@7i ' , 'store ' , 'default ' ),
99- Config('carriers/dhl/account ' , '965269748 ' , 'store ' , 'default ' ),
109+ Config('carriers/dhl/gateway_rest_url ' , 'https://express.api.dhl.com/mydhlapi ' , 'store ' , 'default ' ),
110+ Config('carriers/dhl/id ' , 'some ID ' , 'store ' , 'default ' ),
111+ Config('carriers/dhl/password ' , 'some password ' , 'store ' , 'default ' ),
112+ Config('carriers/dhl/api_key ' , 'some KEY ' , 'store ' , 'default ' ),
113+ Config('carriers/dhl/api_secret ' , 'some secret ' , 'store ' , 'default ' ),
114+ Config('carriers/dhl/account ' , '998765432 ' , 'store ' , 'default ' ),
100115 Config('carriers/dhl/sandbox_mode ' , '1 ' , 'store ' , 'default ' ),
101116 // Store information matching shipping origin
102117 Config('general/store_information/name ' , 'store ' , 'store ' , 'default ' ),
@@ -163,9 +178,12 @@ public function testPlaceOrderWithDhlUsCarrier(): void
163178 {
164179 $ cartId = (int )$ this ->fixtures ->get ('cart ' )->getId ();
165180 $ this ->setShippingAndBillingAddressForQuote ($ cartId );
181+ $ content = file_get_contents (__DIR__ . '/_files/dhl_quote_response.json ' );
182+ $ response = new Response (200 , [], $ content );
183+ $ this ->httpClient ->nextResponses (array_fill (0 , Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1 , $ response ));
166184 $ order = $ this ->orderRepository ->get ($ this ->selectDhlAndCheckmoAndPlaceOrder ($ cartId ));
167185 $ this ->assertNotEmpty ($ order ->getIncrementId ());
168- $ this ->assertStringStartsWith ( ' dhl_ ' , $ order ->getShippingMethod ());
186+ $ this ->assertSame ( $ this -> selectedShippingMethod , $ order ->getShippingMethod ());
169187 }
170188
171189 /**
@@ -197,25 +215,26 @@ private function setShippingAndBillingAddressForQuote(int $cartId): void
197215 * Set dhl any international shipping method for quote and place order
198216 *
199217 * @param int $cartId
200- * @return int $order
218+ * @return int
201219 */
202220 private function selectDhlAndCheckmoAndPlaceOrder (int $ cartId ): int
203221 {
204222 $ quote = $ this ->quoteRepository ->get ($ cartId );
205223 $ shippingAddress = $ quote ->getShippingAddress ();
206224 $ shippingAddress ->setCollectShippingRates (true );
207- $ shippingAddress ->collectShippingRates ();
208- $ dhlRate = null ;
225+ $ quote ->collectTotals ();
209226 foreach ($ shippingAddress ->getAllShippingRates () as $ rate ) {
210227 if ($ rate ->getCarrier () === 'dhl ' ) {
211- $ dhlRate = $ rate ;
228+ $ methodCode = (string )$ rate ->getMethod ();
229+ $ this ->selectedShippingMethod = 'dhl_ ' . $ methodCode ;
212230 break ;
213231 }
214232 }
215- $ this ->assertNotEmpty ($ dhlRate , 'No DHL rates available for the given address. ' );
216- $ shippingAddress ->setShippingMethod ($ dhlRate ->getCode ());
233+ if ($ this ->selectedShippingMethod === null ) {
234+ $ this ->fail ('No DHL shipping rates available to select. ' );
235+ }
236+ $ shippingAddress ->setShippingMethod ($ this ->selectedShippingMethod )->setCollectShippingRates (false );
217237 $ quote ->getPayment ()->setMethod ('checkmo ' );
218- $ quote ->collectTotals ();
219238 $ this ->quoteRepository ->save ($ quote );
220239 return (int )$ this ->cartManagement ->placeOrder ($ quote ->getId ());
221240 }
0 commit comments