Skip to content

Commit 72e0834

Browse files
ACQE-8552: [AC-5466] Use DHL (US) Online Shipping Carrier on Checkout as a Registered Customer
- Added integration code coverage for DHL-US shipping carrier.
1 parent e885088 commit 72e0834

File tree

1 file changed

+219
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)