|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Dhl; |
| 10 | + |
| 11 | +use Magento\Bundle\Model\Product\Price; |
| 12 | +use Magento\Bundle\Test\Fixture\AddProductToCart as AddBundleProductToCart; |
| 13 | +use Magento\Bundle\Test\Fixture\Link as BundleSelectionFixture; |
| 14 | +use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture; |
| 15 | +use Magento\Bundle\Test\Fixture\Product as BundleProductFixture; |
| 16 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 17 | +use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture; |
| 18 | +use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture; |
| 19 | +use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture; |
| 20 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 21 | +use Magento\Dhl\Model\Carrier; |
| 22 | +use Magento\Framework\Exception\LocalizedException; |
| 23 | +use Magento\Framework\HTTP\AsyncClient\Response; |
| 24 | +use Magento\Framework\HTTP\AsyncClientInterface; |
| 25 | +use Magento\Framework\ObjectManagerInterface; |
| 26 | +use Magento\Quote\Api\CartManagementInterface; |
| 27 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 28 | +use Magento\Quote\Api\Data\AddressInterface; |
| 29 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 30 | +use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture; |
| 31 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 32 | +use Magento\TestFramework\Fixture\Config; |
| 33 | +use Magento\TestFramework\Fixture\DataFixture; |
| 34 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 35 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 36 | +use Magento\TestFramework\Helper\Bootstrap; |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration test to verify order placement using dhl international shipping carrier |
| 41 | + * |
| 42 | + * @magentoDbIsolation disabled |
| 43 | + * @magentoAppIsolation enabled |
| 44 | + * @magentoAppArea frontend |
| 45 | + */ |
| 46 | +class PlaceOrderWithDhlUsCarrierTest extends TestCase |
| 47 | +{ |
| 48 | + /** |
| 49 | + * @var DataFixtureStorage |
| 50 | + */ |
| 51 | + private DataFixtureStorage $fixtures; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var CartRepositoryInterface |
| 55 | + */ |
| 56 | + private CartRepositoryInterface $quoteRepository; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var CartManagementInterface |
| 60 | + */ |
| 61 | + private CartManagementInterface $cartManagement; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var OrderRepositoryInterface |
| 65 | + */ |
| 66 | + private OrderRepositoryInterface $orderRepository; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var ObjectManagerInterface |
| 70 | + */ |
| 71 | + private ObjectManagerInterface $objectManager; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var AsyncClientInterface |
| 75 | + */ |
| 76 | + private AsyncClientInterface $httpClient; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var string|null |
| 80 | + */ |
| 81 | + private ?string $selectedShippingMethod = null; |
| 82 | + |
| 83 | + /** |
| 84 | + * @return void |
| 85 | + * @throws LocalizedException |
| 86 | + */ |
| 87 | + protected function setUp(): void |
| 88 | + { |
| 89 | + parent::setUp(); |
| 90 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 91 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 92 | + $this->quoteRepository = $this->objectManager->get(CartRepositoryInterface::class); |
| 93 | + $this->cartManagement = $this->objectManager->get(CartManagementInterface::class); |
| 94 | + $this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class); |
| 95 | + $this->httpClient = $this->objectManager->get(AsyncClientInterface::class); |
| 96 | + } |
| 97 | + |
| 98 | + #[ |
| 99 | + Config('payment/checkmo/active', '1', 'store', 'default'), |
| 100 | + // Shipping origin: US |
| 101 | + Config('shipping/origin/country_id', 'US', 'store', 'default'), |
| 102 | + Config('shipping/origin/region_id', '12', 'store', 'default'), |
| 103 | + Config('shipping/origin/postcode', '90034', 'store', 'default'), |
| 104 | + Config('shipping/origin/city', 'los angeles', 'store', 'default'), |
| 105 | + Config('shipping/origin/street_line1', '123 Warehouse Ave', 'store', 'default'), |
| 106 | + // DHL carrier configuration (US) with fake credentials and REST gateway |
| 107 | + Config('carriers/dhl/active', '1', 'store', 'default'), |
| 108 | + Config('carriers/dhl/type', 'DHL_REST', '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'), |
| 115 | + Config('carriers/dhl/sandbox_mode', '1', 'store', 'default'), |
| 116 | + // Store information matching shipping origin |
| 117 | + Config('general/store_information/name', 'store', 'store', 'default'), |
| 118 | + Config('general/store_information/phone', '1234567890', 'store', 'default'), |
| 119 | + Config('general/store_information/country_id', 'US', 'store', 'default'), |
| 120 | + Config('general/store_information/region_id', '12', 'store', 'default'), |
| 121 | + Config('general/store_information/postcode', '90034', 'store', 'default'), |
| 122 | + Config('general/store_information/city', 'los angeles', 'store', 'default'), |
| 123 | + Config('general/store_information/street_line1', '123 Warehouse Ave', 'store', 'default'), |
| 124 | + DataFixture(CustomerFixture::class, as: 'customer'), |
| 125 | + DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'cart'), |
| 126 | + DataFixture(ProductFixture::class, as: 'product'), |
| 127 | + DataFixture(ProductFixture::class, ['price' => 10], as: 'p1'), |
| 128 | + DataFixture(ProductFixture::class, ['price' => 20], as: 'p2'), |
| 129 | + DataFixture(AttributeFixture::class, as: 'attr'), |
| 130 | + DataFixture( |
| 131 | + ConfigurableProductFixture::class, |
| 132 | + ['_options' => ['$attr$'], '_links' => ['$p1$', '$p2$']], |
| 133 | + 'cp1' |
| 134 | + ), |
| 135 | + DataFixture(ProductFixture::class, ['price' => 20], 'product1'), |
| 136 | + DataFixture(ProductFixture::class, ['price' => 10], 'product2'), |
| 137 | + DataFixture(BundleSelectionFixture::class, ['sku' => '$product1.sku$'], 'selection1'), |
| 138 | + DataFixture(BundleSelectionFixture::class, ['sku' => '$product2.sku$'], 'selection2'), |
| 139 | + DataFixture(BundleOptionFixture::class, ['product_links' => ['$selection1$']], 'opt1'), |
| 140 | + DataFixture(BundleOptionFixture::class, ['product_links' => ['$selection2$']], 'opt2'), |
| 141 | + DataFixture( |
| 142 | + BundleProductFixture::class, |
| 143 | + [ |
| 144 | + 'sku' => 'bundle-product-fixed-price', |
| 145 | + 'price_type' => Price::PRICE_TYPE_DYNAMIC, |
| 146 | + '_options' => ['$opt1$', '$opt2$'] |
| 147 | + ], |
| 148 | + 'bundle_product_1' |
| 149 | + ), |
| 150 | + DataFixture( |
| 151 | + AddProductToCartFixture::class, |
| 152 | + ['cart_id' => '$cart.id$', 'product_id' => '$product.id$', 'qty' => 1] |
| 153 | + ), |
| 154 | + DataFixture( |
| 155 | + AddConfigurableProductToCartFixture::class, |
| 156 | + [ |
| 157 | + 'cart_id' => '$cart.id$', |
| 158 | + 'product_id' => '$cp1.id$', |
| 159 | + 'child_product_id' => '$p2.id$', |
| 160 | + 'qty' => 1 |
| 161 | + ] |
| 162 | + ), |
| 163 | + DataFixture( |
| 164 | + AddBundleProductToCart::class, |
| 165 | + [ |
| 166 | + 'cart_id' => '$cart.id$', |
| 167 | + 'product_id' => '$bundle_product_1.id$', |
| 168 | + 'selections' => [['$product1.id$'], ['$product2.id$']] |
| 169 | + ] |
| 170 | + ), |
| 171 | + ] |
| 172 | + /** |
| 173 | + * Verifies successful order placement using DHL-US shipping carrier |
| 174 | + * |
| 175 | + * @return void |
| 176 | + */ |
| 177 | + public function testPlaceOrderWithDhlUsCarrier(): void |
| 178 | + { |
| 179 | + $cartId = (int)$this->fixtures->get('cart')->getId(); |
| 180 | + $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)); |
| 184 | + $order = $this->orderRepository->get($this->selectDhlAndCheckmoAndPlaceOrder($cartId)); |
| 185 | + $this->assertNotEmpty($order->getIncrementId()); |
| 186 | + $this->assertSame($this->selectedShippingMethod, $order->getShippingMethod()); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Set billing and shipping address for card |
| 191 | + * |
| 192 | + * @param int $cartId |
| 193 | + * @return void |
| 194 | + */ |
| 195 | + private function setShippingAndBillingAddressForQuote(int $cartId): void |
| 196 | + { |
| 197 | + $quote = $this->quoteRepository->get($cartId); |
| 198 | + /** @var AddressInterface $address */ |
| 199 | + $address = $this->objectManager->create(AddressInterface::class); |
| 200 | + $address->setFirstname('Joe') |
| 201 | + ->setLastname('Doe') |
| 202 | + ->setCountryId('CA') |
| 203 | + ->setRegionId(76) |
| 204 | + ->setRegion('Quebec') |
| 205 | + ->setCity('Sherbrooke') |
| 206 | + ->setStreet('3197 rue Parc') |
| 207 | + ->setPostcode('J1L 1C9') |
| 208 | + ->setTelephone('9876543210'); |
| 209 | + $quote->setBillingAddress($address); |
| 210 | + $quote->setShippingAddress($address); |
| 211 | + $this->quoteRepository->save($quote); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Set dhl any international shipping method for quote and place order |
| 216 | + * |
| 217 | + * @param int $cartId |
| 218 | + * @return int |
| 219 | + */ |
| 220 | + private function selectDhlAndCheckmoAndPlaceOrder(int $cartId): int |
| 221 | + { |
| 222 | + $quote = $this->quoteRepository->get($cartId); |
| 223 | + $shippingAddress = $quote->getShippingAddress(); |
| 224 | + $shippingAddress->setCollectShippingRates(true); |
| 225 | + $quote->collectTotals(); |
| 226 | + foreach ($shippingAddress->getAllShippingRates() as $rate) { |
| 227 | + if ($rate->getCarrier() === 'dhl') { |
| 228 | + $methodCode = (string)$rate->getMethod(); |
| 229 | + $this->selectedShippingMethod = 'dhl_' . $methodCode; |
| 230 | + break; |
| 231 | + } |
| 232 | + } |
| 233 | + if ($this->selectedShippingMethod === null) { |
| 234 | + $this->fail('No DHL shipping rates available to select.'); |
| 235 | + } |
| 236 | + $shippingAddress->setShippingMethod($this->selectedShippingMethod)->setCollectShippingRates(false); |
| 237 | + $quote->getPayment()->setMethod('checkmo'); |
| 238 | + $this->quoteRepository->save($quote); |
| 239 | + return (int)$this->cartManagement->placeOrder($quote->getId()); |
| 240 | + } |
| 241 | +} |
0 commit comments