Skip to content

Commit 1e4a9cb

Browse files
authored
Merge pull request #3370 from magento-tsg-csl3/2.2-develop-pr7
[TSG-CSL3] 2.2-develop (pr7)
2 parents 932c1f6 + 26427d5 commit 1e4a9cb

File tree

13 files changed

+515
-143
lines changed

13 files changed

+515
-143
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: 116 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +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

10+
use Magento\Braintree\Gateway\Config\PayPal\Config;
11+
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;
15+
use Magento\Quote\Api\Data\CartExtensionInterface;
816
use Magento\Quote\Model\Quote;
917
use Magento\Quote\Model\Quote\Address;
1018
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;
14-
use Magento\Braintree\Gateway\Config\PayPal\Config;
15-
use Magento\Braintree\Model\Paypal\Helper\QuoteUpdater;
19+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1620

1721
/**
1822
* Class QuoteUpdaterTest
1923
*
20-
* @see \Magento\Braintree\Model\Paypal\Helper\QuoteUpdater
21-
*
2224
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2325
*/
2426
class QuoteUpdaterTest extends \PHPUnit\Framework\TestCase
2527
{
2628
const TEST_NONCE = '3ede7045-2aea-463e-9754-cd658ffeeb48';
2729

2830
/**
29-
* @var Config|\PHPUnit_Framework_MockObject_MockObject
31+
* @var Config|MockObject
3032
*/
31-
private $configMock;
33+
private $config;
3234

3335
/**
34-
* @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
36+
* @var CartRepositoryInterface|MockObject
3537
*/
36-
private $quoteRepositoryMock;
38+
private $quoteRepository;
3739

3840
/**
39-
* @var Address|\PHPUnit_Framework_MockObject_MockObject
41+
* @var Address|MockObject
4042
*/
41-
private $billingAddressMock;
43+
private $billingAddress;
4244

4345
/**
44-
* @var Address|\PHPUnit_Framework_MockObject_MockObject
46+
* @var Address|MockObject
4547
*/
46-
private $shippingAddressMock;
48+
private $shippingAddress;
4749

4850
/**
4951
* @var QuoteUpdater
5052
*/
5153
private $quoteUpdater;
5254

55+
/**
56+
* @inheritdoc
57+
*/
5358
protected function setUp()
5459
{
55-
$this->configMock = $this->getMockBuilder(Config::class)
60+
$this->config = $this->getMockBuilder(Config::class)
5661
->disableOriginalConstructor()
5762
->getMock();
58-
$this->quoteRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
63+
$this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class)
5964
->getMockForAbstractClass();
6065

61-
$this->billingAddressMock = $this->getMockBuilder(Address::class)
66+
$this->billingAddress = $this->getMockBuilder(Address::class)
6267
->setMethods(
6368
[
6469
'setLastname',
@@ -73,9 +78,10 @@ protected function setUp()
7378
'setShouldIgnoreValidation',
7479
'getEmail'
7580
]
76-
)->disableOriginalConstructor()
81+
)
82+
->disableOriginalConstructor()
7783
->getMock();
78-
$this->shippingAddressMock = $this->getMockBuilder(Address::class)
84+
$this->shippingAddress = $this->getMockBuilder(Address::class)
7985
->setMethods(
8086
[
8187
'setLastname',
@@ -89,54 +95,61 @@ protected function setUp()
8995
'setPostcode',
9096
'setShouldIgnoreValidation'
9197
]
92-
)->disableOriginalConstructor()
98+
)
99+
->disableOriginalConstructor()
93100
->getMock();
94101

95102
$this->quoteUpdater = new QuoteUpdater(
96-
$this->configMock,
97-
$this->quoteRepositoryMock
103+
$this->config,
104+
$this->quoteRepository
98105
);
99106
}
100107

108+
/**
109+
* Checks if quote details can be update by the response from Braintree.
110+
*
111+
* @throws \Magento\Framework\Exception\LocalizedException
112+
*/
101113
public function testExecute()
102114
{
103115
$details = $this->getDetails();
104-
$quoteMock = $this->getQuoteMock();
105-
$paymentMock = $this->getPaymentMock();
116+
$quote = $this->getQuoteMock();
117+
$payment = $this->getPaymentMock();
106118

107-
$quoteMock->expects(self::once())
108-
->method('getPayment')
109-
->willReturn($paymentMock);
119+
$quote->method('getPayment')
120+
->willReturn($payment);
110121

111-
$paymentMock->expects(self::once())
112-
->method('setMethod')
122+
$payment->method('setMethod')
113123
->with(ConfigProvider::PAYPAL_CODE);
114-
$paymentMock->expects(self::once())
115-
->method('setAdditionalInformation')
124+
$payment->method('setAdditionalInformation')
116125
->with(DataAssignObserver::PAYMENT_METHOD_NONCE, self::TEST_NONCE);
117126

118-
$this->updateQuoteStep($quoteMock, $details);
127+
$this->updateQuoteStep($quote, $details);
119128

120-
$this->quoteUpdater->execute(self::TEST_NONCE, $details, $quoteMock);
129+
$this->quoteUpdater->execute(self::TEST_NONCE, $details, $quote);
121130
}
122131

132+
/**
133+
* Disables quote's addresses validation.
134+
*
135+
* @return void
136+
*/
123137
private function disabledQuoteAddressValidationStep()
124138
{
125-
$this->billingAddressMock->expects(self::once())
126-
->method('setShouldIgnoreValidation')
139+
$this->billingAddress->method('setShouldIgnoreValidation')
127140
->with(true);
128-
$this->shippingAddressMock->expects(self::once())
129-
->method('setShouldIgnoreValidation')
141+
$this->shippingAddress->method('setShouldIgnoreValidation')
130142
->with(true);
131-
$this->billingAddressMock->expects(self::once())
132-
->method('getEmail')
143+
$this->billingAddress->method('getEmail')
133144
->willReturn('bt_buyer_us@paypal.com');
134145
}
135146

136147
/**
148+
* Gets quote's details.
149+
*
137150
* @return array
138151
*/
139-
private function getDetails()
152+
private function getDetails(): array
140153
{
141154
return [
142155
'email' => 'bt_buyer_us@paypal.com',
@@ -166,54 +179,51 @@ private function getDetails()
166179
}
167180

168181
/**
182+
* Updates shipping address details.
183+
*
169184
* @param array $details
170185
*/
171186
private function updateShippingAddressStep(array $details)
172187
{
173-
$this->shippingAddressMock->expects(self::once())
174-
->method('setLastname')
188+
$this->shippingAddress->method('setLastname')
175189
->with($details['lastName']);
176-
$this->shippingAddressMock->expects(self::once())
177-
->method('setFirstname')
190+
$this->shippingAddress->method('setFirstname')
178191
->with($details['firstName']);
179-
$this->shippingAddressMock->expects(self::once())
180-
->method('setEmail')
192+
$this->shippingAddress->method('setEmail')
181193
->with($details['email']);
182-
$this->shippingAddressMock->expects(self::once())
183-
->method('setCollectShippingRates')
194+
$this->shippingAddress->method('setCollectShippingRates')
184195
->with(true);
185196

186-
$this->updateAddressDataStep($this->shippingAddressMock, $details['shippingAddress']);
197+
$this->updateAddressDataStep($this->shippingAddress, $details['shippingAddress']);
187198
}
188199

189200
/**
190-
* @param \PHPUnit_Framework_MockObject_MockObject $addressMock
201+
* Updates address details.
202+
*
203+
* @param MockObject $address
191204
* @param array $addressData
192205
*/
193-
private function updateAddressDataStep(\PHPUnit_Framework_MockObject_MockObject $addressMock, array $addressData)
206+
private function updateAddressDataStep(MockObject $address, array $addressData)
194207
{
195-
$addressMock->expects(self::once())
196-
->method('setStreet')
208+
$address->method('setStreet')
197209
->with([$addressData['streetAddress'], $addressData['extendedAddress']]);
198-
$addressMock->expects(self::once())
199-
->method('setCity')
210+
$address->method('setCity')
200211
->with($addressData['locality']);
201-
$addressMock->expects(self::once())
202-
->method('setRegionCode')
212+
$address->method('setRegionCode')
203213
->with($addressData['region']);
204-
$addressMock->expects(self::once())
205-
->method('setCountryId')
214+
$address->method('setCountryId')
206215
->with($addressData['countryCodeAlpha2']);
207-
$addressMock->expects(self::once())
208-
->method('setPostcode')
216+
$address->method('setPostcode')
209217
->with($addressData['postalCode']);
210218
}
211219

212220
/**
213-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
221+
* Updates quote's address details.
222+
*
223+
* @param MockObject $quoteMock
214224
* @param array $details
215225
*/
216-
private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
226+
private function updateQuoteAddressStep(MockObject $quoteMock, array $details)
217227
{
218228
$quoteMock->expects(self::exactly(2))
219229
->method('getIsVirtual')
@@ -224,82 +234,92 @@ private function updateQuoteAddressStep(\PHPUnit_Framework_MockObject_MockObject
224234
}
225235

226236
/**
237+
* Updates billing address details.
238+
*
227239
* @param array $details
228240
*/
229241
private function updateBillingAddressStep(array $details)
230242
{
231-
$this->configMock->expects(self::once())
232-
->method('isRequiredBillingAddress')
243+
$this->config->method('isRequiredBillingAddress')
233244
->willReturn(true);
234245

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

237-
$this->billingAddressMock->expects(self::once())
238-
->method('setLastname')
248+
$this->billingAddress->method('setLastname')
239249
->with($details['lastName']);
240-
$this->billingAddressMock->expects(self::once())
241-
->method('setFirstname')
250+
$this->billingAddress->method('setFirstname')
242251
->with($details['firstName']);
243-
$this->billingAddressMock->expects(self::once())
244-
->method('setEmail')
252+
$this->billingAddress->method('setEmail')
245253
->with($details['email']);
246254
}
247255

248256
/**
249-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
257+
* Updates quote details.
258+
*
259+
* @param MockObject $quote
250260
* @param array $details
251261
*/
252-
private function updateQuoteStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
262+
private function updateQuoteStep(MockObject $quote, array $details)
253263
{
254-
$quoteMock->expects(self::once())
255-
->method('setMayEditShippingAddress')
264+
$quote->method('setMayEditShippingAddress')
256265
->with(false);
257-
$quoteMock->expects(self::once())
258-
->method('setMayEditShippingMethod')
266+
$quote->method('setMayEditShippingMethod')
259267
->with(true);
260268

261-
$quoteMock->expects(self::exactly(2))
262-
->method('getShippingAddress')
263-
->willReturn($this->shippingAddressMock);
264-
$quoteMock->expects(self::exactly(2))
269+
$quote->method('getShippingAddress')
270+
->willReturn($this->shippingAddress);
271+
$quote->expects(self::exactly(2))
265272
->method('getBillingAddress')
266-
->willReturn($this->billingAddressMock);
273+
->willReturn($this->billingAddress);
267274

268-
$this->updateQuoteAddressStep($quoteMock, $details);
275+
$this->updateQuoteAddressStep($quote, $details);
269276
$this->disabledQuoteAddressValidationStep();
270277

271-
$quoteMock->expects(self::once())
272-
->method('collectTotals');
278+
$quote->method('collectTotals');
273279

274-
$this->quoteRepositoryMock->expects(self::once())
275-
->method('save')
276-
->with($quoteMock);
280+
$this->quoteRepository->method('save')
281+
->with($quote);
277282
}
278283

279284
/**
280-
* @return Quote|\PHPUnit_Framework_MockObject_MockObject
285+
* Creates a mock for Quote object.
286+
*
287+
* @return Quote|MockObject
281288
*/
282-
private function getQuoteMock()
289+
private function getQuoteMock(): MockObject
283290
{
284-
return $this->getMockBuilder(Quote::class)
291+
$quote = $this->getMockBuilder(Quote::class)
285292
->setMethods(
286293
[
287294
'getIsVirtual',
288295
'getPayment',
296+
'getExtensionAttributes',
289297
'setMayEditShippingAddress',
290298
'setMayEditShippingMethod',
291299
'collectTotals',
292300
'getShippingAddress',
293301
'getBillingAddress',
294302
]
295-
)->disableOriginalConstructor()
303+
)
304+
->disableOriginalConstructor()
296305
->getMock();
306+
307+
$cartExtension = $this->getMockBuilder(CartExtensionInterface::class)
308+
->setMethods(['setShippingAssignments'])
309+
->disableOriginalConstructor()
310+
->getMockForAbstractClass();
311+
312+
$quote->method('getExtensionAttributes')
313+
->willReturn($cartExtension);
314+
return $quote;
297315
}
298316

299317
/**
300-
* @return Payment|\PHPUnit_Framework_MockObject_MockObject
318+
* Creates a mock for Payment object.
319+
*
320+
* @return Payment|MockObject
301321
*/
302-
private function getPaymentMock()
322+
private function getPaymentMock(): MockObject
303323
{
304324
return $this->getMockBuilder(Payment::class)
305325
->disableOriginalConstructor()

0 commit comments

Comments
 (0)