Skip to content

Commit dc76e65

Browse files
committed
Merge remote-tracking branch 'origin/MC-33879' into 2.4.1-develop-pr25
2 parents a3af656 + 09bf477 commit dc76e65

File tree

3 files changed

+137
-15
lines changed

3 files changed

+137
-15
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Quote\Model\Quote;
1111

1212
/**
13-
* Class ShippingMethodUpdater
13+
* Class for updating shipping method in the quote.
1414
*/
1515
class ShippingMethodUpdater extends AbstractHelper
1616
{
@@ -58,6 +58,12 @@ public function execute($shippingMethod, Quote $quote)
5858
$this->disabledQuoteAddressValidation($quote);
5959

6060
$shippingAddress->setShippingMethod($shippingMethod);
61+
$quoteExtensionAttributes = $quote->getExtensionAttributes();
62+
if ($quoteExtensionAttributes && $quoteExtensionAttributes->getShippingAssignments()) {
63+
$quoteExtensionAttributes->getShippingAssignments()[0]
64+
->getShipping()
65+
->setMethod($shippingMethod);
66+
}
6167
$shippingAddress->setCollectShippingRates(true);
6268

6369
$quote->collectTotals();

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

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPUnit\Framework\TestCase;
1717

1818
/**
19-
* @see \Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater
19+
* Test shipping method updater
2020
*/
2121
class ShippingMethodUpdaterTest extends TestCase
2222
{
@@ -39,11 +39,19 @@ class ShippingMethodUpdaterTest extends TestCase
3939
*/
4040
private $shippingAddressMock;
4141

42+
/**
43+
* @var Address|MockObject
44+
*/
45+
private $billingAddressMock;
46+
4247
/**
4348
* @var ShippingMethodUpdater
4449
*/
4550
private $shippingMethodUpdater;
4651

52+
/**
53+
* @inheritdoc
54+
*/
4755
protected function setUp(): void
4856
{
4957
$this->configMock = $this->getMockBuilder(Config::class)
@@ -69,16 +77,24 @@ protected function setUp(): void
6977
);
7078
}
7179

72-
public function testExecuteException()
80+
/**
81+
* Test execute exception
82+
*
83+
* @return void
84+
*/
85+
public function testExecuteException(): void
7386
{
7487
$this->expectException(\InvalidArgumentException::class);
75-
$this->expectExceptionMessage('The "shippingMethod" field does not exists.');
88+
$this->expectExceptionMessage("The \"shippingMethod\" field does not exists.");
7689
$quoteMock = $this->getQuoteMock();
7790

7891
$this->shippingMethodUpdater->execute('', $quoteMock);
7992
}
8093

81-
public function testExecute()
94+
/**
95+
* Test execute
96+
*/
97+
public function testExecute(): void
8298
{
8399
$quoteMock = $this->getQuoteMock();
84100

@@ -114,9 +130,13 @@ public function testExecute()
114130
}
115131

116132
/**
133+
* Disable quote address validation
134+
*
117135
* @param MockObject $quoteMock
136+
*
137+
* @return void
118138
*/
119-
private function disabledQuoteAddressValidationStep(MockObject $quoteMock)
139+
private function disabledQuoteAddressValidationStep(MockObject $quoteMock): void
120140
{
121141
$billingAddressMock = $this->getBillingAddressMock($quoteMock);
122142

@@ -139,35 +159,42 @@ private function disabledQuoteAddressValidationStep(MockObject $quoteMock)
139159
}
140160

141161
/**
162+
* Get billing address mock object
163+
*
142164
* @param MockObject $quoteMock
143165
* @return Address|MockObject
144166
*/
145-
private function getBillingAddressMock(MockObject $quoteMock)
167+
private function getBillingAddressMock(MockObject $quoteMock): MockObject
146168
{
147-
$billingAddressMock = $this->getMockBuilder(Address::class)
148-
->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
149-
->disableOriginalConstructor()
150-
->getMock();
169+
if (!isset($this->billingAddressMock)) {
170+
$this->billingAddressMock = $this->getMockBuilder(Address::class)
171+
->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
172+
->disableOriginalConstructor()
173+
->getMock();
174+
}
151175

152176
$quoteMock->expects(self::any())
153177
->method('getBillingAddress')
154-
->willReturn($billingAddressMock);
178+
->willReturn($this->billingAddressMock);
155179

156-
return $billingAddressMock;
180+
return $this->billingAddressMock;
157181
}
158182

159183
/**
184+
* Get quote mock object
185+
*
160186
* @return Quote|MockObject
161187
*/
162-
private function getQuoteMock()
188+
private function getQuoteMock(): MockObject
163189
{
164190
return $this->getMockBuilder(Quote::class)
165191
->setMethods(
166192
[
167193
'collectTotals',
168194
'getBillingAddress',
169195
'getShippingAddress',
170-
'getIsVirtual'
196+
'getIsVirtual',
197+
'getExtensionAttributes'
171198
]
172199
)->disableOriginalConstructor()
173200
->getMock();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Braintree\Model\Paypal\Helper;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test shipping method updater
20+
*/
21+
class ShippingMethodUpdaterTest extends TestCase
22+
{
23+
/**
24+
* @var ShippingMethodUpdater
25+
*/
26+
private $shippingMethodUpdater;
27+
28+
/**
29+
* @var ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$this->objectManager = Bootstrap::getObjectManager();
39+
$this->shippingMethodUpdater = $this->objectManager->get(ShippingMethodUpdater::class);
40+
}
41+
42+
/**
43+
* Tests that shipping method is actually updated in quote.
44+
*
45+
* @return void
46+
* @magentoAppArea frontend
47+
* @magentoConfigFixture default_store carriers/flatrate/active 1
48+
* @magentoConfigFixture default_store carriers/freeshipping/active 1
49+
* @magentoDataFixture Magento/Braintree/Fixtures/paypal_quote.php
50+
*/
51+
public function testExecute(): void
52+
{
53+
$reservedOrderId = 'test01';
54+
/** @var Quote $quote */
55+
$quote = $this->getQuote($reservedOrderId);
56+
57+
$this->assertEquals(
58+
'flatrate_flatrate',
59+
$quote->getShippingAddress()->getShippingMethod()
60+
);
61+
62+
$this->shippingMethodUpdater->execute('freeshipping_freeshipping', $quote);
63+
64+
$this->assertEquals(
65+
'freeshipping_freeshipping',
66+
$quote->getShippingAddress()->getShippingMethod()
67+
);
68+
}
69+
70+
/**
71+
* Gets quote by reserved order ID.
72+
*
73+
* @param string $reservedOrderId
74+
* @return CartInterface
75+
*/
76+
private function getQuote(string $reservedOrderId): CartInterface
77+
{
78+
$searchCriteria = $this->objectManager->get(SearchCriteriaBuilder::class)
79+
->addFilter('reserved_order_id', $reservedOrderId)
80+
->create();
81+
82+
/** @var CartRepositoryInterface $quoteRepository */
83+
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
84+
$items = $quoteRepository->getList($searchCriteria)
85+
->getItems();
86+
87+
return array_pop($items);
88+
}
89+
}

0 commit comments

Comments
 (0)