Skip to content

Commit f295aa0

Browse files
committed
MAGETWO-69260: Unable to create shipping label due to error of lb to kg conversion
- Replace the `round` method by `sprintf`
1 parent a45e591 commit f295aa0

File tree

7 files changed

+143
-133
lines changed

7 files changed

+143
-133
lines changed

app/code/Magento/Dhl/Block/Adminhtml/Unitofmeasure.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ public function _construct()
7171
)
7272
);
7373

74-
$weight = round(
75-
$this->carrierHelper->convertMeasureWeight(
76-
$kgWeight,
77-
\Zend_Measure_Weight::KILOGRAM,
78-
\Zend_Measure_Weight::POUND
79-
),
80-
3
74+
$convertedWeight = $this->carrierHelper->convertMeasureWeight(
75+
$kgWeight,
76+
\Zend_Measure_Weight::KILOGRAM,
77+
\Zend_Measure_Weight::POUND
8178
);
79+
$weight = sprintf('%.3f', $convertedWeight);
8280

8381
$this->setDivideOrderWeightNoteLbp(
8482
__(

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public function setRequest(\Magento\Framework\DataObject $request)
422422

423423
$shippingWeight = $request->getPackageWeight();
424424

425-
$requestObject->setValue(round($request->getPackageValue(), 2))
425+
$requestObject->setValue(sprintf('%.2f', $request->getPackageValue()))
426426
->setValueWithDiscount($request->getPackageValueWithDiscount())
427427
->setCustomsValue($request->getPackageCustomsValue())
428428
->setDestStreet($this->string->substr(str_replace("\n", '', $request->getDestStreet()), 0, 35))
@@ -670,13 +670,13 @@ protected function _getWeight($weight, $maxWeight = false, $configWeightUnit = f
670670

671671
if ($configWeightUnit != $countryWeightUnit) {
672672
$weight = $this->_carrierHelper->convertMeasureWeight(
673-
round($weight, 3),
673+
sprintf('%.3f', $weight),
674674
$configWeightUnit,
675675
$countryWeightUnit
676676
);
677677
}
678678

679-
return round($weight, 3);
679+
return sprintf('%.3f', $weight);
680680
}
681681

682682
/**
@@ -806,7 +806,7 @@ protected function _makePieces(\Magento\Shipping\Model\Simplexml\Element $nodeBk
806806
$nodePiece = $nodePieces->addChild('Piece', '', '');
807807
$nodePiece->addChild('PieceID', $numberOfPieces);
808808
$this->_addDimension($nodePiece);
809-
$nodePiece->addChild('Weight', $sumWeight);
809+
$nodePiece->addChild('Weight', sprintf('%.3f', $sumWeight));
810810
break;
811811
} else {
812812
unset($items[$key]);
@@ -815,7 +815,7 @@ protected function _makePieces(\Magento\Shipping\Model\Simplexml\Element $nodeBk
815815
$nodePiece = $nodePieces->addChild('Piece', '', '');
816816
$nodePiece->addChild('PieceID', $numberOfPieces);
817817
$this->_addDimension($nodePiece);
818-
$nodePiece->addChild('Weight', $sumWeight);
818+
$nodePiece->addChild('Weight', sprintf('%.3f', $sumWeight));
819819
$sumWeight = 0;
820820
break;
821821
}
@@ -826,7 +826,7 @@ protected function _makePieces(\Magento\Shipping\Model\Simplexml\Element $nodeBk
826826
$nodePiece = $nodePieces->addChild('Piece', '', '');
827827
$nodePiece->addChild('PieceID', $numberOfPieces);
828828
$this->_addDimension($nodePiece);
829-
$nodePiece->addChild('Weight', $sumWeight);
829+
$nodePiece->addChild('Weight', sprintf('%.3f', $sumWeight));
830830
}
831831
} else {
832832
$nodePiece = $nodePieces->addChild('Piece', '', '');
@@ -870,13 +870,13 @@ protected function _getDimension($dimension, $configWeightUnit = false)
870870

871871
if ($configDimensionUnit != $countryDimensionUnit) {
872872
$dimension = $this->_carrierHelper->convertMeasureDimension(
873-
round($dimension, 3),
873+
sprintf('%.3f', $dimension),
874874
$configDimensionUnit,
875875
$countryDimensionUnit
876876
);
877877
}
878878

879-
return round($dimension, 3);
879+
return sprintf('%.3f', $dimension);
880880
}
881881

882882
/**
@@ -1614,7 +1614,7 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
16141614
}
16151615
$nodePiece->addChild('PieceID', ++$i);
16161616
$nodePiece->addChild('PackageType', $packageType);
1617-
$nodePiece->addChild('Weight', round($package['params']['weight'], 1));
1617+
$nodePiece->addChild('Weight', sprintf('%.1f', $package['params']['weight']));
16181618
$params = $package['params'];
16191619
if ($params['width'] && $params['length'] && $params['height']) {
16201620
if (!$originRegion) {
@@ -1635,7 +1635,7 @@ protected function _shipmentDetails($xml, $rawRequest, $originRegion = '')
16351635
}
16361636

16371637
if (!$originRegion) {
1638-
$nodeShipmentDetails->addChild('Weight', round($rawRequest->getPackageWeight(), 1));
1638+
$nodeShipmentDetails->addChild('Weight', sprintf('%.1f', $rawRequest->getPackageWeight()));
16391639
$nodeShipmentDetails->addChild('WeightUnit', substr($this->_getWeightUnit(), 0, 1));
16401640
$nodeShipmentDetails->addChild('GlobalProductCode', $rawRequest->getShippingMethod());
16411641
$nodeShipmentDetails->addChild('LocalProductCode', $rawRequest->getShippingMethod());

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

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,65 @@
55
*/
66
namespace Magento\Dhl\Test\Unit\Model;
77

8-
use Magento\Quote\Model\Quote\Address\RateRequest;
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\HTTP\ZendClient;
10+
use Magento\Framework\HTTP\ZendClientFactory;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
912
use Magento\Framework\Xml\Security;
13+
use Magento\Quote\Model\Quote\Address\RateRequest;
14+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1015

1116
/**
1217
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1318
*/
1419
class CarrierTest extends \PHPUnit_Framework_TestCase
1520
{
1621
/**
17-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
22+
* @var ObjectManager
1823
*/
19-
protected $_helper;
24+
private $objectManager;
2025

2126
/**
22-
* @var \PHPUnit_Framework_MockObject_MockObject
27+
* @var \Zend_Http_Response|MockObject
2328
*/
24-
protected $_httpResponse;
29+
private $httpResponse;
2530

2631
/**
2732
* @var \Magento\Dhl\Model\Carrier
2833
*/
29-
protected $_model;
34+
private $model;
3035

3136
/**
32-
* @var \Magento\Quote\Model\Quote\Address\RateResult\Error|\PHPUnit_Framework_MockObject_MockObject
37+
* @var \Magento\Quote\Model\Quote\Address\RateResult\Error|MockObject
3338
*/
34-
protected $error;
39+
private $error;
3540

3641
/**
37-
* @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory|\PHPUnit_Framework_MockObject_MockObject
42+
* @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory|MockObject
3843
*/
39-
protected $errorFactory;
44+
private $errorFactory;
4045

4146
/**
42-
* @var \Magento\Dhl\Model\Carrier|\PHPUnit_Framework_MockObject_MockObject
47+
* @var ScopeConfigInterface|MockObject
4348
*/
44-
protected $carrier;
49+
private $scope;
4550

4651
/**
47-
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
52+
* @var ZendClient|MockObject
4853
*/
49-
protected $scope;
54+
private $httpClient;
5055

5156
/**
5257
* @return void
5358
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
5459
*/
5560
protected function setUp()
5661
{
57-
$this->_helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
62+
$this->objectManager = new ObjectManager($this);
5863

59-
$this->scope = $this->getMockBuilder(
60-
\Magento\Framework\App\Config\ScopeConfigInterface::class
61-
)->disableOriginalConstructor()->getMock();
62-
63-
$this->scope->expects(
64-
$this->any()
65-
)->method(
66-
'getValue'
67-
)->will(
68-
$this->returnCallback([$this, 'scopeConfiggetValue'])
69-
);
64+
$this->scope = $this->getMockForAbstractClass(ScopeConfigInterface::class);
65+
$this->scope->method('getValue')
66+
->willReturnCallback([$this, 'scopeConfigGetValue']);
7067

7168
// xml element factory
7269
$xmlElFactory = $this->getMockBuilder(
@@ -77,7 +74,7 @@ protected function setUp()
7774
$xmlElFactory->expects($this->any())->method('create')->will(
7875
$this->returnCallback(
7976
function ($data) {
80-
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
77+
$helper = new ObjectManager($this);
8178

8279
return $helper->getObject(
8380
\Magento\Shipping\Model\Simplexml\Element::class,
@@ -115,27 +112,23 @@ function ($data) {
115112

116113
$rateMethodFactory->expects($this->any())->method('create')->will($this->returnValue($rateMethod));
117114

118-
// http client
119-
$this->_httpResponse = $this->getMockBuilder(
120-
\Zend_Http_Response::class
121-
)->disableOriginalConstructor()->setMethods(
122-
['getBody']
123-
)->getMock();
115+
$this->httpResponse = $this->getMockBuilder(\Zend_Http_Response::class)
116+
->disableOriginalConstructor()
117+
->getMock();
124118

125-
$httpClient = $this->getMockBuilder(
126-
\Magento\Framework\HTTP\ZendClient::class
127-
)->disableOriginalConstructor()->setMethods(
128-
['request']
129-
)->getMock();
130-
$httpClient->expects($this->any())->method('request')->will($this->returnValue($this->_httpResponse));
119+
$this->httpClient = $this->getMockBuilder(ZendClient::class)
120+
->disableOriginalConstructor()
121+
->setMethods(['request'])
122+
->getMock();
123+
$this->httpClient->method('request')
124+
->willReturn($this->httpResponse);
131125

132-
$httpClientFactory = $this->getMockBuilder(
133-
\Magento\Framework\HTTP\ZendClientFactory::class
134-
)->disableOriginalConstructor()->setMethods(
135-
['create']
136-
)->getMock();
126+
$httpClientFactory = $this->getMockBuilder(ZendClientFactory::class)
127+
->disableOriginalConstructor()
128+
->getMock();
129+
$httpClientFactory->method('create')
130+
->willReturn($this->httpClient);
137131

138-
$httpClientFactory->expects($this->any())->method('create')->will($this->returnValue($httpClient));
139132
$modulesDirectory = $this->getMockBuilder(
140133
\Magento\Framework\Filesystem\Directory\Read::class
141134
)->disableOriginalConstructor()->setMethods(
@@ -174,7 +167,7 @@ function ($data) {
174167

175168
$this->errorFactory->expects($this->any())->method('create')->willReturn($this->error);
176169

177-
$this->_model = $this->_helper->getObject(
170+
$this->model = $this->objectManager->getObject(
178171
\Magento\Dhl\Model\Carrier::class,
179172
[
180173
'scopeConfig' => $this->scope,
@@ -192,11 +185,12 @@ function ($data) {
192185
}
193186

194187
/**
195-
* Callback function, emulates getValue function
196-
* @param $path
197-
* @return null|string
188+
* Emulates the config's `getValue` method.
189+
*
190+
* @param string $path
191+
* @return string|null
198192
*/
199-
public function scopeConfiggetValue($path)
193+
public function scopeConfigGetValue($path)
200194
{
201195
$pathMap = [
202196
'carriers/dhl/shipment_days' => 'Mon,Tue,Wed,Thu,Fri,Sat',
@@ -211,6 +205,7 @@ public function scopeConfiggetValue($path)
211205
'carriers/dhl/showmethod' => 1,
212206
'carriers/dhl/title' => 'dhl Title',
213207
'carriers/dhl/specificerrmsg' => 'dhl error message',
208+
'carriers/dhl/unit_of_measure' => 'L',
214209
];
215210
return isset($pathMap[$path]) ? $pathMap[$path] : null;
216211
}
@@ -260,29 +255,32 @@ public function prepareShippingLabelContentExceptionDataProvider()
260255
*/
261256
protected function _invokePrepareShippingLabelContent(\SimpleXMLElement $xml)
262257
{
263-
$model = $this->_helper->getObject(\Magento\Dhl\Model\Carrier::class);
258+
$model = $this->objectManager->getObject(\Magento\Dhl\Model\Carrier::class);
264259
$method = new \ReflectionMethod($model, '_prepareShippingLabelContent');
265260
$method->setAccessible(true);
266261
return $method->invoke($model, $xml);
267262
}
268263

269264
public function testCollectRates()
270265
{
271-
$this->scope->expects($this->any())->method('isSetFlag')->willReturn(true);
266+
$this->scope->method('isSetFlag')
267+
->willReturn(true);
272268

273-
$this->_httpResponse->expects(
274-
$this->any()
275-
)->method(
276-
'getBody'
277-
)->will(
278-
$this->returnValue(file_get_contents(__DIR__ . '/_files/success_dhl_response_rates.xml'))
279-
);
280-
// for setRequest
281-
$request = $this->_helper->getObject(
282-
\Magento\Quote\Model\Quote\Address\RateRequest::class,
269+
$this->httpResponse->method('getBody')
270+
->willReturn(file_get_contents(__DIR__ . '/_files/success_dhl_response_rates.xml'));
271+
272+
/** @var RateRequest $request */
273+
$request = $this->objectManager->getObject(
274+
RateRequest::class,
283275
require __DIR__ . '/_files/rates_request_data_dhl.php'
284276
);
285-
$this->assertNotEmpty($this->_model->collectRates($request)->getAllRates());
277+
278+
$reflectionClass = new \ReflectionObject($this->httpClient);
279+
$rawPostData = $reflectionClass->getProperty('raw_post_data');
280+
$rawPostData->setAccessible(true);
281+
282+
self::assertNotEmpty($this->model->collectRates($request)->getAllRates());
283+
self::assertContains('<Weight>8.266</Weight>', $rawPostData->getValue($this->httpClient));
286284
}
287285

288286
public function testCollectRatesErrorMessage()
@@ -296,7 +294,7 @@ public function testCollectRatesErrorMessage()
296294
$request = new RateRequest();
297295
$request->setPackageWeight(1);
298296

299-
$this->assertSame($this->error, $this->_model->collectRates($request));
297+
$this->assertSame($this->error, $this->model->collectRates($request));
300298
}
301299

302300
public function testCollectRatesFail()
@@ -306,6 +304,6 @@ public function testCollectRatesFail()
306304
$request = new RateRequest();
307305
$request->setPackageWeight(1);
308306

309-
$this->assertFalse(false, $this->_model->collectRates($request));
307+
$this->assertFalse(false, $this->model->collectRates($request));
310308
}
311309
}

app/code/Magento/Dhl/Test/Unit/Model/_files/rates_request_data_dhl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'dest_postal' => '10559',
1515
'package_value' => '5',
1616
'package_value_with_discount' => '5',
17-
'package_weight' => '5',
17+
'package_weight' => '8.2657',
1818
'package_qty' => '1',
1919
'package_physical_value' => '5',
2020
'free_method_weight' => '5',

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ public function setRequest(\Magento\Quote\Model\Quote\Address\RateRequest $reque
340340

341341
$weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
342342
$r->setWeightPounds(floor($weight));
343-
$r->setWeightOunces(round(($weight - floor($weight)) * self::OUNCES_POUND, self::$weightPrecision));
343+
$ounces = ($weight - floor($weight)) * self::OUNCES_POUND;
344+
$r->setWeightOunces(sprintf('%.' . self::$weightPrecision . 'f', $ounces));
344345
if ($request->getFreeMethodWeight() != $request->getPackageWeight()) {
345346
$r->setFreeMethodWeight($request->getFreeMethodWeight());
346347
}
@@ -387,7 +388,8 @@ protected function _setFreeMethodRequest($freeMethod)
387388

388389
$weight = $this->getTotalNumOfBoxes($r->getFreeMethodWeight());
389390
$r->setWeightPounds(floor($weight));
390-
$r->setWeightOunces(round(($weight - floor($weight)) * self::OUNCES_POUND, self::$weightPrecision));
391+
$ounces = ($weight - floor($weight)) * self::OUNCES_POUND;
392+
$r->setWeightOunces(sprintf('%.' . self::$weightPrecision . 'f', $ounces));
391393
$r->setService($freeMethod);
392394
}
393395

0 commit comments

Comments
 (0)