Skip to content

Commit d3afc47

Browse files
MAGETWO-82527: MAGETWO-9007: Get MAGETWO-52856 into Magento 2.1.x #11640
2 parents 9adbca7 + 0fd76e1 commit d3afc47

File tree

6 files changed

+286
-454
lines changed

6 files changed

+286
-454
lines changed

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

Lines changed: 28 additions & 7 deletions
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

@@ -44,26 +45,37 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
4445
*/
4546
private $logger;
4647

48+
/**
49+
* Provide active quote.
50+
*
51+
* @var \Magento\Quote\Api\CartRepositoryInterface
52+
*/
53+
private $cartRepository;
54+
4755
/**
4856
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
49-
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
50-
* @param \Magento\Quote\Api\CartManagementInterface $cartManagement
51-
* @param PaymentDetailsFactory $paymentDetailsFactory
52-
* @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
57+
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
58+
* @param \Magento\Quote\Api\CartManagementInterface $cartManagement
59+
* @param PaymentDetailsFactory $paymentDetailsFactory
60+
* @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
61+
* @param \Magento\Quote\Api\CartRepositoryInterface|null $cartRepository
5362
* @codeCoverageIgnore
5463
*/
5564
public function __construct(
5665
\Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement,
5766
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
5867
\Magento\Quote\Api\CartManagementInterface $cartManagement,
5968
\Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory,
60-
\Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
69+
\Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository,
70+
\Magento\Quote\Api\CartRepositoryInterface $cartRepository = null
6171
) {
6272
$this->billingAddressManagement = $billingAddressManagement;
6373
$this->paymentMethodManagement = $paymentMethodManagement;
6474
$this->cartManagement = $cartManagement;
6575
$this->paymentDetailsFactory = $paymentDetailsFactory;
6676
$this->cartTotalsRepository = $cartTotalsRepository;
77+
$this->cartRepository = $cartRepository ? : \Magento\Framework\App\ObjectManager::getInstance()
78+
->get(\Magento\Quote\Api\CartRepositoryInterface::class);
6779
}
6880

6981
/**
@@ -102,7 +114,17 @@ public function savePaymentInformation(
102114
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
103115
) {
104116
if ($billingAddress) {
105-
$this->billingAddressManagement->assign($cartId, $billingAddress);
117+
/** @var \Magento\Quote\Model\Quote $quote */
118+
$quote = $this->cartRepository->getActive($cartId);
119+
$quote->removeAddress($quote->getBillingAddress()->getId());
120+
$quote->setBillingAddress($billingAddress);
121+
$quote->setDataChanges(true);
122+
$shippingAddress = $quote->getShippingAddress();
123+
if ($shippingAddress && $shippingAddress->getShippingMethod()) {
124+
$shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
125+
$shippingCarrier = array_shift($shippingDataArray);
126+
$shippingAddress->setLimitCarrier($shippingCarrier);
127+
}
106128
}
107129
$this->paymentMethodManagement->set($cartId, $paymentMethod);
108130

@@ -133,7 +155,6 @@ private function getLogger()
133155
if (!$this->logger) {
134156
$this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
135157
}
136-
137158
return $this->logger;
138159
}
139160
}

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: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace Magento\Checkout\Test\Unit\Model;
88

9+
/**
10+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11+
*/
912
class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
1013
{
1114
/**
@@ -33,6 +36,11 @@ class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
3336
*/
3437
private $loggerMock;
3538

39+
/**
40+
* @var \PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $cartRepositoryMock;
43+
3644
protected function setUp()
3745
{
3846
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -45,13 +53,14 @@ protected function setUp()
4553
$this->cartManagementMock = $this->getMock(\Magento\Quote\Api\CartManagementInterface::class);
4654

4755
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
48-
56+
$this->cartRepositoryMock = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)->getMock();
4957
$this->model = $objectManager->getObject(
5058
\Magento\Checkout\Model\PaymentInformationManagement::class,
5159
[
5260
'billingAddressManagement' => $this->billingAddressManagementMock,
5361
'paymentMethodManagement' => $this->paymentMethodManagementMock,
54-
'cartManagement' => $this->cartManagementMock
62+
'cartManagement' => $this->cartManagementMock,
63+
'cartRepository' => $this->cartRepositoryMock
5564
]
5665
);
5766
$objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
@@ -64,9 +73,7 @@ public function testSavePaymentInformationAndPlaceOrder()
6473
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
6574
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
6675

67-
$this->billingAddressManagementMock->expects($this->once())
68-
->method('assign')
69-
->with($cartId, $billingAddressMock);
76+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
7077
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
7178
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
7279

@@ -86,9 +93,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
8693
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
8794
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
8895

89-
$this->billingAddressManagementMock->expects($this->once())
90-
->method('assign')
91-
->with($cartId, $billingAddressMock);
96+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
9297
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
9398
$exception = new \Exception(__('DB exception'));
9499
$this->loggerMock->expects($this->once())->method('critical');
@@ -103,7 +108,6 @@ public function testSavePaymentInformationAndPlaceOrderIfBillingAddressNotExist(
103108
$orderId = 200;
104109
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
105110

106-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
107111
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
108112
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
109113

@@ -119,9 +123,7 @@ public function testSavePaymentInformation()
119123
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
120124
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
121125

122-
$this->billingAddressManagementMock->expects($this->once())
123-
->method('assign')
124-
->with($cartId, $billingAddressMock);
126+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
125127
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
126128

127129
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock, $billingAddressMock));
@@ -132,7 +134,6 @@ public function testSavePaymentInformationWithoutBillingAddress()
132134
$cartId = 100;
133135
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
134136

135-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
136137
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
137138

138139
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock));
@@ -148,9 +149,8 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
148149
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
149150
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
150151

151-
$this->billingAddressManagementMock->expects($this->once())
152-
->method('assign')
153-
->with($cartId, $billingAddressMock);
152+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
153+
154154
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
155155
$phrase = new \Magento\Framework\Phrase(__('DB exception'));
156156
$exception = new \Magento\Framework\Exception\LocalizedException($phrase);
@@ -159,4 +159,31 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
159159

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

0 commit comments

Comments
 (0)