Skip to content

Commit defc2c5

Browse files
committed
MAGETWO-91610: [2.3] Billing Address in Braintree PayPal response is absent
- Added check if a billing address is returned by Braintree
1 parent 6e05396 commit defc2c5

File tree

2 files changed

+107
-109
lines changed

2 files changed

+107
-109
lines changed

app/code/Magento/Braintree/Model/Paypal/Helper/QuoteUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private function updateBillingAddress(Quote $quote, array $details)
148148
{
149149
$billingAddress = $quote->getBillingAddress();
150150

151-
if ($this->config->isRequiredBillingAddress()) {
151+
if ($this->config->isRequiredBillingAddress() && !empty($details['billingAddress'])) {
152152
$this->updateAddressData($billingAddress, $details['billingAddress']);
153153
} else {
154154
$this->updateAddressData($billingAddress, $details['shippingAddress']);

app/code/Magento/Braintree/Test/Unit/Model/Paypal/Helper/QuoteUpdaterTest.php

Lines changed: 106 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,67 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Braintree\Test\Unit\Model\Paypal\Helper;
79

8-
use Magento\Quote\Model\Quote;
9-
use Magento\Quote\Model\Quote\Address;
10-
use Magento\Quote\Model\Quote\Payment;
11-
use Magento\Quote\Api\CartRepositoryInterface;
12-
use Magento\Braintree\Model\Ui\PayPal\ConfigProvider;
13-
use Magento\Braintree\Observer\DataAssignObserver;
1410
use Magento\Braintree\Gateway\Config\PayPal\Config;
1511
use Magento\Braintree\Model\Paypal\Helper\QuoteUpdater;
12+
use Magento\Braintree\Model\Ui\PayPal\ConfigProvider;
13+
use Magento\Braintree\Observer\DataAssignObserver;
14+
use Magento\Quote\Api\CartRepositoryInterface;
1615
use Magento\Quote\Api\Data\CartExtensionInterface;
16+
use Magento\Quote\Model\Quote;
17+
use Magento\Quote\Model\Quote\Address;
18+
use Magento\Quote\Model\Quote\Payment;
19+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1720

1821
/**
1922
* Class QuoteUpdaterTest
2023
*
21-
* @see \Magento\Braintree\Model\Paypal\Helper\QuoteUpdater
22-
*
2324
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2425
*/
2526
class QuoteUpdaterTest extends \PHPUnit\Framework\TestCase
2627
{
2728
const TEST_NONCE = '3ede7045-2aea-463e-9754-cd658ffeeb48';
2829

2930
/**
30-
* @var Config|\PHPUnit_Framework_MockObject_MockObject
31+
* @var Config|MockObject
3132
*/
32-
private $configMock;
33+
private $config;
3334

3435
/**
35-
* @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
36+
* @var CartRepositoryInterface|MockObject
3637
*/
37-
private $quoteRepositoryMock;
38+
private $quoteRepository;
3839

3940
/**
40-
* @var Address|\PHPUnit_Framework_MockObject_MockObject
41+
* @var Address|MockObject
4142
*/
42-
private $billingAddressMock;
43+
private $billingAddress;
4344

4445
/**
45-
* @var Address|\PHPUnit_Framework_MockObject_MockObject
46+
* @var Address|MockObject
4647
*/
47-
private $shippingAddressMock;
48+
private $shippingAddress;
4849

4950
/**
5051
* @var QuoteUpdater
5152
*/
5253
private $quoteUpdater;
5354

5455
/**
55-
* @return void
56+
* @inheritdoc
5657
*/
5758
protected function setUp()
5859
{
59-
$this->configMock = $this->getMockBuilder(Config::class)
60+
$this->config = $this->getMockBuilder(Config::class)
6061
->disableOriginalConstructor()
6162
->getMock();
62-
$this->quoteRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
63+
$this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class)
6364
->getMockForAbstractClass();
6465

65-
$this->billingAddressMock = $this->getMockBuilder(Address::class)
66+
$this->billingAddress = $this->getMockBuilder(Address::class)
6667
->setMethods(
6768
[
6869
'setLastname',
@@ -77,9 +78,10 @@ protected function setUp()
7778
'setShouldIgnoreValidation',
7879
'getEmail'
7980
]
80-
)->disableOriginalConstructor()
81+
)
82+
->disableOriginalConstructor()
8183
->getMock();
82-
$this->shippingAddressMock = $this->getMockBuilder(Address::class)
84+
$this->shippingAddress = $this->getMockBuilder(Address::class)
8385
->setMethods(
8486
[
8587
'setLastname',
@@ -93,61 +95,61 @@ protected function setUp()
9395
'setPostcode',
9496
'setShouldIgnoreValidation'
9597
]
96-
)->disableOriginalConstructor()
98+
)
99+
->disableOriginalConstructor()
97100
->getMock();
98101

99102
$this->quoteUpdater = new QuoteUpdater(
100-
$this->configMock,
101-
$this->quoteRepositoryMock
103+
$this->config,
104+
$this->quoteRepository
102105
);
103106
}
104107

105108
/**
106-
* @return void
109+
* Checks if quote details can be update by the response from Braintree.
110+
*
107111
* @throws \Magento\Framework\Exception\LocalizedException
108112
*/
109-
public function testExecute()
113+
public function testExecute(): void
110114
{
111115
$details = $this->getDetails();
112-
$quoteMock = $this->getQuoteMock();
113-
$paymentMock = $this->getPaymentMock();
116+
$quote = $this->getQuoteMock();
117+
$payment = $this->getPaymentMock();
114118

115-
$quoteMock->expects(self::once())
116-
->method('getPayment')
117-
->willReturn($paymentMock);
119+
$quote->method('getPayment')
120+
->willReturn($payment);
118121

119-
$paymentMock->expects(self::once())
120-
->method('setMethod')
122+
$payment->method('setMethod')
121123
->with(ConfigProvider::PAYPAL_CODE);
122-
$paymentMock->expects(self::once())
123-
->method('setAdditionalInformation')
124+
$payment->method('setAdditionalInformation')
124125
->with(DataAssignObserver::PAYMENT_METHOD_NONCE, self::TEST_NONCE);
125126

126-
$this->updateQuoteStep($quoteMock, $details);
127+
$this->updateQuoteStep($quote, $details);
127128

128-
$this->quoteUpdater->execute(self::TEST_NONCE, $details, $quoteMock);
129+
$this->quoteUpdater->execute(self::TEST_NONCE, $details, $quote);
129130
}
130131

131132
/**
133+
* Disables quote's addresses validation.
134+
*
132135
* @return void
133136
*/
134-
private function disabledQuoteAddressValidationStep()
137+
private function disabledQuoteAddressValidationStep(): void
135138
{
136-
$this->billingAddressMock->expects(self::once())
137-
->method('setShouldIgnoreValidation')
139+
$this->billingAddress->method('setShouldIgnoreValidation')
138140
->with(true);
139-
$this->shippingAddressMock->expects(self::once())
140-
->method('setShouldIgnoreValidation')
141+
$this->shippingAddress->method('setShouldIgnoreValidation')
141142
->with(true);
142-
$this->billingAddressMock->expects(self::once())
143-
->method('getEmail')
143+
$this->billingAddress->method('getEmail')
144144
->willReturn('bt_buyer_us@paypal.com');
145145
}
146146

147147
/**
148+
* Gets quote's details.
149+
*
148150
* @return array
149151
*/
150-
private function getDetails()
152+
private function getDetails(): array
151153
{
152154
return [
153155
'email' => 'bt_buyer_us@paypal.com',
@@ -177,54 +179,51 @@ private function getDetails()
177179
}
178180

179181
/**
182+
* Updates shipping address details.
183+
*
180184
* @param array $details
181185
*/
182-
private function updateShippingAddressStep(array $details)
186+
private function updateShippingAddressStep(array $details): void
183187
{
184-
$this->shippingAddressMock->expects(self::once())
185-
->method('setLastname')
188+
$this->shippingAddress->method('setLastname')
186189
->with($details['lastName']);
187-
$this->shippingAddressMock->expects(self::once())
188-
->method('setFirstname')
190+
$this->shippingAddress->method('setFirstname')
189191
->with($details['firstName']);
190-
$this->shippingAddressMock->expects(self::once())
191-
->method('setEmail')
192+
$this->shippingAddress->method('setEmail')
192193
->with($details['email']);
193-
$this->shippingAddressMock->expects(self::once())
194-
->method('setCollectShippingRates')
194+
$this->shippingAddress->method('setCollectShippingRates')
195195
->with(true);
196196

197-
$this->updateAddressDataStep($this->shippingAddressMock, $details['shippingAddress']);
197+
$this->updateAddressDataStep($this->shippingAddress, $details['shippingAddress']);
198198
}
199199

200200
/**
201-
* @param \PHPUnit_Framework_MockObject_MockObject $addressMock
201+
* Updates address details.
202+
*
203+
* @param MockObject $address
202204
* @param array $addressData
203205
*/
204-
private function updateAddressDataStep(\PHPUnit_Framework_MockObject_MockObject $addressMock, array $addressData)
206+
private function updateAddressDataStep(MockObject $address, array $addressData): void
205207
{
206-
$addressMock->expects(self::once())
207-
->method('setStreet')
208+
$address->method('setStreet')
208209
->with([$addressData['streetAddress'], $addressData['extendedAddress']]);
209-
$addressMock->expects(self::once())
210-
->method('setCity')
210+
$address->method('setCity')
211211
->with($addressData['locality']);
212-
$addressMock->expects(self::once())
213-
->method('setRegionCode')
212+
$address->method('setRegionCode')
214213
->with($addressData['region']);
215-
$addressMock->expects(self::once())
216-
->method('setCountryId')
214+
$address->method('setCountryId')
217215
->with($addressData['countryCodeAlpha2']);
218-
$addressMock->expects(self::once())
219-
->method('setPostcode')
216+
$address->method('setPostcode')
220217
->with($addressData['postalCode']);
221218
}
222219

223220
/**
224-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
221+
* Updates quote's address details.
222+
*
223+
* @param MockObject $quoteMock
225224
* @param array $details
226225
*/
227-
private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
226+
private function updateQuoteAddressStep(MockObject $quoteMock, array $details): void
228227
{
229228
$quoteMock->expects(self::exactly(2))
230229
->method('getIsVirtual')
@@ -235,64 +234,61 @@ private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject
235234
}
236235

237236
/**
237+
* Updates billing address details.
238+
*
238239
* @param array $details
239240
*/
240-
private function updateBillingAddressStep(array $details)
241+
private function updateBillingAddressStep(array $details): void
241242
{
242-
$this->configMock->expects(self::once())
243-
->method('isRequiredBillingAddress')
243+
$this->config->method('isRequiredBillingAddress')
244244
->willReturn(true);
245245

246-
$this->updateAddressDataStep($this->billingAddressMock, $details['billingAddress']);
246+
$this->updateAddressDataStep($this->billingAddress, $details['billingAddress']);
247247

248-
$this->billingAddressMock->expects(self::once())
249-
->method('setLastname')
248+
$this->billingAddress->method('setLastname')
250249
->with($details['lastName']);
251-
$this->billingAddressMock->expects(self::once())
252-
->method('setFirstname')
250+
$this->billingAddress->method('setFirstname')
253251
->with($details['firstName']);
254-
$this->billingAddressMock->expects(self::once())
255-
->method('setEmail')
252+
$this->billingAddress->method('setEmail')
256253
->with($details['email']);
257254
}
258255

259256
/**
260-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
257+
* Updates quote details.
258+
*
259+
* @param MockObject $quote
261260
* @param array $details
262261
*/
263-
private function updateQuoteStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
262+
private function updateQuoteStep(MockObject $quote, array $details): void
264263
{
265-
$quoteMock->expects(self::once())
266-
->method('setMayEditShippingAddress')
264+
$quote->method('setMayEditShippingAddress')
267265
->with(false);
268-
$quoteMock->expects(self::once())
269-
->method('setMayEditShippingMethod')
266+
$quote->method('setMayEditShippingMethod')
270267
->with(true);
271268

272-
$quoteMock->expects(self::exactly(2))
273-
->method('getShippingAddress')
274-
->willReturn($this->shippingAddressMock);
275-
$quoteMock->expects(self::exactly(2))
269+
$quote->method('getShippingAddress')
270+
->willReturn($this->shippingAddress);
271+
$quote->expects(self::exactly(2))
276272
->method('getBillingAddress')
277-
->willReturn($this->billingAddressMock);
273+
->willReturn($this->billingAddress);
278274

279-
$this->updateQuoteAddressStep($quoteMock, $details);
275+
$this->updateQuoteAddressStep($quote, $details);
280276
$this->disabledQuoteAddressValidationStep();
281277

282-
$quoteMock->expects(self::once())
283-
->method('collectTotals');
278+
$quote->method('collectTotals');
284279

285-
$this->quoteRepositoryMock->expects(self::once())
286-
->method('save')
287-
->with($quoteMock);
280+
$this->quoteRepository->method('save')
281+
->with($quote);
288282
}
289283

290284
/**
291-
* @return Quote|\PHPUnit_Framework_MockObject_MockObject
285+
* Creates a mock for Quote object.
286+
*
287+
* @return Quote|MockObject
292288
*/
293-
private function getQuoteMock()
289+
private function getQuoteMock(): MockObject
294290
{
295-
$quoteMock = $this->getMockBuilder(Quote::class)
291+
$quote = $this->getMockBuilder(Quote::class)
296292
->setMethods(
297293
[
298294
'getIsVirtual',
@@ -304,25 +300,27 @@ private function getQuoteMock()
304300
'getBillingAddress',
305301
'getExtensionAttributes'
306302
]
307-
)->disableOriginalConstructor()
303+
)
304+
->disableOriginalConstructor()
308305
->getMock();
309306

310-
$cartExtensionMock = $this->getMockBuilder(CartExtensionInterface::class)
307+
$cartExtension = $this->getMockBuilder(CartExtensionInterface::class)
311308
->setMethods(['setShippingAssignments'])
312309
->disableOriginalConstructor()
313310
->getMockForAbstractClass();
314311

315-
$quoteMock->expects(self::any())
316-
->method('getExtensionAttributes')
317-
->willReturn($cartExtensionMock);
312+
$quote->method('getExtensionAttributes')
313+
->willReturn($cartExtension);
318314

319-
return $quoteMock;
315+
return $quote;
320316
}
321317

322318
/**
323-
* @return Payment|\PHPUnit_Framework_MockObject_MockObject
319+
* Creates a mock for Payment object.
320+
*
321+
* @return Payment|MockObject
324322
*/
325-
private function getPaymentMock()
323+
private function getPaymentMock(): MockObject
326324
{
327325
return $this->getMockBuilder(Payment::class)
328326
->disableOriginalConstructor()

0 commit comments

Comments
 (0)