Skip to content

Commit 3495e60

Browse files
committed
MAGETWO-52856: [WebAPI] Improve WebAPI performance of Checkout Payment Info/Place order call
1 parent 1f4f104 commit 3495e60

File tree

6 files changed

+110
-22
lines changed

6 files changed

+110
-22
lines changed

app/code/Magento/Checkout/Model/PaymentInformationManagement.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
1414
{
1515
/**
1616
* @var \Magento\Quote\Api\BillingAddressManagementInterface
17+
* @deprecated This call was substituted to eliminate extra quote::save call
1718
*/
1819
protected $billingAddressManagement;
1920

@@ -42,6 +43,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
4243
*/
4344
private $logger;
4445

46+
/**
47+
* @var \Magento\Quote\Api\CartRepositoryInterface
48+
*/
49+
private $cartRepository;
50+
4551
/**
4652
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
4753
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -99,7 +105,19 @@ public function savePaymentInformation(
99105
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
100106
) {
101107
if ($billingAddress) {
102-
$this->billingAddressManagement->assign($cartId, $billingAddress);
108+
/** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
109+
$quoteRepository = $this->getCartRepository();
110+
/** @var \Magento\Quote\Model\Quote $quote */
111+
$quote = $quoteRepository->getActive($cartId);
112+
$quote->removeAddress($quote->getBillingAddress()->getId());
113+
$quote->setBillingAddress($billingAddress);
114+
$quote->setDataChanges(true);
115+
$shippingAddress = $quote->getShippingAddress();
116+
if ($shippingAddress && $shippingAddress->getShippingMethod()) {
117+
$shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
118+
$shippingCarrier = array_shift($shippingDataArray);
119+
$shippingAddress->setLimitCarrier($shippingCarrier);
120+
}
103121
}
104122
$this->paymentMethodManagement->set($cartId, $paymentMethod);
105123
return true;
@@ -130,4 +148,19 @@ private function getLogger()
130148
}
131149
return $this->logger;
132150
}
151+
152+
/**
153+
* Get Cart repository
154+
*
155+
* @return \Magento\Quote\Api\CartRepositoryInterface
156+
* @deprecated
157+
*/
158+
private function getCartRepository()
159+
{
160+
if (!$this->cartRepository) {
161+
$this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance()
162+
->get(\Magento\Quote\Api\CartRepositoryInterface::class);
163+
}
164+
return $this->cartRepository;
165+
}
133166
}

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public function saveAddressInformation(
144144

145145
/** @var \Magento\Quote\Model\Quote $quote */
146146
$quote = $this->quoteRepository->getActive($cartId);
147+
$address->setLimitCarrier($carrierCode);
147148
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
148149
$this->validateQuote($quote);
149150
$quote->setIsMultiShipping(false);

app/code/Magento/Checkout/Test/Unit/Model/PaymentInformationManagementTest.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66
namespace Magento\Checkout\Test\Unit\Model;
77

8-
use Magento\Framework\Exception\CouldNotSaveException;
9-
8+
/**
9+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10+
*/
1011
class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
1112
{
1213
/**
@@ -34,6 +35,11 @@ class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
3435
*/
3536
private $loggerMock;
3637

38+
/**
39+
* @var \PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $cartRepositoryMock;
42+
3743
protected function setUp()
3844
{
3945
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -46,7 +52,7 @@ protected function setUp()
4652
$this->cartManagementMock = $this->getMock(\Magento\Quote\Api\CartManagementInterface::class);
4753

4854
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
49-
55+
$this->cartRepositoryMock = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)->getMock();
5056
$this->model = $objectManager->getObject(
5157
\Magento\Checkout\Model\PaymentInformationManagement::class,
5258
[
@@ -56,6 +62,7 @@ protected function setUp()
5662
]
5763
);
5864
$objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
65+
$objectManager->setBackwardCompatibleProperty($this->model, 'cartRepository', $this->cartRepositoryMock);
5966
}
6067

6168
public function testSavePaymentInformationAndPlaceOrder()
@@ -65,9 +72,7 @@ public function testSavePaymentInformationAndPlaceOrder()
6572
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
6673
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
6774

68-
$this->billingAddressManagementMock->expects($this->once())
69-
->method('assign')
70-
->with($cartId, $billingAddressMock);
75+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
7176
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
7277
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
7378

@@ -87,9 +92,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
8792
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
8893
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
8994

90-
$this->billingAddressManagementMock->expects($this->once())
91-
->method('assign')
92-
->with($cartId, $billingAddressMock);
95+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
9396
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
9497
$exception = new \Exception(__('DB exception'));
9598
$this->loggerMock->expects($this->once())->method('critical');
@@ -104,7 +107,6 @@ public function testSavePaymentInformationAndPlaceOrderIfBillingAddressNotExist(
104107
$orderId = 200;
105108
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
106109

107-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
108110
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
109111
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
110112

@@ -120,9 +122,7 @@ public function testSavePaymentInformation()
120122
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
121123
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
122124

123-
$this->billingAddressManagementMock->expects($this->once())
124-
->method('assign')
125-
->with($cartId, $billingAddressMock);
125+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
126126
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
127127

128128
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock, $billingAddressMock));
@@ -133,7 +133,6 @@ public function testSavePaymentInformationWithoutBillingAddress()
133133
$cartId = 100;
134134
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
135135

136-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
137136
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
138137

139138
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock));
@@ -149,9 +148,8 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
149148
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
150149
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
151150

152-
$this->billingAddressManagementMock->expects($this->once())
153-
->method('assign')
154-
->with($cartId, $billingAddressMock);
151+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
152+
155153
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
156154
$phrase = new \Magento\Framework\Phrase(__('DB exception'));
157155
$exception = new \Magento\Framework\Exception\LocalizedException($phrase);
@@ -160,4 +158,31 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
160158

161159
$this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);
162160
}
161+
162+
/**
163+
* @param int $cartId
164+
* @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
165+
*/
166+
private function getMockForAssignBillingAddress($cartId, $billingAddressMock)
167+
{
168+
$billingAddressId = 1;
169+
$quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false);
170+
$quoteBillingAddress = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
171+
$quoteShippingAddress = $this->getMock(
172+
\Magento\Quote\Model\Quote\Address::class,
173+
['setLimitCarrier', 'getShippingMethod'],
174+
[],
175+
'',
176+
false
177+
);
178+
$this->cartRepositoryMock->expects($this->any())->method('getActive')->with($cartId)->willReturn($quoteMock);
179+
$quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($quoteBillingAddress);
180+
$quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($quoteShippingAddress);
181+
$quoteBillingAddress->expects($this->once())->method('getId')->willReturn($billingAddressId);
182+
$quoteMock->expects($this->once())->method('removeAddress')->with($billingAddressId);
183+
$quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddressMock);
184+
$quoteMock->expects($this->once())->method('setDataChanges')->willReturnSelf();
185+
$quoteShippingAddress->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate');
186+
$quoteShippingAddress->expects($this->once())->method('setLimitCarrier')->with('flatrate')->willReturnSelf();
187+
}
163188
}

app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ protected function setUp()
109109
'importCustomerAddressData',
110110
'save',
111111
'getShippingRateByCode',
112-
'getShippingMethod'
112+
'getShippingMethod',
113+
'setLimitCarrier'
113114
],
114115
[],
115116
'',
@@ -208,7 +209,7 @@ public function testSaveAddressInformationIfCartIsEmpty()
208209
private function setShippingAssignmentsMocks($shippingMethod)
209210
{
210211
$this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
211-
212+
$this->shippingAddressMock->expects($this->once())->method('setLimitCarrier');
212213
$this->cartExtensionMock = $this->getMock(
213214
\Magento\Quote\Api\Data\CartExtension::class,
214215
['getShippingAssignments', 'setShippingAssignments'],

app/code/Magento/Quote/Model/CustomerManagement.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public function populateCustomerInfo(QuoteEntity $quote)
6262
$quote->getPasswordHash()
6363
);
6464
$quote->setCustomer($customer);
65-
} else {
66-
$this->customerRepository->save($customer);
6765
}
6866
if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
6967
$quote->getBillingAddress()->importCustomerAddressData(

app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,34 @@ public function testPopulateCustomerInfo()
158158
->willReturn($this->customerMock);
159159
$this->customerManagement->populateCustomerInfo($this->quoteMock);
160160
}
161+
162+
public function testPopulateCustomerInfoForExistingCustomer()
163+
{
164+
$this->quoteMock->expects($this->once())
165+
->method('getCustomer')
166+
->willReturn($this->customerMock);
167+
$this->customerMock->expects($this->atLeastOnce())
168+
->method('getId')
169+
->willReturn(1);
170+
$this->customerMock->expects($this->atLeastOnce())
171+
->method('getDefaultBilling')
172+
->willReturn(100500);
173+
$this->quoteMock->expects($this->atLeastOnce())
174+
->method('getBillingAddress')
175+
->willReturn($this->quoteAddressMock);
176+
$this->quoteMock->expects($this->atLeastOnce())
177+
->method('getShippingAddress')
178+
->willReturn($this->quoteAddressMock);
179+
$this->quoteAddressMock->expects($this->atLeastOnce())
180+
->method('getId')
181+
->willReturn(null);
182+
$this->customerAddressRepositoryMock->expects($this->atLeastOnce())
183+
->method('getById')
184+
->with(100500)
185+
->willReturn($this->customerAddressMock);
186+
$this->quoteAddressMock->expects($this->atLeastOnce())
187+
->method('importCustomerAddressData')
188+
->willReturnSelf();
189+
$this->customerManagement->populateCustomerInfo($this->quoteMock);
190+
}
161191
}

0 commit comments

Comments
 (0)