Skip to content

Commit aa0718b

Browse files
committed
MAGETWO-57098: [Backport] Issue with fetching shipping charges based on city for 2.0.x
1 parent 3699a92 commit aa0718b

File tree

4 files changed

+104
-42
lines changed

4 files changed

+104
-42
lines changed

app/code/Magento/Dhl/view/frontend/web/js/model/shipping-rates-validation-rules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ define(
1515
},
1616
'country_id': {
1717
'required': true
18+
},
19+
'city': {
20+
'required': true
1821
}
1922
};
2023
}

app/code/Magento/Fedex/Model/Carrier.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ public function setRequest(RateRequest $request)
331331
} else {
332332
}
333333

334+
if ($request->getDestCity()) {
335+
$r->setDestCity($request->getDestCity());
336+
}
337+
334338
$weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
335339
$r->setWeight($weight);
336340
if ($request->getFreeMethodWeight() != $request->getPackageWeight()) {
@@ -425,6 +429,10 @@ protected function _formRateRequest($purpose)
425429
],
426430
];
427431

432+
if ($r->getDestCity()) {
433+
$ratesRequest['RequestedShipment']['Recipient']['Address']['City'] = $r->getDestCity();
434+
}
435+
428436
if ($purpose == self::RATE_REQUEST_GENERAL) {
429437
$ratesRequest['RequestedShipment']['RequestedPackageLineItems'][0]['InsuredValue'] = [
430438
'Amount' => $r->getValue(),

app/code/Magento/Fedex/Test/Unit/Model/CarrierTest.php

Lines changed: 90 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
*/
66
namespace Magento\Fedex\Test\Unit\Model;
77

8-
use Magento\Quote\Model\Quote\Address\RateRequest;
9-
use Magento\Framework\DataObject;
8+
use Magento\Fedex\Model\Carrier;
109
use Magento\Framework\Xml\Security;
10+
use Magento\Quote\Model\Quote\Address\RateRequest;
1111

1212
/**
1313
* Class CarrierTest
1414
* @package Magento\Fedex\Model
1515
* TODO refactor me
16+
*
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1618
*/
1719
class CarrierTest extends \PHPUnit_Framework_TestCase
1820
{
@@ -46,92 +48,129 @@ class CarrierTest extends \PHPUnit_Framework_TestCase
4648
/**
4749
* @return void
4850
*/
49-
public function setUp()
51+
protected function setUp()
5052
{
5153
$this->scope = $this->getMockBuilder(
52-
'\Magento\Framework\App\Config\ScopeConfigInterface'
54+
\Magento\Framework\App\Config\ScopeConfigInterface::class
5355
)->disableOriginalConstructor()->getMock();
5456

55-
$this->scope->expects(
56-
$this->any()
57-
)->method(
58-
'getValue'
59-
)->will(
60-
$this->returnCallback([$this, 'scopeConfiggetValue'])
61-
);
62-
57+
$this->scope->expects($this->any())->method('getValue')->willReturnCallback([$this, 'scopeConfiggetValue']);
6358
$country = $this->getMock(
64-
'Magento\Directory\Model\Country',
59+
\Magento\Directory\Model\Country::class,
6560
['load', 'getData', '__wakeup'],
6661
[],
6762
'',
6863
false
6964
);
7065
$country->expects($this->any())->method('load')->will($this->returnSelf());
71-
$countryFactory = $this->getMock('Magento\Directory\Model\CountryFactory', ['create'], [], '', false);
66+
$countryFactory = $this->getMock(\Magento\Directory\Model\CountryFactory::class, ['create'], [], '', false);
7267
$countryFactory->expects($this->any())->method('create')->will($this->returnValue($country));
7368

74-
$rate = $this->getMock('Magento\Shipping\Model\Rate\Result', ['getError'], [], '', false);
75-
$rateFactory = $this->getMock('Magento\Shipping\Model\Rate\ResultFactory', ['create'], [], '', false);
69+
$rate = $this->getMock(\Magento\Shipping\Model\Rate\Result::class, ['getError'], [], '', false);
70+
$rateFactory = $this->getMock(\Magento\Shipping\Model\Rate\ResultFactory::class, ['create'], [], '', false);
7671
$rateFactory->expects($this->any())->method('create')->will($this->returnValue($rate));
77-
78-
$this->error = $this->getMockBuilder('\Magento\Quote\Model\Quote\Address\RateResult\Error')
79-
->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
80-
->getMock();
81-
$this->errorFactory = $this->getMockBuilder('Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory')
82-
->disableOriginalConstructor()
83-
->setMethods(['create'])
84-
->getMock();
72+
$this->error = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateResult\Error::class)
73+
->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])->getMock();
74+
$this->errorFactory = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory::class)
75+
->disableOriginalConstructor()->setMethods(['create'])->getMock();
8576
$this->errorFactory->expects($this->any())->method('create')->willReturn($this->error);
8677

87-
$store = $this->getMock('Magento\Store\Model\Store', ['getBaseCurrencyCode', '__wakeup'], [], '', false);
88-
$storeManager = $this->getMockForAbstractClass('Magento\Store\Model\StoreManagerInterface');
78+
$store = $this->getMock(\Magento\Store\Model\Store::class, ['getBaseCurrencyCode', '__wakeup'], [], '', false);
79+
$storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
8980
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store));
90-
$priceCurrency = $this->getMockBuilder('Magento\Framework\Pricing\PriceCurrencyInterface')->getMock();
81+
$priceCurrency = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)->getMock();
9182

9283
$rateMethod = $this->getMock(
93-
'Magento\Quote\Model\Quote\Address\RateResult\Method',
84+
\Magento\Quote\Model\Quote\Address\RateResult\Method::class,
9485
null,
9586
['priceCurrency' => $priceCurrency]
9687
);
9788
$rateMethodFactory = $this->getMock(
98-
'Magento\Quote\Model\Quote\Address\RateResult\MethodFactory',
89+
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory::class,
9990
['create'],
10091
[],
10192
'',
10293
false
10394
);
10495
$rateMethodFactory->expects($this->any())->method('create')->will($this->returnValue($rateMethod));
10596
$this->_model = $this->getMock(
106-
'Magento\Fedex\Model\Carrier',
97+
\Magento\Fedex\Model\Carrier::class,
10798
['_getCachedQuotes', '_debug'],
10899
[
109100
'scopeConfig' => $this->scope,
110101
'rateErrorFactory' => $this->errorFactory,
111-
'logger' => $this->getMock('Psr\Log\LoggerInterface'),
102+
'logger' => $this->getMock(\Psr\Log\LoggerInterface::class),
112103
'xmlSecurity' => new Security(),
113-
'xmlElFactory' => $this->getMock('Magento\Shipping\Model\Simplexml\ElementFactory', [], [], '', false),
104+
'xmlElFactory' => $this->getMock(
105+
\Magento\Shipping\Model\Simplexml\ElementFactory::class,
106+
[],
107+
[],
108+
'',
109+
false
110+
),
114111
'rateFactory' => $rateFactory,
115112
'rateMethodFactory' => $rateMethodFactory,
116-
'trackFactory' => $this->getMock('Magento\Shipping\Model\Tracking\ResultFactory', [], [], '', false),
113+
'trackFactory' => $this->getMock(
114+
\Magento\Shipping\Model\Tracking\ResultFactory::class,
115+
[],
116+
[],
117+
'',
118+
false
119+
),
117120
'trackErrorFactory' =>
118-
$this->getMock('Magento\Shipping\Model\Tracking\Result\ErrorFactory', [], [], '', false),
121+
$this->getMock(\Magento\Shipping\Model\Tracking\Result\ErrorFactory::class, [], [], '', false),
119122
'trackStatusFactory' =>
120-
$this->getMock('Magento\Shipping\Model\Tracking\Result\StatusFactory', [], [], '', false),
121-
'regionFactory' => $this->getMock('Magento\Directory\Model\RegionFactory', [], [], '', false),
123+
$this->getMock(\Magento\Shipping\Model\Tracking\Result\StatusFactory::class, [], [], '', false),
124+
'regionFactory' => $this->getMock(\Magento\Directory\Model\RegionFactory::class, [], [], '', false),
122125
'countryFactory' => $countryFactory,
123-
'currencyFactory' => $this->getMock('Magento\Directory\Model\CurrencyFactory', [], [], '', false),
124-
'directoryData' => $this->getMock('Magento\Directory\Helper\Data', [], [], '', false),
125-
'stockRegistry' => $this->getMock('Magento\CatalogInventory\Model\StockRegistry', [], [], '', false),
126+
'currencyFactory' => $this->getMock(\Magento\Directory\Model\CurrencyFactory::class, [], [], '', false),
127+
'directoryData' => $this->getMock(\Magento\Directory\Helper\Data::class, [], [], '', false),
128+
'stockRegistry' => $this->getMock(
129+
\Magento\CatalogInventory\Model\StockRegistry::class,
130+
[],
131+
[],
132+
'',
133+
false
134+
),
126135
'storeManager' => $storeManager,
127-
'configReader' => $this->getMock('Magento\Framework\Module\Dir\Reader', [], [], '', false),
136+
'configReader' => $this->getMock(\Magento\Framework\Module\Dir\Reader::class, [], [], '', false),
128137
'productCollectionFactory' =>
129-
$this->getMock('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory', [], [], '', false),
138+
$this->getMock(
139+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
140+
[],
141+
[],
142+
'',
143+
false
144+
),
130145
'data' => []
131146
]
132147
);
133148
}
134149

150+
public function testSetRequestWithoutCity()
151+
{
152+
$requestMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateRequest::class)
153+
->disableOriginalConstructor()
154+
->setMethods(['getDestCity'])
155+
->getMock();
156+
$requestMock->expects($this->once())
157+
->method('getDestCity')
158+
->willReturn(null);
159+
$this->_model->setRequest($requestMock);
160+
}
161+
162+
public function testSetRequestWithCity()
163+
{
164+
$requestMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\RateRequest::class)
165+
->disableOriginalConstructor()
166+
->setMethods(['getDestCity'])
167+
->getMock();
168+
$requestMock->expects($this->exactly(2))
169+
->method('getDestCity')
170+
->willReturn('Small Town');
171+
$this->_model->setRequest($requestMock);
172+
}
173+
135174
/**
136175
* Callback function, emulates getValue function
137176
* @param $path
@@ -178,7 +217,16 @@ public function testCollectRatesRateAmountOriginBased($amount, $rateType, $expec
178217
$this->_model->expects($this->any())->method('_getCachedQuotes')->will(
179218
$this->returnValue(serialize($response))
180219
);
181-
$request = $this->getMock('Magento\Quote\Model\Quote\Address\RateRequest', [], [], '', false);
220+
$request = $this->getMock(
221+
\Magento\Quote\Model\Quote\Address\RateRequest::class,
222+
['getDestCity'],
223+
[],
224+
'',
225+
false
226+
);
227+
$request->expects($this->exactly(2))
228+
->method('getDestCity')
229+
->willReturn('Wonderful City');
182230
foreach ($this->_model->collectRates($request)->getAllRates() as $allRates) {
183231
$this->assertEquals($expected, $allRates->getData('cost'));
184232
}

app/code/Magento/Fedex/view/frontend/web/js/model/shipping-rates-validation-rules.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ define(
1515
},
1616
'country_id': {
1717
'required': true
18+
},
19+
'city': {
20+
'required': true
1821
}
1922
};
2023
}

0 commit comments

Comments
 (0)