Skip to content

Commit f902d82

Browse files
committed
Integration test to save address information on virtual quote without exceptions
1 parent 9afb7bd commit f902d82

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Checkout\Model;
8+
9+
use Magento\Checkout\Api\Data\ShippingInformationInterface;
10+
use Magento\Checkout\Api\PaymentInformationManagementInterface;
11+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Quote\Api\CartItemRepositoryInterface;
14+
use Magento\Quote\Api\CartManagementInterface;
15+
use Magento\Quote\Api\Data\AddressInterfaceFactory;
16+
use Magento\Quote\Api\Data\CartItemInterface;
17+
use Magento\Quote\Api\Data\PaymentInterface;
18+
use Magento\Quote\Api\ShipmentEstimationInterface;
19+
use Magento\Sales\Api\InvoiceOrderInterface;
20+
21+
class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
22+
{
23+
/** @var CartManagementInterface */
24+
private $cartManagement;
25+
26+
/** @var CartItemRepositoryInterface */
27+
private $cartItemRepository;
28+
29+
/** @var CartItemInterface */
30+
private $cartItem;
31+
32+
/** @var ShippingInformationManagementInterface */
33+
private $shippingInformationManagement;
34+
35+
/** @var ShippingInformationInterface */
36+
private $shippingInformation;
37+
38+
/** @var CustomerRepositoryInterface */
39+
private $customerRepository;
40+
41+
/** @var AddressInterfaceFactory */
42+
private $apiAddressFactory;
43+
44+
/** @var ShipmentEstimationInterface */
45+
private $shipmentEstimation;
46+
47+
/** @var PaymentInformationManagementInterface */
48+
private $paymentInformationManagement;
49+
50+
/** @var PaymentInterface */
51+
private $payment;
52+
53+
/** @var InvoiceOrderInterface */
54+
private $invoiceOrder;
55+
56+
public function setUp()
57+
{
58+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
59+
60+
$this->cartManagement = $objectManager->create(CartManagementInterface::class);
61+
$this->cartItemRepository = $objectManager->create(CartItemRepositoryInterface::class);
62+
$this->cartItem = $objectManager->create(CartItemInterface::class);
63+
$this->shippingInformationManagement = $objectManager->create(ShippingInformationManagementInterface::class);
64+
$this->shippingInformation = $objectManager->create(ShippingInformationInterface::class);
65+
$this->customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
66+
$this->apiAddressFactory = $objectManager->create(AddressInterfaceFactory::class);
67+
$this->shipmentEstimation = $objectManager->create(ShipmentEstimationInterface::class);
68+
$this->paymentInformationManagement = $objectManager->create(PaymentInformationManagementInterface::class);
69+
$this->payment = $objectManager->create(PaymentInterface::class);
70+
$this->invoiceOrder = $objectManager->create(InvoiceOrderInterface::class);
71+
}
72+
73+
/**
74+
* @magentoDataFixture Magento/Customer/_files/customer.php
75+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
76+
* @magentoDataFixture Magento/Catalog/_files/product_virtual_in_stock.php
77+
*/
78+
public function testQuoteApiWithOnlyVirtualProducts()
79+
{
80+
$customer = $this->customerRepository->getById(1);
81+
82+
// Create empty quote
83+
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customer->getId());
84+
85+
$cartItem = $this->cartItem
86+
->setSku('virtual-product')
87+
->setQty(1)
88+
->setQuoteId($quoteId);
89+
90+
// Add item to cart
91+
$this->cartItemRepository->save($cartItem);
92+
93+
$billingAddress = $shippingAddress = null;
94+
foreach ($customer->getAddresses() as $address) {
95+
$billingAddress = $address;
96+
$shippingAddress = $address;
97+
break;
98+
}
99+
100+
/** @var \Magento\Quote\Model\Quote\Address $apiBillingAddress */
101+
$apiBillingAddress = $this->apiAddressFactory->create();
102+
$apiBillingAddress->setRegion($billingAddress->getRegion())
103+
->setRegionId($billingAddress->getRegionId())
104+
->setCountryId($billingAddress->getCountryId())
105+
->setStreet($billingAddress->getStreet())
106+
->setPostcode($billingAddress->getPostcode())
107+
->setCity($billingAddress->getCity())
108+
->setFirstname($billingAddress->getFirstname())
109+
->setLastname($billingAddress->getLastname())
110+
->setEmail($customer->getEmail())
111+
->setTelephone($billingAddress->getTelephone());
112+
113+
/** @var \Magento\Quote\Model\Quote\Address $apiShippingAddress */
114+
$apiShippingAddress = $this->apiAddressFactory->create();
115+
$apiShippingAddress->setRegion($shippingAddress->getRegion())
116+
->setRegionId($shippingAddress->getRegionId())
117+
->setCountryId($shippingAddress->getCountryId())
118+
->setStreet($shippingAddress->getStreet())
119+
->setPostcode($shippingAddress->getPostcode())
120+
->setCity($shippingAddress->getCity())
121+
->setFirstname($shippingAddress->getFirstname())
122+
->setLastname($shippingAddress->getLastname())
123+
->setEmail($customer->getEmail())
124+
->setTelephone($shippingAddress->getTelephone());
125+
126+
// Estimate shipping
127+
$this->shipmentEstimation->estimateByExtendedAddress($quoteId, $apiShippingAddress);
128+
129+
$addressInformation = $this->shippingInformation
130+
->setBillingAddress($apiBillingAddress)
131+
->setShippingAddress($apiShippingAddress)
132+
->setShippingCarrierCode('flatrate')
133+
->setShippingMethodCode('flatrate');
134+
135+
// Set address information on quote
136+
$this->shippingInformationManagement->saveAddressInformation($quoteId, $addressInformation);
137+
}
138+
}

0 commit comments

Comments
 (0)