Skip to content

Commit 0878622

Browse files
authored
Merge pull request #221 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-55117-ShippingMethodManagement-estimateByAddressId-not-full
[Fearless Kiwis] MAGETWO-55117: Shipping method management estimate by address id
2 parents 6806aec + c3c7192 commit 0878622

File tree

2 files changed

+217
-45
lines changed

2 files changed

+217
-45
lines changed

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

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Quote\Api\Data\EstimateAddressInterface;
1414
use Magento\Quote\Api\ShipmentEstimationInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\Framework\Reflection\DataObjectProcessor;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1619

1720
/**
1821
* Shipping method read service.
@@ -49,6 +52,16 @@ class ShippingMethodManagement implements
4952
*/
5053
protected $totalsCollector;
5154

55+
/**
56+
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
57+
*/
58+
private $dataProcessor;
59+
60+
/**
61+
* @var AddressInterfaceFactory $addressFactory
62+
*/
63+
private $addressFactory;
64+
5265
/**
5366
* Constructs a shipping method read service object.
5467
*
@@ -189,13 +202,7 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
189202
return [];
190203
}
191204

192-
return $this->getEstimatedRates(
193-
$quote,
194-
$address->getCountryId(),
195-
$address->getPostcode(),
196-
$address->getRegionId(),
197-
$address->getRegion()
198-
);
205+
return $this->getShippingMethods($quote, $address);
199206
}
200207

201208
/**
@@ -210,7 +217,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
210217
if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
211218
return [];
212219
}
213-
return $this->getShippingMethods($quote, $address->getData());
220+
return $this->getShippingMethods($quote, $address);
214221
}
215222

216223
/**
@@ -227,13 +234,7 @@ public function estimateByAddressId($cartId, $addressId)
227234
}
228235
$address = $this->addressRepository->getById($addressId);
229236

230-
return $this->getEstimatedRates(
231-
$quote,
232-
$address->getCountryId(),
233-
$address->getPostcode(),
234-
$address->getRegionId(),
235-
$address->getRegion()
236-
);
237+
return $this->getShippingMethods($quote, $address);
237238
}
238239

239240
/**
@@ -244,30 +245,39 @@ public function estimateByAddressId($cartId, $addressId)
244245
* @param string $postcode
245246
* @param int $regionId
246247
* @param string $region
248+
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
247249
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
250+
* @deprecated
248251
*/
249-
protected function getEstimatedRates(\Magento\Quote\Model\Quote $quote, $country, $postcode, $regionId, $region)
250-
{
251-
$data = [
252-
EstimateAddressInterface::KEY_COUNTRY_ID => $country,
253-
EstimateAddressInterface::KEY_POSTCODE => $postcode,
254-
EstimateAddressInterface::KEY_REGION_ID => $regionId,
255-
EstimateAddressInterface::KEY_REGION => $region
256-
];
257-
return $this->getShippingMethods($quote, $data);
252+
protected function getEstimatedRates(
253+
\Magento\Quote\Model\Quote $quote,
254+
$country,
255+
$postcode,
256+
$regionId,
257+
$region,
258+
$address = null
259+
) {
260+
if (!$address) {
261+
$address = $this->getAddressFactory()->create()
262+
->setCountryId($country)
263+
->setPostcode($postcode)
264+
->setRegionId($regionId)
265+
->setRegion($region);
266+
}
267+
return $this->getShippingMethods($quote, $address);
258268
}
259269

260270
/**
261271
* Get list of available shipping methods
262272
* @param \Magento\Quote\Model\Quote $quote
263-
* @param array $addressData
273+
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
264274
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
265275
*/
266-
private function getShippingMethods(Quote $quote, array $addressData)
276+
private function getShippingMethods(Quote $quote, $address)
267277
{
268278
$output = [];
269279
$shippingAddress = $quote->getShippingAddress();
270-
$shippingAddress->addData($addressData);
280+
$shippingAddress->addData($this->extractAddressData($address));
271281
$shippingAddress->setCollectShippingRates(true);
272282

273283
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
@@ -279,4 +289,51 @@ private function getShippingMethods(Quote $quote, array $addressData)
279289
}
280290
return $output;
281291
}
292+
293+
/**
294+
* Get transform address interface into Array
295+
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
296+
* @return array
297+
*/
298+
private function extractAddressData($address)
299+
{
300+
$className = \Magento\Customer\Api\Data\AddressInterface::class;
301+
if ($address instanceof \Magento\Quote\Api\Data\AddressInterface) {
302+
$className = \Magento\Quote\Api\Data\AddressInterface::class;
303+
} elseif ($address instanceof EstimateAddressInterface) {
304+
$className = EstimateAddressInterface::class;
305+
}
306+
return $this->getDataObjectProcessor()->buildOutputDataArray(
307+
$address,
308+
$className
309+
);
310+
}
311+
312+
/**
313+
* Gets the data object processor
314+
* @return \Magento\Framework\Reflection\DataObjectProcessor
315+
* @deprecated
316+
*/
317+
private function getDataObjectProcessor()
318+
{
319+
if ($this->dataProcessor === null) {
320+
$this->dataProcessor = ObjectManager::getInstance()
321+
->get(DataObjectProcessor::class);
322+
}
323+
return $this->dataProcessor;
324+
}
325+
326+
/**
327+
* Gets the address factory
328+
* @return AddressInterfaceFactory
329+
* @deprecated
330+
*/
331+
private function getAddressFactory()
332+
{
333+
if ($this->addressFactory === null) {
334+
$this->addressFactory = ObjectManager::getInstance()
335+
->get(AddressInterfaceFactory::class);
336+
}
337+
return $this->addressFactory;
338+
}
282339
}

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

Lines changed: 133 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ class ShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
6363
*/
6464
private $shippingAddress;
6565

66+
/**
67+
* @var \Magento\Framework\Reflection\DataObjectProcessor|MockObject
68+
*/
69+
private $dataProcessor;
70+
71+
/**
72+
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory|MockObject
73+
*/
74+
private $addressFactory;
75+
76+
/**
77+
* @var \Magento\Customer\Api\AddressRepositoryInterface|MockObject
78+
*/
79+
private $addressRepository;
80+
6681
/**
6782
* @var TotalsCollector|MockObject
6883
*/
@@ -72,16 +87,17 @@ protected function setUp()
7287
{
7388
$this->objectManager = new ObjectManager($this);
7489
$this->quoteRepository = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class);
75-
$this->methodDataFactoryMock = $this->getMock(
76-
\Magento\Quote\Api\Data\ShippingMethodInterfaceFactory::class,
77-
[
78-
'create'
79-
],
80-
[],
81-
'',
82-
false
83-
);
90+
$this->addressRepository = $this->getMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
91+
92+
$className = \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory::class;
93+
$this->methodDataFactoryMock = $this->getMock($className, ['create'], [], '', false);
8494

95+
$className = \Magento\Customer\Api\Data\AddressInterfaceFactory::class;
96+
$this->addressFactory = $this->getMock($className, ['create'], [], '', false);
97+
98+
$className = \Magento\Framework\Reflection\DataObjectProcessor::class;
99+
$this->dataProcessor = $this->getMock($className, [], [], '', false);
100+
85101
$this->storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
86102
$this->quote = $this->getMockBuilder(Quote::class)
87103
->disableOriginalConstructor()
@@ -132,9 +148,22 @@ protected function setUp()
132148
'quoteRepository' => $this->quoteRepository,
133149
'methodDataFactory' => $this->methodDataFactoryMock,
134150
'converter' => $this->converter,
135-
'totalsCollector' => $this->totalsCollector
151+
'totalsCollector' => $this->totalsCollector,
152+
'addressRepository' => $this->addressRepository
136153
]
137154
);
155+
156+
$this->objectManager->setBackwardCompatibleProperty(
157+
$this->model,
158+
'addressFactory',
159+
$this->addressFactory
160+
);
161+
162+
$this->objectManager->setBackwardCompatibleProperty(
163+
$this->model,
164+
'dataProcessor',
165+
$this->dataProcessor
166+
);
138167
}
139168

140169
/**
@@ -457,11 +486,17 @@ public function testEstimateByExtendedAddress()
457486
];
458487
$currencyCode = 'UAH';
459488

489+
/**
490+
* @var \Magento\Quote\Api\Data\AddressInterface|MockObject $address
491+
*/
460492
$address = $this->getMockBuilder(Address::class)
461493
->disableOriginalConstructor()
462-
->setMethods(['getData'])
463494
->getMock();
464495

496+
$this->addressFactory->expects($this->any())
497+
->method('create')
498+
->will($this->returnValue($address));
499+
465500
$this->quoteRepository->expects(static::once())
466501
->method('getActive')
467502
->with($cartId)
@@ -474,18 +509,98 @@ public function testEstimateByExtendedAddress()
474509
->method('getItemsCount')
475510
->willReturn(1);
476511

477-
$address->expects(static::once())
478-
->method('getData')
479-
->willReturn($addressData);
480-
481512
$this->quote->expects(static::once())
482513
->method('getShippingAddress')
483514
->willReturn($this->shippingAddress);
484515

516+
$this->dataProcessor->expects(static::any())
517+
->method('buildOutputDataArray')
518+
->willReturn($addressData);
519+
485520
$this->shippingAddress->expects(static::once())
486-
->method('addData')
487-
->with($addressData)
521+
->method('setCollectShippingRates')
522+
->with(true)
523+
->willReturnSelf();
524+
525+
$this->totalsCollector->expects(static::once())
526+
->method('collectAddressTotals')
527+
->with($this->quote, $this->shippingAddress)
488528
->willReturnSelf();
529+
530+
$rate = $this->getMockBuilder(Rate::class)
531+
->disableOriginalConstructor()
532+
->setMethods([])
533+
->getMock();
534+
$methodObject = $this->getMockForAbstractClass(ShippingMethodInterface::class);
535+
$expectedRates = [$methodObject];
536+
537+
$this->shippingAddress->expects(static::once())
538+
->method('getGroupedAllShippingRates')
539+
->willReturn([[$rate]]);
540+
541+
$this->quote->expects(static::once())
542+
->method('getQuoteCurrencyCode')
543+
->willReturn($currencyCode);
544+
545+
$this->converter->expects(static::once())
546+
->method('modelToDataObject')
547+
->with($rate, $currencyCode)
548+
->willReturn($methodObject);
549+
550+
$carriersRates = $this->model->estimateByExtendedAddress($cartId, $address);
551+
static::assertEquals($expectedRates, $carriersRates);
552+
}
553+
554+
/**
555+
* @covers \Magento\Quote\Model\ShippingMethodManagement::estimateByAddressId
556+
*/
557+
public function testEstimateByAddressId()
558+
{
559+
$cartId = 1;
560+
561+
$addressData = [
562+
'region' => 'California',
563+
'region_id' => 23,
564+
'country_id' => 1,
565+
'postcode' => 90200
566+
];
567+
$currencyCode = 'UAH';
568+
569+
/**
570+
* @var \Magento\Customer\Api\Data\AddressInterface|MockObject $address
571+
*/
572+
$address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
573+
->disableOriginalConstructor()
574+
->getMock();
575+
576+
$this->addressRepository->expects($this->any())
577+
->method('getById')
578+
->will($this->returnValue($address));
579+
580+
$this->addressFactory->expects($this->any())
581+
->method('create')
582+
->will($this->returnValue($address));
583+
584+
$this->quoteRepository->expects(static::once())
585+
->method('getActive')
586+
->with($cartId)
587+
->willReturn($this->quote);
588+
589+
$this->quote->expects(static::once())
590+
->method('isVirtual')
591+
->willReturn(false);
592+
$this->quote->expects(static::once())
593+
->method('getItemsCount')
594+
->willReturn(1);
595+
596+
$this->quote->expects(static::once())
597+
->method('getShippingAddress')
598+
->willReturn($this->shippingAddress);
599+
600+
$this->dataProcessor->expects(static::any())
601+
->method('buildOutputDataArray')
602+
->willReturn($addressData);
603+
489604
$this->shippingAddress->expects(static::once())
490605
->method('setCollectShippingRates')
491606
->with(true)
@@ -516,7 +631,7 @@ public function testEstimateByExtendedAddress()
516631
->with($rate, $currencyCode)
517632
->willReturn($methodObject);
518633

519-
$carriersRates = $this->model->estimateByExtendedAddress($cartId, $address);
634+
$carriersRates = $this->model->estimateByAddressId($cartId, $address);
520635
static::assertEquals($expectedRates, $carriersRates);
521636
}
522637
}

0 commit comments

Comments
 (0)