Skip to content

Commit 0408d09

Browse files
committed
Merge remote-tracking branch 'origin/MC-32637' into 2.3-develop-pr40
2 parents 627d609 + c4971a6 commit 0408d09

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Quote\Model;
87

8+
use Magento\Customer\Api\AddressRepositoryInterface;
99
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1010
use Magento\Customer\Model\Session as CustomerSession;
11+
use Magento\Framework\Api\ExtensibleDataInterface;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Exception\CouldNotSaveException;
1314
use Magento\Framework\Exception\InputException;
1415
use Magento\Framework\Exception\NoSuchEntityException;
1516
use Magento\Framework\Exception\StateException;
1617
use Magento\Framework\Reflection\DataObjectProcessor;
18+
use Magento\Quote\Api\CartRepositoryInterface;
1719
use Magento\Quote\Api\Data\AddressInterface;
1820
use Magento\Quote\Api\Data\EstimateAddressInterface;
21+
use Magento\Quote\Api\Data\ShippingMethodInterface;
1922
use Magento\Quote\Api\ShipmentEstimationInterface;
23+
use Magento\Quote\Model\Cart\ShippingMethodConverter;
24+
use Magento\Quote\Model\Quote\Address;
25+
use Magento\Quote\Model\Quote\Address\Rate;
26+
use Magento\Quote\Model\Quote\TotalsCollector;
2027
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
2128

2229
/**
@@ -33,21 +40,21 @@ class ShippingMethodManagement implements
3340
/**
3441
* Quote repository.
3542
*
36-
* @var \Magento\Quote\Api\CartRepositoryInterface
43+
* @var CartRepositoryInterface
3744
*/
3845
protected $quoteRepository;
3946

4047
/**
4148
* Shipping method converter
4249
*
43-
* @var \Magento\Quote\Model\Cart\ShippingMethodConverter
50+
* @var ShippingMethodConverter
4451
*/
4552
protected $converter;
4653

4754
/**
4855
* Customer Address repository
4956
*
50-
* @var \Magento\Customer\Api\AddressRepositoryInterface
57+
* @var AddressRepositoryInterface
5158
*/
5259
protected $addressRepository;
5360

@@ -57,7 +64,7 @@ class ShippingMethodManagement implements
5764
protected $totalsCollector;
5865

5966
/**
60-
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
67+
* @var DataObjectProcessor $dataProcessor
6168
*/
6269
private $dataProcessor;
6370

@@ -79,19 +86,19 @@ class ShippingMethodManagement implements
7986
/**
8087
* Constructor
8188
*
82-
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
89+
* @param CartRepositoryInterface $quoteRepository
8390
* @param Cart\ShippingMethodConverter $converter
84-
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
91+
* @param AddressRepositoryInterface $addressRepository
8592
* @param Quote\TotalsCollector $totalsCollector
8693
* @param AddressInterfaceFactory|null $addressFactory
8794
* @param QuoteAddressResource|null $quoteAddressResource
8895
* @param CustomerSession|null $customerSession
8996
*/
9097
public function __construct(
91-
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
98+
CartRepositoryInterface $quoteRepository,
9299
Cart\ShippingMethodConverter $converter,
93-
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
94-
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
100+
AddressRepositoryInterface $addressRepository,
101+
TotalsCollector $totalsCollector,
95102
AddressInterfaceFactory $addressFactory = null,
96103
QuoteAddressResource $quoteAddressResource = null,
97104
CustomerSession $customerSession = null
@@ -112,10 +119,10 @@ public function __construct(
112119
*/
113120
public function get($cartId)
114121
{
115-
/** @var \Magento\Quote\Model\Quote $quote */
122+
/** @var Quote $quote */
116123
$quote = $this->quoteRepository->getActive($cartId);
117124

118-
/** @var \Magento\Quote\Model\Quote\Address $shippingAddress */
125+
/** @var Address $shippingAddress */
119126
$shippingAddress = $quote->getShippingAddress();
120127
if (!$shippingAddress->getCountryId()) {
121128
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
@@ -127,7 +134,7 @@ public function get($cartId)
127134
}
128135

129136
$shippingAddress->collectShippingRates();
130-
/** @var \Magento\Quote\Model\Quote\Address\Rate $shippingRate */
137+
/** @var Rate $shippingRate */
131138
$shippingRate = $shippingAddress->getShippingRateByCode($shippingMethod);
132139
if (!$shippingRate) {
133140
return null;
@@ -142,7 +149,7 @@ public function getList($cartId)
142149
{
143150
$output = [];
144151

145-
/** @var \Magento\Quote\Model\Quote $quote */
152+
/** @var Quote $quote */
146153
$quote = $this->quoteRepository->getActive($cartId);
147154

148155
// no methods applicable for empty carts or carts with virtual products
@@ -169,7 +176,7 @@ public function getList($cartId)
169176
*/
170177
public function set($cartId, $carrierCode, $methodCode)
171178
{
172-
/** @var \Magento\Quote\Model\Quote $quote */
179+
/** @var Quote $quote */
173180
$quote = $this->quoteRepository->getActive($cartId);
174181
try {
175182
$this->apply($cartId, $carrierCode, $methodCode);
@@ -199,7 +206,7 @@ public function set($cartId, $carrierCode, $methodCode)
199206
*/
200207
public function apply($cartId, $carrierCode, $methodCode)
201208
{
202-
/** @var \Magento\Quote\Model\Quote $quote */
209+
/** @var Quote $quote */
203210
$quote = $this->quoteRepository->getActive($cartId);
204211
if (0 == $quote->getItemsCount()) {
205212
throw new InputException(
@@ -223,9 +230,9 @@ public function apply($cartId, $carrierCode, $methodCode)
223230
/**
224231
* @inheritDoc
225232
*/
226-
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address)
233+
public function estimateByAddress($cartId, EstimateAddressInterface $address)
227234
{
228-
/** @var \Magento\Quote\Model\Quote $quote */
235+
/** @var Quote $quote */
229236
$quote = $this->quoteRepository->getActive($cartId);
230237

231238
// no methods applicable for empty carts or carts with virtual products
@@ -241,7 +248,7 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
241248
*/
242249
public function estimateByExtendedAddress($cartId, AddressInterface $address)
243250
{
244-
/** @var \Magento\Quote\Model\Quote $quote */
251+
/** @var Quote $quote */
245252
$quote = $this->quoteRepository->getActive($cartId);
246253

247254
// no methods applicable for empty carts or carts with virtual products
@@ -256,7 +263,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
256263
*/
257264
public function estimateByAddressId($cartId, $addressId)
258265
{
259-
/** @var \Magento\Quote\Model\Quote $quote */
266+
/** @var Quote $quote */
260267
$quote = $this->quoteRepository->getActive($cartId);
261268

262269
// no methods applicable for empty carts or carts with virtual products
@@ -276,13 +283,13 @@ public function estimateByAddressId($cartId, $addressId)
276283
* @param string $postcode
277284
* @param int $regionId
278285
* @param string $region
279-
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
280-
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
286+
* @param ExtensibleDataInterface|null $address
287+
* @return ShippingMethodInterface[] An array of shipping methods.
281288
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
282289
* @deprecated 100.1.6
283290
*/
284291
protected function getEstimatedRates(
285-
\Magento\Quote\Model\Quote $quote,
292+
Quote $quote,
286293
$country,
287294
$postcode,
288295
$regionId,
@@ -301,9 +308,9 @@ protected function getEstimatedRates(
301308
/**
302309
* Get list of available shipping methods
303310
*
304-
* @param \Magento\Quote\Model\Quote $quote
305-
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
306-
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
311+
* @param Quote $quote
312+
* @param ExtensibleDataInterface $address
313+
* @return ShippingMethodInterface[]
307314
*/
308315
private function getShippingMethods(Quote $quote, $address)
309316
{
@@ -334,27 +341,30 @@ private function getShippingMethods(Quote $quote, $address)
334341
/**
335342
* Get transform address interface into Array
336343
*
337-
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
344+
* @param ExtensibleDataInterface $address
338345
* @return array
339346
*/
340347
private function extractAddressData($address)
341348
{
342349
$className = \Magento\Customer\Api\Data\AddressInterface::class;
343-
if ($address instanceof \Magento\Quote\Api\Data\AddressInterface) {
344-
$className = \Magento\Quote\Api\Data\AddressInterface::class;
350+
if ($address instanceof AddressInterface) {
351+
$className = AddressInterface::class;
345352
} elseif ($address instanceof EstimateAddressInterface) {
346353
$className = EstimateAddressInterface::class;
347354
}
348-
return $this->getDataObjectProcessor()->buildOutputDataArray(
355+
$addressData = $this->getDataObjectProcessor()->buildOutputDataArray(
349356
$address,
350357
$className
351358
);
359+
unset($addressData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
360+
361+
return $addressData;
352362
}
353363

354364
/**
355365
* Gets the data object processor
356366
*
357-
* @return \Magento\Framework\Reflection\DataObjectProcessor
367+
* @return DataObjectProcessor
358368
* @deprecated 101.0.0
359369
*/
360370
private function getDataObjectProcessor()

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Quote\Test\Unit\Model;
99

10+
use Magento\Framework\Api\ExtensibleDataInterface;
1011
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1112
use Magento\Quote\Api\Data\ShippingMethodInterface;
1213
use Magento\Quote\Model\Cart\ShippingMethodConverter;
@@ -31,12 +32,12 @@ class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
3132
protected $model;
3233

3334
/**
34-
* @var \PHPUnit_Framework_MockObject_MockObject
35+
* @var MockObject
3536
*/
3637
protected $shippingMethodMock;
3738

3839
/**
39-
* @var \PHPUnit_Framework_MockObject_MockObject
40+
* @var MockObject
4041
*/
4142
protected $methodDataFactoryMock;
4243

@@ -95,6 +96,9 @@ class ShippingMethodManagementTest extends \PHPUnit\Framework\TestCase
9596
*/
9697
private $quoteAddressResource;
9798

99+
/**
100+
* @inheritdoc
101+
*/
98102
protected function setUp()
99103
{
100104
$this->objectManager = new ObjectManager($this);
@@ -196,6 +200,9 @@ public function testGetMethodWhenShippingAddressIsNotSet()
196200
$this->assertNull($this->model->get($cartId));
197201
}
198202

203+
/**
204+
* Test to returns selected shipping method for a specified quote
205+
*/
199206
public function testGetMethod()
200207
{
201208
$cartId = 666;
@@ -228,6 +235,9 @@ public function testGetMethod()
228235
$this->model->get($cartId);
229236
}
230237

238+
/**
239+
* Test to returns selected shipping method for a specified quote if method isn't set
240+
*/
231241
public function testGetMethodIfMethodIsNotSet()
232242
{
233243
$cartId = 666;
@@ -245,6 +255,9 @@ public function testGetMethodIfMethodIsNotSet()
245255
$this->assertNull($this->model->get($cartId));
246256
}
247257

258+
/**
259+
* Test to get lists applicable shipping methods for a specified quote
260+
*/
248261
public function testGetListForVirtualCart()
249262
{
250263
$cartId = 834;
@@ -256,6 +269,9 @@ public function testGetListForVirtualCart()
256269
$this->assertEquals([], $this->model->getList($cartId));
257270
}
258271

272+
/**
273+
* Test to get lists applicable shipping methods for a specified quote
274+
*/
259275
public function testGetListForEmptyCart()
260276
{
261277
$cartId = 834;
@@ -289,6 +305,9 @@ public function testGetListWhenShippingAddressIsNotSet()
289305
$this->model->getList($cartId);
290306
}
291307

308+
/**
309+
* Test to get lists applicable shipping methods for a specified quote
310+
*/
292311
public function testGetList()
293312
{
294313
$cartId = 834;
@@ -441,6 +460,9 @@ public function testSetMethodWithoutAddress()
441460
$this->model->set($cartId, $carrierCode, $methodCode);
442461
}
443462

463+
/**
464+
* Test to sets the carrier and shipping methods codes for a specified cart
465+
*/
444466
public function testSetMethod()
445467
{
446468
$cartId = 12;
@@ -472,6 +494,9 @@ public function testEstimateByExtendedAddress()
472494
{
473495
$cartId = 1;
474496

497+
$addressExtAttr = [
498+
'discounts' => 100
499+
];
475500
$addressData = [
476501
'region' => 'California',
477502
'region_id' => 23,
@@ -509,7 +534,9 @@ public function testEstimateByExtendedAddress()
509534

510535
$this->dataProcessor->expects(static::any())
511536
->method('buildOutputDataArray')
512-
->willReturn($addressData);
537+
->willReturn($addressData + [ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $addressExtAttr]);
538+
539+
$this->shippingAddress->expects($this->once())->method('addData')->with($addressData);
513540

514541
$this->shippingAddress->expects(static::once())
515542
->method('setCollectShippingRates')

0 commit comments

Comments
 (0)