From dca4539171f8346ba0d68585a52adbba9bd7e3e4 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 06:18:31 +0100 Subject: [PATCH 1/7] added tests --- .phpunit.dist.xml | 3 + app/code/core/Mage/Usa/Helper/Data.php | 3 +- tests/unit/Mage/Usa/Helper/DataTest.php | 92 +++++++++++++++++++ .../Mage/Usa/Helper/DataTrait.php | 65 +++++++++++++ 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Mage/Usa/Helper/DataTest.php create mode 100644 tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php diff --git a/.phpunit.dist.xml b/.phpunit.dist.xml index 4ea1c4749d8..72c2fc2ffa2 100644 --- a/.phpunit.dist.xml +++ b/.phpunit.dist.xml @@ -106,6 +106,9 @@ tests/unit/Mage/Uploader + + tests/unit/Mage/Usa + tests/unit/Mage/Wishlist diff --git a/app/code/core/Mage/Usa/Helper/Data.php b/app/code/core/Mage/Usa/Helper/Data.php index c4be9594f26..e081eb02894 100644 --- a/app/code/core/Mage/Usa/Helper/Data.php +++ b/app/code/core/Mage/Usa/Helper/Data.php @@ -21,6 +21,8 @@ class Mage_Usa_Helper_Data extends Mage_Core_Helper_Abstract * @param string $sourceWeightMeasure * @param string $toWeightMeasure * @return null|int|string + * @throws Zend_Locale_Exception + * @throws Zend_Measure_Exception */ public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure) { @@ -132,7 +134,6 @@ public function displayGirthValue($shippingMethod) * Validate ups type value * * @param string $valueForCheck ups type value for check - * * @return bool */ public function validateUpsType($valueForCheck) diff --git a/tests/unit/Mage/Usa/Helper/DataTest.php b/tests/unit/Mage/Usa/Helper/DataTest.php new file mode 100644 index 00000000000..8f99e837925 --- /dev/null +++ b/tests/unit/Mage/Usa/Helper/DataTest.php @@ -0,0 +1,92 @@ +convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure); + #self::assertIsNotFloat($result); + self::assertIsString($result); + #self::assertSame($expectedResult, $result); + self::assertStringStartsWith($expectedResult, $result); + } + + /** + * @dataProvider provideConvertMeasureDimensionData + * @group Helper + */ + public function testConvertMeasureDimension($expectedResult, $value, $sourceWeightMeasure, $toWeightMeasure): void + { + $result = self::$subject->convertMeasureDimension($value, $sourceWeightMeasure, $toWeightMeasure); + #self::assertIsNotFloat($result); + self::assertIsString($result); + #self::assertSame($expectedResult, $result); + self::assertStringStartsWith($expectedResult, $result); + } + + /** + * @dataProvider provideGetMeasureWeightNameData + * @group Helper + */ + public function testGetMeasureWeightName($expectedResult, $value): void + { + $result = self::$subject->getMeasureWeightName($value); + self::assertIsString($result); + self::assertSame($expectedResult, $result); + } + + /** + * @dataProvider provideGetMeasureDimensionNameData + * @group Helper + */ + public function testGetMeasureDimensionName($expectedResult, $value): void + { + $result = self::$subject->getMeasureDimensionName($value); + self::assertIsString($result); + self::assertSame($expectedResult, $result); + } + + /** + * @dataProvider provideDisplayGirthValueData + * @group Helper + */ + public function testDisplayGirthValue(bool $expectedResult, string $value): void + { + self::assertSame($expectedResult, self::$subject->displayGirthValue($value)); + } + + public function testCalidateUpsType(): void + { + self::assertIsBool(self::$subject->validateUpsType('invalid')); + } +} diff --git a/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php new file mode 100644 index 00000000000..ae834a322e9 --- /dev/null +++ b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php @@ -0,0 +1,65 @@ + [ + '22.046', + 10, + 'KILOGRAM', + 'POUND', + ]; + } + + public function provideConvertMeasureDimensionData(): Generator + { + yield 'm to cm' => [ + '1000', + 10, + 'METER', + 'CENTIMETER', + ]; + } + + public function provideGetMeasureWeightNameData(): Generator + { + yield 'kg' => [ + 'kg', + 'KILOGRAM', + ]; + } + + public function provideGetMeasureDimensionNameData(): Generator + { + yield 'm to cm' => [ + 'm', + 'METER', + ]; + } + + public function provideDisplayGirthValueData(): Generator + { + yield 'valid' => [ + true, + 'usps_1', + ]; + + yield 'invalid' => [ + false, + 'invalid', + ]; + } +} From 3560cdefbd0f8914659b2f6210d26f17137ce7ee Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 07:46:00 +0100 Subject: [PATCH 2/7] moved some const from Zend_Measure --- .../core/Mage/Core/Helper/Measure/Length.php | 26 +++++++++ .../core/Mage/Core/Helper/Measure/Weight.php | 28 ++++++++++ .../Usa/Block/Adminhtml/Dhl/Unitofmeasure.php | 4 +- app/code/core/Mage/Usa/Helper/Data.php | 54 +++++++------------ .../Mage/Usa/Model/Shipping/Carrier/Dhl.php | 12 ++--- .../Shipping/Carrier/Dhl/International.php | 32 +++++------ .../Mage/Usa/Model/Shipping/Carrier/Fedex.php | 4 +- .../Mage/Usa/Model/Shipping/Carrier/Ups.php | 8 +-- .../Mage/Usa/Model/Shipping/Carrier/Usps.php | 28 +++++----- .../order/shipment/packaging/popup.phtml | 12 ++--- 10 files changed, 124 insertions(+), 84 deletions(-) create mode 100644 app/code/core/Mage/Core/Helper/Measure/Length.php create mode 100644 app/code/core/Mage/Core/Helper/Measure/Weight.php diff --git a/app/code/core/Mage/Core/Helper/Measure/Length.php b/app/code/core/Mage/Core/Helper/Measure/Length.php new file mode 100644 index 00000000000..3f9b894f506 --- /dev/null +++ b/app/code/core/Mage/Core/Helper/Measure/Length.php @@ -0,0 +1,26 @@ +convertMeasureWeight( $kgWeight, - Zend_Measure_Weight::KILOGRAM, - Zend_Measure_Weight::POUND, + Mage_Core_Helper_Measure_Weight::KILOGRAM, + Mage_Core_Helper_Measure_Weight::POUND, ), 3, ); diff --git a/app/code/core/Mage/Usa/Helper/Data.php b/app/code/core/Mage/Usa/Helper/Data.php index e081eb02894..90cef458abf 100644 --- a/app/code/core/Mage/Usa/Helper/Data.php +++ b/app/code/core/Mage/Usa/Helper/Data.php @@ -7,6 +7,10 @@ * @package Mage_Usa */ +use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure; +use PhpUnitsOfMeasure\PhysicalQuantity\Mass; +use PhpUnitsOfMeasure\PhysicalQuantity\Length; + /** * @package Mage_Usa */ @@ -17,20 +21,16 @@ class Mage_Usa_Helper_Data extends Mage_Core_Helper_Abstract /** * Convert weight in different measure types * - * @param mixed $value + * @param float $value * @param string $sourceWeightMeasure * @param string $toWeightMeasure - * @return null|int|string - * @throws Zend_Locale_Exception - * @throws Zend_Measure_Exception + * @return null|float */ public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure) { if ($value) { - $locale = Mage::app()->getLocale()->getLocale(); - $unitWeight = new Zend_Measure_Weight($value, $sourceWeightMeasure, $locale); - $unitWeight->setType($toWeightMeasure); - return $unitWeight->getValue(); + $unitWeight = new Mass($value, $sourceWeightMeasure); + return $unitWeight->toUnit($toWeightMeasure); } return null; @@ -39,20 +39,16 @@ public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeas /** * Convert dimensions in different measure types * - * @param int|string $value + * @param float $value * @param string $sourceDimensionMeasure * @param string $toDimensionMeasure - * @return null|int|string - * @throws Zend_Locale_Exception - * @throws Zend_Measure_Exception + * @return null|float */ public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDimensionMeasure) { if ($value) { - $locale = Mage::app()->getLocale()->getLocale(); - $unitDimension = new Zend_Measure_Length($value, $sourceDimensionMeasure, $locale); - $unitDimension->setType($toDimensionMeasure); - return $unitDimension->getValue(); + $unitDimension = new Length($value, $sourceDimensionMeasure); + return $unitDimension->toUnit($toDimensionMeasure); } return null; @@ -61,37 +57,27 @@ public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDime /** * Get name of measure by its type * - * @param $key + * @param string $key * @return string - * @throws Zend_Measure_Exception + * @throws UnknownUnitOfMeasure */ public function getMeasureWeightName($key) { - $weight = new Zend_Measure_Weight(0); - $conversionList = $weight->getConversionList(); - if (!empty($conversionList[$key]) && !empty($conversionList[$key][1])) { - return $conversionList[$key][1]; - } - - return ''; + $unit = Mass::getUnit($key); + return $unit->getName(); } /** * Get name of measure by its type * - * @param $key + * @param string $key * @return string - * @throws Zend_Measure_Exception + * @throws UnknownUnitOfMeasure */ public function getMeasureDimensionName($key) { - $weight = new Zend_Measure_Length(0); - $conversionList = $weight->getConversionList(); - if (!empty($conversionList[$key]) && !empty($conversionList[$key][1])) { - return $conversionList[$key][1]; - } - - return ''; + $unit = Length::getUnit($key); + return $unit->getName(); } /** diff --git a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl.php b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl.php index bd1f6e857e9..455195fdacc 100644 --- a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl.php +++ b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl.php @@ -288,29 +288,29 @@ public function setRequest(Varien_Object $request) if ($request->getAction() == 'GenerateLabel') { $packageParams = $request->getPackageParams(); $shippingWeight = $request->getPackageWeight(); - if ($packageParams->getWeightUnits() != Zend_Measure_Weight::POUND) { + if ($packageParams->getWeightUnits() != Mage_Core_Helper_Measure_Weight::POUND) { $shippingWeight = round((float) Mage::helper('usa')->convertMeasureWeight( $request->getPackageWeight(), $packageParams->getWeightUnits(), - Zend_Measure_Weight::POUND, + Mage_Core_Helper_Measure_Weight::POUND, )); } - if ($packageParams->getDimensionUnits() != Zend_Measure_Length::INCH) { + if ($packageParams->getDimensionUnits() != Mage_Core_Helper_Measure_Length::INCH) { $packageParams->setLength(round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getLength(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, ))); $packageParams->setWidth(round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getWidth(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, ))); $packageParams->setHeight(round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getHeight(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, ))); } diff --git a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php index 4e633c1d4e2..5fd7155e44c 100644 --- a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php +++ b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php @@ -427,18 +427,18 @@ public function getCode($type, $code = '') '1' => Mage::helper('usa')->__('Specific'), ], 'dimensions_variables' => [ - 'L' => Zend_Measure_Weight::POUND, - 'LB' => Zend_Measure_Weight::POUND, - 'POUND' => Zend_Measure_Weight::POUND, - 'K' => Zend_Measure_Weight::KILOGRAM, - 'KG' => Zend_Measure_Weight::KILOGRAM, - 'KILOGRAM' => Zend_Measure_Weight::KILOGRAM, - 'I' => Zend_Measure_Length::INCH, - 'IN' => Zend_Measure_Length::INCH, - 'INCH' => Zend_Measure_Length::INCH, - 'C' => Zend_Measure_Length::CENTIMETER, - 'CM' => Zend_Measure_Length::CENTIMETER, - 'CENTIMETER' => Zend_Measure_Length::CENTIMETER, + 'L' => Mage_Core_Helper_Measure_Weight::POUND, + 'LB' => Mage_Core_Helper_Measure_Weight::POUND, + 'POUND' => Mage_Core_Helper_Measure_Weight::POUND, + 'K' => Mage_Core_Helper_Measure_Weight::KILOGRAM, + 'KG' => Mage_Core_Helper_Measure_Weight::KILOGRAM, + 'KILOGRAM' => Mage_Core_Helper_Measure_Weight::KILOGRAM, + 'I' => Mage_Core_Helper_Measure_Length::INCH, + 'IN' => Mage_Core_Helper_Measure_Length::INCH, + 'INCH' => Mage_Core_Helper_Measure_Length::INCH, + 'C' => Mage_Core_Helper_Measure_Length::CENTIMETER, + 'CM' => Mage_Core_Helper_Measure_Length::CENTIMETER, + 'CENTIMETER' => Mage_Core_Helper_Measure_Length::CENTIMETER, ], ]; @@ -537,7 +537,7 @@ public function getDhlProductTitle($code) protected function _getWeight($weight, $maxWeight = false, $configWeightUnit = false) { if ($maxWeight) { - $configWeightUnit = Zend_Measure_Weight::KILOGRAM; + $configWeightUnit = Mage_Core_Helper_Measure_Weight::KILOGRAM; } elseif ($configWeightUnit) { $configWeightUnit = $this->getCode('dimensions_variables', $configWeightUnit); } else { @@ -731,10 +731,10 @@ protected function _getDimension($dimension, $configWeightUnit = false) $configWeightUnit = $this->getCode('dimensions_variables', $configWeightUnit); } - if ($configWeightUnit == Zend_Measure_Weight::POUND) { - $configDimensionUnit = Zend_Measure_Length::INCH; + if ($configWeightUnit == Mage_Core_Helper_Measure_Weight::POUND) { + $configDimensionUnit = Mage_Core_Helper_Measure_Length::INCH; } else { - $configDimensionUnit = Zend_Measure_Length::CENTIMETER; + $configDimensionUnit = Mage_Core_Helper_Measure_Length::CENTIMETER; } $countryDimensionUnit = $this->getCode('dimensions_variables', $this->_getDimensionUnit()); diff --git a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Fedex.php b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Fedex.php index c3f6484df96..3987afb5036 100644 --- a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Fedex.php +++ b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Fedex.php @@ -1340,8 +1340,8 @@ protected function _formShipmentRequest(Varien_Object $request) $height = $packageParams->getHeight(); $width = $packageParams->getWidth(); $length = $packageParams->getLength(); - $weightUnits = $packageParams->getWeightUnits() == Zend_Measure_Weight::POUND ? 'LB' : 'KG'; - $dimensionsUnits = $packageParams->getDimensionUnits() == Zend_Measure_Length::INCH ? 'IN' : 'CM'; + $weightUnits = $packageParams->getWeightUnits() == Mage_Core_Helper_Measure_Weight::POUND ? 'LB' : 'KG'; + $dimensionsUnits = $packageParams->getDimensionUnits() == Mage_Core_Helper_Measure_Length::INCH ? 'IN' : 'CM'; $unitPrice = 0; $itemsQty = 0; $itemsDesc = []; diff --git a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php index 04363fed5f6..f2b59bae443 100644 --- a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php +++ b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php @@ -1379,8 +1379,8 @@ protected function _formShipmentRequest(Varien_Object $request) $height = $packageParams->getHeight(); $width = $packageParams->getWidth(); $length = $packageParams->getLength(); - $weightUnits = $packageParams->getWeightUnits() == Zend_Measure_Weight::POUND ? 'LBS' : 'KGS'; - $dimensionsUnits = $packageParams->getDimensionUnits() == Zend_Measure_Length::INCH ? 'IN' : 'CM'; + $weightUnits = $packageParams->getWeightUnits() == Mage_Core_Helper_Measure_Weight::POUND ? 'LBS' : 'KGS'; + $dimensionsUnits = $packageParams->getDimensionUnits() == Mage_Core_Helper_Measure_Length::INCH ? 'IN' : 'CM'; $xmlRequest = new SimpleXMLElement(''); $requestPart = $xmlRequest->addChild('Request'); @@ -1797,8 +1797,8 @@ protected function _formShipmentRestRequest(Varien_Object $request): string $width = $packageParams->getWidth(); $length = $packageParams->getLength(); $weight = $packageParams->getWeight(); - $weightUnits = $packageParams->getWeightUnits() == Zend_Measure_Weight::POUND ? 'LBS' : 'KGS'; - $dimensionsUnits = $packageParams->getDimensionUnits() == Zend_Measure_Length::INCH ? 'IN' : 'CM'; + $weightUnits = $packageParams->getWeightUnits() == Mage_Core_Helper_Measure_Weight::POUND ? 'LBS' : 'KGS'; + $dimensionsUnits = $packageParams->getDimensionUnits() == Mage_Core_Helper_Measure_Length::INCH ? 'IN' : 'CM'; /** Shipment API Payload */ $shipParams = [ diff --git a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php index 08174e742b0..cb8806e627f 100644 --- a/app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php +++ b/app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php @@ -1353,11 +1353,11 @@ protected function _formUsExpressShipmentRequest(Varien_Object $request) $packageParams = $request->getPackageParams(); $packageWeight = $request->getPackageWeight(); - if ($packageParams->getWeightUnits() != Zend_Measure_Weight::OUNCE) { + if ($packageParams->getWeightUnits() != Mage_Core_Helper_Measure_Weight::OUNCE) { $packageWeight = round((float) Mage::helper('usa')->convertMeasureWeight( $request->getPackageWeight(), $packageParams->getWeightUnits(), - Zend_Measure_Weight::OUNCE, + Mage_Core_Helper_Measure_Weight::OUNCE, )); } @@ -1424,11 +1424,11 @@ protected function _formUsSignatureConfirmationShipmentRequest(Varien_Object $re }; $packageParams = $request->getPackageParams(); $packageWeight = $request->getPackageWeight(); - if ($packageParams->getWeightUnits() != Zend_Measure_Weight::OUNCE) { + if ($packageParams->getWeightUnits() != Mage_Core_Helper_Measure_Weight::OUNCE) { $packageWeight = round((float) Mage::helper('usa')->convertMeasureWeight( $request->getPackageWeight(), $packageParams->getWeightUnits(), - Zend_Measure_Weight::OUNCE, + Mage_Core_Helper_Measure_Weight::OUNCE, )); } @@ -1501,37 +1501,37 @@ protected function _formIntlShipmentRequest(Varien_Object $request) $length = $packageParams->getLength(); $girth = $packageParams->getGirth(); $packageWeight = $request->getPackageWeight(); - if ($packageParams->getWeightUnits() != Zend_Measure_Weight::POUND) { + if ($packageParams->getWeightUnits() != Mage_Core_Helper_Measure_Weight::POUND) { $packageWeight = Mage::helper('usa')->convertMeasureWeight( $request->getPackageWeight(), $packageParams->getWeightUnits(), - Zend_Measure_Weight::POUND, + Mage_Core_Helper_Measure_Weight::POUND, ); } - if ($packageParams->getDimensionUnits() != Zend_Measure_Length::INCH) { + if ($packageParams->getDimensionUnits() != Mage_Core_Helper_Measure_Length::INCH) { $length = round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getLength(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, )); $width = round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getWidth(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, )); $height = round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getHeight(), $packageParams->getDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, )); } - if ($packageParams->getGirthDimensionUnits() != Zend_Measure_Length::INCH) { + if ($packageParams->getGirthDimensionUnits() != Mage_Core_Helper_Measure_Length::INCH) { $girth = round((float) Mage::helper('usa')->convertMeasureDimension( $packageParams->getGirth(), $packageParams->getGirthDimensionUnits(), - Zend_Measure_Length::INCH, + Mage_Core_Helper_Measure_Length::INCH, )); } @@ -1650,11 +1650,11 @@ protected function _formIntlShipmentRequest(Varien_Object $request) $item->setData($itemShipment); $itemWeight = $item->getWeight() * $item->getQty(); - if ($packageParams->getWeightUnits() != Zend_Measure_Weight::POUND) { + if ($packageParams->getWeightUnits() != Mage_Core_Helper_Measure_Weight::POUND) { $itemWeight = Mage::helper('usa')->convertMeasureWeight( $itemWeight, $packageParams->getWeightUnits(), - Zend_Measure_Weight::POUND, + Mage_Core_Helper_Measure_Weight::POUND, ); } diff --git a/app/design/adminhtml/default/default/template/sales/order/shipment/packaging/popup.phtml b/app/design/adminhtml/default/default/template/sales/order/shipment/packaging/popup.phtml index 520947371a1..4403d3f37d8 100644 --- a/app/design/adminhtml/default/default/template/sales/order/shipment/packaging/popup.phtml +++ b/app/design/adminhtml/default/default/template/sales/order/shipment/packaging/popup.phtml @@ -87,8 +87,8 @@ document.observe("dom:loaded", function() { @@ -108,8 +108,8 @@ document.observe("dom:loaded", function() { @@ -123,8 +123,8 @@ document.observe("dom:loaded", function() { getDeliveryConfirmationTypes()): ?> From 851e06b21162613629bd8050fc0f81d16096d5f1 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 07:59:18 +0100 Subject: [PATCH 3/7] Updated tests --- tests/unit/Mage/Usa/Helper/DataTest.php | 39 ++++++++++--------- .../Mage/Usa/Helper/DataTrait.php | 37 +++++++++++++----- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/tests/unit/Mage/Usa/Helper/DataTest.php b/tests/unit/Mage/Usa/Helper/DataTest.php index 8f99e837925..96eae761eb8 100644 --- a/tests/unit/Mage/Usa/Helper/DataTest.php +++ b/tests/unit/Mage/Usa/Helper/DataTest.php @@ -15,6 +15,7 @@ use Mage_Usa_Helper_Data as Subject; use OpenMage\Tests\Unit\OpenMageTest; use OpenMage\Tests\Unit\Traits\DataProvider\Mage\Usa\Helper\DataTrait; +use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure; final class DataTest extends OpenMageTest { @@ -32,48 +33,50 @@ public static function setUpBeforeClass(): void * @dataProvider provideConvertMeasureWeightData * @group Helper */ - public function testConvertMeasureWeight($expectedResult, $value, $sourceWeightMeasure, $toWeightMeasure): void + public function testConvertMeasureWeight(float $expectedResult, $value, string $sourceWeightMeasure, string $toWeightMeasure): void { $result = self::$subject->convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure); - #self::assertIsNotFloat($result); - self::assertIsString($result); - #self::assertSame($expectedResult, $result); - self::assertStringStartsWith($expectedResult, $result); + + self::assertIsFloat($result); + self::assertSame($expectedResult, $result); } /** * @dataProvider provideConvertMeasureDimensionData * @group Helper */ - public function testConvertMeasureDimension($expectedResult, $value, $sourceWeightMeasure, $toWeightMeasure): void + public function testConvertMeasureDimension(float $expectedResult, $value, string $sourceWeightMeasure, string $toWeightMeasure): void { $result = self::$subject->convertMeasureDimension($value, $sourceWeightMeasure, $toWeightMeasure); - #self::assertIsNotFloat($result); - self::assertIsString($result); - #self::assertSame($expectedResult, $result); - self::assertStringStartsWith($expectedResult, $result); + + self::assertIsFloat($result); + self::assertSame($expectedResult, $result); } /** * @dataProvider provideGetMeasureWeightNameData * @group Helper */ - public function testGetMeasureWeightName($expectedResult, $value): void + public function testGetMeasureWeightName(string $expectedResult, string $eey): void { - $result = self::$subject->getMeasureWeightName($value); - self::assertIsString($result); - self::assertSame($expectedResult, $result); + try { + self::assertSame($expectedResult, self::$subject->getMeasureWeightName($eey)); + } catch (UnknownUnitOfMeasure $unitOfMeasure) { + self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } } /** * @dataProvider provideGetMeasureDimensionNameData * @group Helper */ - public function testGetMeasureDimensionName($expectedResult, $value): void + public function testGetMeasureDimensionName(string $expectedResult, string $key): void { - $result = self::$subject->getMeasureDimensionName($value); - self::assertIsString($result); - self::assertSame($expectedResult, $result); + try { + self::assertSame($expectedResult, self::$subject->getMeasureDimensionName($key)); + } catch (UnknownUnitOfMeasure $unitOfMeasure) { + self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } } /** diff --git a/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php index ae834a322e9..941e8af7211 100644 --- a/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php +++ b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php @@ -11,26 +11,35 @@ namespace OpenMage\Tests\Unit\Traits\DataProvider\Mage\Usa\Helper; use Generator; +use Mage_Core_Helper_Measure_Length; +use Mage_Core_Helper_Measure_Weight; trait DataTrait { public function provideConvertMeasureWeightData(): Generator { yield 'kg to lbs' => [ - '22.046', + 22.046226218487757, 10, - 'KILOGRAM', - 'POUND', + Mage_Core_Helper_Measure_Weight::KILOGRAM, + Mage_Core_Helper_Measure_Weight::POUND, + ]; + + yield 'ounce to lbs' => [ + 0.625, + 10, + Mage_Core_Helper_Measure_Weight::OUNCE, + Mage_Core_Helper_Measure_Weight::POUND, ]; } public function provideConvertMeasureDimensionData(): Generator { yield 'm to cm' => [ - '1000', + 1000, 10, - 'METER', - 'CENTIMETER', + Mage_Core_Helper_Measure_Length::STANDARD, + Mage_Core_Helper_Measure_Length::CENTIMETER, ]; } @@ -38,15 +47,25 @@ public function provideGetMeasureWeightNameData(): Generator { yield 'kg' => [ 'kg', - 'KILOGRAM', + Mage_Core_Helper_Measure_Weight::KILOGRAM, + ]; + + yield 'exception' => [ + 'Unknown unit of measure (xyz).', + 'xyz', ]; } public function provideGetMeasureDimensionNameData(): Generator { - yield 'm to cm' => [ + yield 'm' => [ 'm', - 'METER', + Mage_Core_Helper_Measure_Length::STANDARD, + ]; + + yield 'exception' => [ + 'Unknown unit of measure (xyz).', + 'xyz', ]; } From 558601671df7c7650af4acffc756da9618407ee2 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 08:52:20 +0100 Subject: [PATCH 4/7] Replace `Zend_Measure` with `php-units-of-measure` - see https://github.com/PhpUnitsOfMeasure/php-units-of-measure --- composer.json | 3 ++- composer.lock | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b89986e0e9f..4ebe32f5078 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ "nnnick/chartjs": "^4.4", "openmage/composer-plugin": "^3.1", "pelago/emogrifier": "^8.0", + "php-units-of-measure/php-units-of-measure": "^2.2", "phpseclib/mcrypt_compat": "^2.0.3", "phpseclib/phpseclib": "^3.0.14", "shardj/zf1-future": "^1.24.1", @@ -196,4 +197,4 @@ "vendor:patch": "Create patch file", "test": "Run php-cs-fixer, phpstan & phpunit" } -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index d9a0353158b..e3bcc4f4b16 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "aa6c7df91ba39052ec1ddfdaaedf324e", + "content-hash": "51adf0e82caf127ffda9e8b4c7999705", "packages": [ { "name": "carbonphp/carbon-doctrine-types", @@ -1631,6 +1631,59 @@ }, "time": "2025-09-15T12:53:23+00:00" }, + { + "name": "php-units-of-measure/php-units-of-measure", + "version": "v2.2.0", + "source": { + "type": "git", + "url": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure.git", + "reference": "cf1b83b1ef7615dd53f70864c9e7f496b1ec0fd1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PhpUnitsOfMeasure/php-units-of-measure/zipball/cf1b83b1ef7615dd53f70864c9e7f496b1ec0fd1", + "reference": "cf1b83b1ef7615dd53f70864c9e7f496b1ec0fd1", + "shasum": "" + }, + "require": { + "php": ">=7.4" + }, + "replace": { + "triplepoint/php-units-of-measure": "*" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpUnitsOfMeasure\\": "source/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Hanson", + "role": "Developer" + } + ], + "description": "A PHP library for converting between standard units of measure.", + "homepage": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure", + "keywords": [ + "conversion", + "data", + "measurements" + ], + "support": { + "issues": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure/issues", + "source": "https://github.com/PhpUnitsOfMeasure/php-units-of-measure" + }, + "time": "2025-04-30T23:02:43+00:00" + }, { "name": "phpseclib/mcrypt_compat", "version": "2.0.6", From f6662515d0c82f15fad4f042849e2df8f9183a9d Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 09:00:04 +0100 Subject: [PATCH 5/7] tests --- .phpstan.dist.baseline.neon | 12 ++++---- app/code/core/Mage/Usa/Helper/Data.php | 14 +++++++--- tests/unit/Mage/Usa/Helper/DataTest.php | 28 +++++++++++-------- .../Mage/Usa/Helper/DataTrait.php | 21 ++++++++++++++ 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/.phpstan.dist.baseline.neon b/.phpstan.dist.baseline.neon index 1610621911c..b7f8dc3bd19 100644 --- a/.phpstan.dist.baseline.neon +++ b/.phpstan.dist.baseline.neon @@ -3096,12 +3096,6 @@ parameters: count: 1 path: app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php - - - rawMessage: 'Parameter #1 $value of method Mage_Usa_Helper_Data::convertMeasureDimension() expects int|string, float given.' - identifier: argument.type - count: 1 - path: app/code/core/Mage/Usa/Model/Shipping/Carrier/Dhl/International.php - - rawMessage: Access to an undefined property Mage_Usa_Model_Shipping_Carrier_Fedex::$_defaultGatewayUrl. identifier: property.notFound @@ -4139,3 +4133,9 @@ parameters: identifier: argument.type count: 1 path: lib/Varien/Simplexml/Element.php + + - + rawMessage: Dead catch - PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure is never thrown in the try block. + identifier: catch.neverThrown + count: 2 + path: tests/unit/Mage/Usa/Helper/DataTest.php diff --git a/app/code/core/Mage/Usa/Helper/Data.php b/app/code/core/Mage/Usa/Helper/Data.php index 90cef458abf..c8b8fbe8e7d 100644 --- a/app/code/core/Mage/Usa/Helper/Data.php +++ b/app/code/core/Mage/Usa/Helper/Data.php @@ -7,6 +7,8 @@ * @package Mage_Usa */ +use PhpUnitsOfMeasure\Exception\NonNumericValue; +use PhpUnitsOfMeasure\Exception\NonStringUnitName; use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure; use PhpUnitsOfMeasure\PhysicalQuantity\Mass; use PhpUnitsOfMeasure\PhysicalQuantity\Length; @@ -22,9 +24,11 @@ class Mage_Usa_Helper_Data extends Mage_Core_Helper_Abstract * Convert weight in different measure types * * @param float $value - * @param string $sourceWeightMeasure - * @param string $toWeightMeasure + * @param Mage_Core_Helper_Measure_Weight::* $sourceWeightMeasure + * @param Mage_Core_Helper_Measure_Weight::* $toWeightMeasure * @return null|float + * @throws NonNumericValue + * @throws NonStringUnitName */ public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure) { @@ -40,9 +44,11 @@ public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeas * Convert dimensions in different measure types * * @param float $value - * @param string $sourceDimensionMeasure - * @param string $toDimensionMeasure + * @param Mage_Core_Helper_Measure_Length::* $sourceDimensionMeasure + * @param Mage_Core_Helper_Measure_Length::* $toDimensionMeasure * @return null|float + * @throws NonNumericValue + * @throws NonStringUnitName */ public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDimensionMeasure) { diff --git a/tests/unit/Mage/Usa/Helper/DataTest.php b/tests/unit/Mage/Usa/Helper/DataTest.php index 96eae761eb8..464fdf71d04 100644 --- a/tests/unit/Mage/Usa/Helper/DataTest.php +++ b/tests/unit/Mage/Usa/Helper/DataTest.php @@ -15,6 +15,8 @@ use Mage_Usa_Helper_Data as Subject; use OpenMage\Tests\Unit\OpenMageTest; use OpenMage\Tests\Unit\Traits\DataProvider\Mage\Usa\Helper\DataTrait; +use PhpUnitsOfMeasure\Exception\NonNumericValue; +use PhpUnitsOfMeasure\Exception\NonStringUnitName; use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure; final class DataTest extends OpenMageTest @@ -33,34 +35,36 @@ public static function setUpBeforeClass(): void * @dataProvider provideConvertMeasureWeightData * @group Helper */ - public function testConvertMeasureWeight(float $expectedResult, $value, string $sourceWeightMeasure, string $toWeightMeasure): void + public function testConvertMeasureWeight(float|string $expectedResult, $value, ?string $sourceWeightMeasure, ?string $toWeightMeasure): void { - $result = self::$subject->convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure); - - self::assertIsFloat($result); - self::assertSame($expectedResult, $result); + try { + self::assertSame($expectedResult, self::$subject->convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure)); + } catch (NonNumericValue|NonStringUnitName|UnknownUnitOfMeasure $unitOfMeasure) { + self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } } /** * @dataProvider provideConvertMeasureDimensionData * @group Helper */ - public function testConvertMeasureDimension(float $expectedResult, $value, string $sourceWeightMeasure, string $toWeightMeasure): void + public function testConvertMeasureDimension(float|string $expectedResult, $value, ?string $sourceWeightMeasure, ?string $toWeightMeasure): void { - $result = self::$subject->convertMeasureDimension($value, $sourceWeightMeasure, $toWeightMeasure); - - self::assertIsFloat($result); - self::assertSame($expectedResult, $result); + try { + self::assertSame($expectedResult, self::$subject->convertMeasureDimension($value, $sourceWeightMeasure, $toWeightMeasure)); + } catch (NonNumericValue|NonStringUnitName|UnknownUnitOfMeasure $unitOfMeasure) { + self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } } /** * @dataProvider provideGetMeasureWeightNameData * @group Helper */ - public function testGetMeasureWeightName(string $expectedResult, string $eey): void + public function testGetMeasureWeightName(string $expectedResult, string $key): void { try { - self::assertSame($expectedResult, self::$subject->getMeasureWeightName($eey)); + self::assertSame($expectedResult, self::$subject->getMeasureWeightName($key)); } catch (UnknownUnitOfMeasure $unitOfMeasure) { self::assertSame($expectedResult, $unitOfMeasure->getMessage()); } diff --git a/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php index 941e8af7211..ecc211185d7 100644 --- a/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php +++ b/tests/unit/Traits/DataProvider/Mage/Usa/Helper/DataTrait.php @@ -31,6 +31,20 @@ public function provideConvertMeasureWeightData(): Generator Mage_Core_Helper_Measure_Weight::OUNCE, Mage_Core_Helper_Measure_Weight::POUND, ]; + + yield 'non-numeric value' => [ + 'Value (xyz) must be numeric.', + 'xyz', + Mage_Core_Helper_Measure_Weight::OUNCE, + Mage_Core_Helper_Measure_Weight::POUND, + ]; + + yield 'non-string unit' => [ + 'Unit name or alias () must be a string value.', + 10, + Mage_Core_Helper_Measure_Weight::OUNCE, + null, + ]; } public function provideConvertMeasureDimensionData(): Generator @@ -41,6 +55,13 @@ public function provideConvertMeasureDimensionData(): Generator Mage_Core_Helper_Measure_Length::STANDARD, Mage_Core_Helper_Measure_Length::CENTIMETER, ]; + + yield 'invalid to cm' => [ + 'Unknown unit of measure (xyz).', + 10, + 'xyz', + Mage_Core_Helper_Measure_Length::CENTIMETER, + ]; } public function provideGetMeasureWeightNameData(): Generator From 3417c711088ae9cc8b66bddf9bb2582f603d9680 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 15 Nov 2025 09:37:46 +0100 Subject: [PATCH 6/7] rector --- tests/unit/Mage/Usa/Helper/DataTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/Mage/Usa/Helper/DataTest.php b/tests/unit/Mage/Usa/Helper/DataTest.php index 464fdf71d04..37235f35278 100644 --- a/tests/unit/Mage/Usa/Helper/DataTest.php +++ b/tests/unit/Mage/Usa/Helper/DataTest.php @@ -65,8 +65,8 @@ public function testGetMeasureWeightName(string $expectedResult, string $key): v { try { self::assertSame($expectedResult, self::$subject->getMeasureWeightName($key)); - } catch (UnknownUnitOfMeasure $unitOfMeasure) { - self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } catch (UnknownUnitOfMeasure $unknownUnitOfMeasure) { + self::assertSame($expectedResult, $unknownUnitOfMeasure->getMessage()); } } @@ -78,8 +78,8 @@ public function testGetMeasureDimensionName(string $expectedResult, string $key) { try { self::assertSame($expectedResult, self::$subject->getMeasureDimensionName($key)); - } catch (UnknownUnitOfMeasure $unitOfMeasure) { - self::assertSame($expectedResult, $unitOfMeasure->getMessage()); + } catch (UnknownUnitOfMeasure $unknownUnitOfMeasure) { + self::assertSame($expectedResult, $unknownUnitOfMeasure->getMessage()); } } From c66f695dffc506b87bc4d1cb9c9f87b709a85335 Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sun, 16 Nov 2025 19:30:09 +0100 Subject: [PATCH 7/7] baseline --- .phpstan.dist.baseline.neon | 82 +------------------------------------ 1 file changed, 2 insertions(+), 80 deletions(-) diff --git a/.phpstan.dist.baseline.neon b/.phpstan.dist.baseline.neon index 2d8328987a3..6fe9bc81531 100644 --- a/.phpstan.dist.baseline.neon +++ b/.phpstan.dist.baseline.neon @@ -2778,54 +2778,6 @@ parameters: count: 1 path: app/code/core/Mage/Tax/Model/Resource/Setup.php - - - rawMessage: 'Call to method toUnit() on an unknown class PhpUnitsOfMeasure\PhysicalQuantity\Length.' - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: 'Call to method toUnit() on an unknown class PhpUnitsOfMeasure\PhysicalQuantity\Mass.' - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: 'Call to static method getUnit() on an unknown class PhpUnitsOfMeasure\PhysicalQuantity\Length.' - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: 'Call to static method getUnit() on an unknown class PhpUnitsOfMeasure\PhysicalQuantity\Mass.' - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: Instantiated class PhpUnitsOfMeasure\PhysicalQuantity\Length not found. - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: Instantiated class PhpUnitsOfMeasure\PhysicalQuantity\Mass not found. - identifier: class.notFound - count: 1 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: PHPDoc tag @throws with type PhpUnitsOfMeasure\Exception\NonNumericValue|PhpUnitsOfMeasure\Exception\NonStringUnitName is not subtype of Throwable - identifier: throws.notThrowable - count: 2 - path: app/code/core/Mage/Usa/Helper/Data.php - - - - rawMessage: PHPDoc tag @throws with type PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure is not subtype of Throwable - identifier: throws.notThrowable - count: 2 - path: app/code/core/Mage/Usa/Helper/Data.php - - rawMessage: 'Property Mage_Usa_Model_Shipping_Carrier_Dhl::$_result (Mage_Shipping_Model_Rate_Result|null) does not accept Mage_Shipping_Model_Tracking_Result.' identifier: assign.propertyType @@ -3529,37 +3481,7 @@ parameters: path: lib/Varien/Simplexml/Element.php - - rawMessage: 'Call to method getMessage() on an unknown class PhpUnitsOfMeasure\Exception\NonNumericValue.' - identifier: class.notFound - count: 2 - path: tests/unit/Mage/Usa/Helper/DataTest.php - - - - rawMessage: 'Call to method getMessage() on an unknown class PhpUnitsOfMeasure\Exception\NonStringUnitName.' - identifier: class.notFound - count: 2 - path: tests/unit/Mage/Usa/Helper/DataTest.php - - - - rawMessage: 'Call to method getMessage() on an unknown class PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure.' - identifier: class.notFound - count: 4 - path: tests/unit/Mage/Usa/Helper/DataTest.php - - - - rawMessage: Caught class PhpUnitsOfMeasure\Exception\NonNumericValue not found. - identifier: class.notFound - count: 2 - path: tests/unit/Mage/Usa/Helper/DataTest.php - - - - rawMessage: Caught class PhpUnitsOfMeasure\Exception\NonStringUnitName not found. - identifier: class.notFound + rawMessage: Dead catch - PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure is never thrown in the try block. + identifier: catch.neverThrown count: 2 path: tests/unit/Mage/Usa/Helper/DataTest.php - - - - rawMessage: Caught class PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure not found. - identifier: class.notFound - count: 4 - path: tests/unit/Mage/Usa/Helper/DataTest.php