From 5308d5665afe296b7eff0a5cbf73d56fd313867d Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Tue, 26 Sep 2017 19:00:59 +0200 Subject: [PATCH 01/55] Added storage types --- source/PhysicalQuantity/Storage.php | 52 ++++++++++++++++++++++++++ tests/PhysicalQuantity/StorageTest.php | 49 ++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 source/PhysicalQuantity/Storage.php create mode 100644 tests/PhysicalQuantity/StorageTest.php diff --git a/source/PhysicalQuantity/Storage.php b/source/PhysicalQuantity/Storage.php new file mode 100644 index 0000000..49a5428 --- /dev/null +++ b/source/PhysicalQuantity/Storage.php @@ -0,0 +1,52 @@ +addAlias('B'); + static::addUnit($unit); + + // Bit + $unit = UnitOfMeasure::linearUnitFactory('bit', 0.125); + $unit->addAlias('b'); + static::addUnit($unit); + + // kilobyte (KB) + $unit = UnitOfMeasure::linearUnitFactory('kilobyte', 1024); + $unit->addAlias('KB'); + static::addUnit($unit); + + // megabyte (MB) + $unit = UnitOfMeasure::linearUnitFactory('megabyte', 1048576); + $unit->addAlias('MB'); + static::addUnit($unit); + + // gigabyte (GB) + $unit = UnitOfMeasure::linearUnitFactory('gigabyte', 1073741824); + $unit->addAlias('GB'); + static::addUnit($unit); + + // terabyte (TB) + $unit = UnitOfMeasure::linearUnitFactory('terabyte', 1099511627776); + $unit->addAlias('TB'); + static::addUnit($unit); + + // petabyte (PB) + $unit = UnitOfMeasure::linearUnitFactory('petabyte', 1125899906842624); + $unit->addAlias('PB'); + static::addUnit($unit); + + } + +} diff --git a/tests/PhysicalQuantity/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php new file mode 100644 index 0000000..55663c2 --- /dev/null +++ b/tests/PhysicalQuantity/StorageTest.php @@ -0,0 +1,49 @@ +assertEquals(1, (new Storage(1, 'byte'))->toUnit('B')); + } + + public function testByteToBit() + { + $this->assertEquals(40, (new Storage(5, 'byte'))->toUnit('bit')); + } + + public function testBitToByte() + { + $this->assertEquals(1.25, (new Storage(10, 'b'))->toUnit('B')); + } + + public function testMegabyteToByte() + { + $this->assertEquals(1048576, (new Storage(1, 'MB'))->toUnit('B')); + } + + public function testGigabyteToByte() + { + $this->assertEquals(1073741824, (new Storage(1, 'gigabyte'))->toUnit('byte')); + } + + public function testTerabyteToByte() + { + $this->assertEquals(1099511627776, (new Storage(1, 'TB'))->toUnit('byte')); + } + + public function testPetabyteToByte() + { + $this->assertEquals(3377699720527872, (new Storage(3, 'PB'))->toUnit('byte')); + } + +} \ No newline at end of file From 5ece8064f5e6e0c2ea713906deced5b9b8f7ff0d Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Tue, 26 Sep 2017 19:11:11 +0200 Subject: [PATCH 02/55] coding standard fix --- source/PhysicalQuantity/Storage.php | 1 - tests/PhysicalQuantity/StorageTest.php | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/source/PhysicalQuantity/Storage.php b/source/PhysicalQuantity/Storage.php index 49a5428..7543354 100644 --- a/source/PhysicalQuantity/Storage.php +++ b/source/PhysicalQuantity/Storage.php @@ -48,5 +48,4 @@ protected static function initialize() static::addUnit($unit); } - } diff --git a/tests/PhysicalQuantity/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php index 55663c2..3f52cba 100644 --- a/tests/PhysicalQuantity/StorageTest.php +++ b/tests/PhysicalQuantity/StorageTest.php @@ -45,5 +45,4 @@ public function testPetabyteToByte() { $this->assertEquals(3377699720527872, (new Storage(3, 'PB'))->toUnit('byte')); } - -} \ No newline at end of file +} From 3a2bf303a7dd3e2b555935c8483fe65b8787213f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Fejfar?= Date: Mon, 30 Oct 2017 17:26:20 +0100 Subject: [PATCH 03/55] Use composer scripts --- .travis.yml | 4 ++-- README.md | 4 ++-- composer.json | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3e3b711..07cf270 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,8 @@ before_script: - composer install --verbose --prefer-dist --no-interaction -o script: - - ./vendor/bin/phpunit -c ./tests/phpunit.xml.dist ./tests - - ./vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./ + - composer test + - composer phpcs notifications: email: false diff --git a/README.md b/README.md index 2a7e6b1..af7f565 100644 --- a/README.md +++ b/README.md @@ -307,14 +307,14 @@ composer.phar update --verbose --prefer-dist All the tests associated with this project can be manually run with: ``` bash -vendor/bin/phpunit -c ./tests/phpunit.xml.dist ./tests +composer test ``` ### CodeSniffer Codesniffer verifies that coding standards are being met. Once the project is built with development dependencies, you can run the checks with: ``` bash -vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./ +composer phpcs ``` ### Continuous Integration diff --git a/composer.json b/composer.json index 93d14a7..e1f05de 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,11 @@ "replace": { "triplepoint/php-units-of-measure": "*" }, + + "scripts": { + "test": "phpunit -c ./tests/phpunit.xml.dist ./tests", + "phpcs": "phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./" + }, "autoload": { "psr-4": { From 5100de12c42464f6557c38a50e4bc02677efb7ec Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 18 Apr 2019 17:38:13 +0300 Subject: [PATCH 04/55] Added ly (light year) and pc (parsec) length units. --- source/PhysicalQuantity/Length.php | 23 +++++++ tests/PhysicalQuantity/LengthTest.php | 86 +++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/source/PhysicalQuantity/Length.php b/source/PhysicalQuantity/Length.php index 1d52f89..da67dfa 100644 --- a/source/PhysicalQuantity/Length.php +++ b/source/PhysicalQuantity/Length.php @@ -75,5 +75,28 @@ protected static function initialize() $newUnit->addAlias('astronomical unit'); $newUnit->addAlias('astronomical units'); static::addUnit($newUnit); + + // Light Year + $newUnit = UnitOfMeasure::linearUnitFactory('ly', 9460730472580800); + $newUnit->addAlias('LY'); + $newUnit->addAlias('light year'); + $newUnit->addAlias('light years'); + static::addUnit($newUnit); + + // Light Year + $parsec = UnitOfMeasure::linearUnitFactory('pc', 3.085677581491367279E+16); // parsec == au * 648000 / pi (30856775814913672.789139379577965) + $parsec->addAlias('parsec'); + $parsec->addAlias('parsecs'); + static::addUnit($parsec); + + static::addMissingSIPrefixedUnits( + $parsec, + 1, + '%ppc', + [ + '%Pparsec', + '%Pparsecs' + ] + ); } } diff --git a/tests/PhysicalQuantity/LengthTest.php b/tests/PhysicalQuantity/LengthTest.php index 27b27f0..8ad854f 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -133,6 +133,74 @@ class LengthTest extends AbstractPhysicalQuantityTestCase 'au', 'astronomical unit', 'astronomical units', + 'ly', + 'LY', + 'light year', + 'light years', + 'pc', + 'parsec', + 'parsecs', + 'Ypc', + 'yottaparsec', + 'yottaparsecs', + 'Zpc', + 'zettaparsec', + 'zettaparsecs', + 'Epc', + 'exaparsec', + 'exaparsecs', + 'Ppc', + 'petaparsec', + 'petaparsecs', + 'Tpc', + 'teraparsec', + 'teraparsecs', + 'Gpc', + 'gigaparsec', + 'gigaparsecs', + 'Mpc', + 'megaparsec', + 'megaparsecs', + 'kpc', + 'kiloparsec', + 'kiloparsecs', + 'hpc', + 'hectoparsec', + 'hectoparsecs', + 'dapc', + 'decaparsec', + 'decaparsecs', + 'dpc', + 'deciparsec', + 'deciparsecs', + 'cpc', + 'centiparsec', + 'centiparsecs', + 'mpc', + 'milliparsec', + 'milliparsecs', + 'µpc', + 'microparsec', + 'microparsecs', + 'npc', + 'nanoparsec', + 'nanoparsecs', + 'ppc', + 'picoparsec', + 'picoparsecs', + 'fpc', + 'femtoparsec', + 'femtoparsecs', + 'apc', + 'attoparsec', + 'attoparsecs', + 'zpc', + 'zeptoparsec', + 'zeptoparsecs', + 'ypc', + 'yoctoparsec', + 'yoctoparsecs', + ]; protected function instantiateTestQuantity() @@ -175,4 +243,22 @@ public function testToAstronomicalUnit() $quantity = new Length(150000000, 'km'); $this->assertEquals(1.0026880683402668, $quantity->toUnit('AU')); } + + public function testToLightYear() + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(15.855012510369232, $quantity->toUnit('ly')); + } + + public function testToParSec() + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(4.861168934166548, $quantity->toUnit('pc')); + } + + public function testToMilliParSec() + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(4861.168934166548, $quantity->toUnit('mpc')); + } } From ca6ad0bab14b0829d4c00e303110b484e4d1a69c Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 18 Apr 2019 18:41:24 +0300 Subject: [PATCH 05/55] Added power factor for Si prefixes, allowed use prefixes on squire, cubic, etc --- CHANGELOG.md | 7 ++++++- source/HasSIUnitsTrait.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86baa65..38fbc7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v2.2.0 (April XXth, 2019) +- Added option power factor for Si factor, ie for square (2) and cubic (3) +- Added Si prefixes for area unit m^2 +- Additional units for length: light year, parsec + + ## v2.1.0 (July 24th, 2016) - Added getUnitDefinitions() method to PhysicalQuantity classes, to get a raw list of UnitofMeasure objects defined on that quantity - Added UPPERCASE templating support for autogenerated metric units to support names like MEGA_METERS, for example. @@ -7,7 +13,6 @@ - Additional units for time: decade, century, and millenium - Additional units for volume: teaspoon, tablespoon, gallon, quart, fluid ounce, and pint - ## v2.0.1 (December 12th, 2015) - Migrated package from triplepoint/php-units-of-measure to php-units-of-measure/php-units-of-measure. - New physical quantity: Energy diff --git a/source/HasSIUnitsTrait.php b/source/HasSIUnitsTrait.php index 6ec10f4..fdeac69 100644 --- a/source/HasSIUnitsTrait.php +++ b/source/HasSIUnitsTrait.php @@ -36,12 +36,14 @@ trait HasSIUnitsTrait * @param integer $toBaseSiUnitFactor The power-of-ten factor that converts the given SI unit into the not-prefixed SI base unit (ie 1e-3 for kilograms) * @param string $namePattern The pattern to apply to the base unit's name to generate a new SI unit name * @param array $aliasPatterns The collection of alias patterns to use in generating a new SI unit's aliases + * @param integer $powerFactor Use power factor for squares, qubic and other multiplication of SI factor (ie. square is 2, qubic is 3) */ protected static function addMissingSIPrefixedUnits( UnitOfMeasure $siUnit, $toBaseSiUnitFactor, $namePattern, - array $aliasPatterns = [] + array $aliasPatterns = [], + $powerFactor = NULL ) { /** * The standard set of SI prefixes @@ -176,6 +178,9 @@ protected static function addMissingSIPrefixedUnits( // Determine the factor that converts the new unit into the physical quantity's // native unit of measure. + if (is_int($powerFactor) && $powerFactor != 0) { + $prefixDefinition['factor'] = pow($prefixDefinition['factor'], $powerFactor); + } $toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor']; // Instantiate the new unit of measure From 808ac69e2078bad4324ab58690aac2befc80dc0d Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 18 Apr 2019 18:42:23 +0300 Subject: [PATCH 06/55] Use Si prefixes for area units --- source/PhysicalQuantity/Area.php | 61 ++++------- tests/PhysicalQuantity/AreaTest.php | 152 ++++++++++++++++++++++++---- 2 files changed, 152 insertions(+), 61 deletions(-) diff --git a/source/PhysicalQuantity/Area.php b/source/PhysicalQuantity/Area.php index dfccd24..101ece0 100644 --- a/source/PhysicalQuantity/Area.php +++ b/source/PhysicalQuantity/Area.php @@ -2,10 +2,13 @@ namespace PhpUnitsOfMeasure\PhysicalQuantity; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; +use PhpUnitsOfMeasure\HasSIUnitsTrait; use PhpUnitsOfMeasure\UnitOfMeasure; class Area extends AbstractPhysicalQuantity { + use HasSIUnitsTrait; + protected static $unitDefinitions; protected static function initialize() @@ -21,49 +24,21 @@ protected static function initialize() $metersquared->addAlias('metres squared'); static::addUnit($metersquared); - // Millimeter squared - $newUnit = UnitOfMeasure::linearUnitFactory('mm^2', 1e-6); - $newUnit->addAlias('mm²'); - $newUnit->addAlias('millimeter squared'); - $newUnit->addAlias('square millimeter'); - $newUnit->addAlias('square millimeters'); - $newUnit->addAlias('millimeters squared'); - $newUnit->addAlias('millimetre squared'); - $newUnit->addAlias('millimetres squared'); - static::addUnit($newUnit); - - // Centimeter squared - $newUnit = UnitOfMeasure::linearUnitFactory('cm^2', 1e-4); - $newUnit->addAlias('cm²'); - $newUnit->addAlias('centimeter squared'); - $newUnit->addAlias('square centimeter'); - $newUnit->addAlias('square centimeters'); - $newUnit->addAlias('centimeters squared'); - $newUnit->addAlias('centimetre squared'); - $newUnit->addAlias('centimetres squared'); - static::addUnit($newUnit); - - // Decimeter squared - $newUnit = UnitOfMeasure::linearUnitFactory('dm^2', 1e-2); - $newUnit->addAlias('dm²'); - $newUnit->addAlias('decimeter squared'); - $newUnit->addAlias('square decimeters'); - $newUnit->addAlias('square decimeter'); - $newUnit->addAlias('decimeters squared'); - $newUnit->addAlias('decimetre squared'); - $newUnit->addAlias('decimetres squared'); - static::addUnit($newUnit); - - // Kilometer squared - $newUnit = UnitOfMeasure::linearUnitFactory('km^2', 1e6); - $newUnit->addAlias('km²'); - $newUnit->addAlias('kilometer squared'); - $newUnit->addAlias('kilometers squared'); - $newUnit->addAlias('square kilometer'); - $newUnit->addAlias('square kilometers'); - $newUnit->addAlias('kilometre squared'); - $newUnit->addAlias('kilometres squared'); - static::addUnit($newUnit); + static::addMissingSIPrefixedUnits( + $metersquared, + 1, + '%pm^2', + [ + '%pm²', + '%Pmeter squared', + 'square %Pmeter', + 'square %Pmeters', + '%Pmeters squared', + '%Pmetre squared', + '%Pmetres squared' + ], + 2 // square power factor + ); // Foot squared $newUnit = UnitOfMeasure::linearUnitFactory('ft^2', 9.290304e-2); diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 2647184..782c8f0 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -13,30 +13,130 @@ class AreaTest extends AbstractPhysicalQuantityTestCase 'meters squared', 'metre squared', 'metres squared', - 'mm^2', - 'mm²', - 'millimeter squared', - 'millimeters squared', - 'millimetre squared', - 'millimetres squared', - 'cm^2', - 'cm²', - 'centimeter squared', - 'centimeters squared', - 'centimetre squared', - 'centimetres squared', - 'dm^2', - 'dm²', - 'decimeter squared', - 'decimeters squared', - 'decimetre squared', - 'decimetres squared', + + 'Ym^2', + 'Ym²', + 'yottameter squared', + 'yottameters squared', + 'yottametre squared', + 'yottametres squared', + 'Zm^2', + 'Zm²', + 'zettameter squared', + 'zettameters squared', + 'zettametre squared', + 'zettametres squared', + 'Em^2', + 'Em²', + 'exameter squared', + 'exameters squared', + 'exametre squared', + 'exametres squared', + 'Pm^2', + 'Pm²', + 'petameter squared', + 'petameters squared', + 'petametre squared', + 'petametres squared', + 'Tm^2', + 'Tm²', + 'terameter squared', + 'terameters squared', + 'terametre squared', + 'terametres squared', + 'Gm^2', + 'Gm²', + 'gigameter squared', + 'gigameters squared', + 'gigametre squared', + 'gigametres squared', + 'Mm^2', + 'Mm²', + 'megameter squared', + 'megameters squared', + 'megametre squared', + 'megametres squared', 'km^2', 'km²', 'kilometer squared', 'kilometers squared', 'kilometre squared', 'kilometres squared', + + 'hm^2', + 'hm²', + 'hectometer squared', + 'hectometers squared', + 'hectometre squared', + 'hectometres squared', + 'dam^2', + 'dam²', + 'decameter squared', + 'decameters squared', + 'decametre squared', + 'decametres squared', + 'dm^2', + 'dm²', + 'decimeter squared', + 'decimeters squared', + 'decimetre squared', + 'decimetres squared', + 'cm^2', + 'cm²', + 'centimeter squared', + 'centimeters squared', + 'centimetre squared', + 'centimetres squared', + 'mm^2', + 'mm²', + 'millimeter squared', + 'millimeters squared', + 'millimetre squared', + 'millimetres squared', + + 'µm^2', + 'µm²', + 'micrometer squared', + 'micrometers squared', + 'micrometre squared', + 'micrometres squared', + 'nm^2', + 'nm²', + 'nanometer squared', + 'nanometers squared', + 'nanometre squared', + 'nanometres squared', + 'pm^2', + 'pm²', + 'picometer squared', + 'picometers squared', + 'picometre squared', + 'picometres squared', + 'fm^2', + 'fm²', + 'femtometer squared', + 'femtometers squared', + 'femtometre squared', + 'femtometres squared', + 'am^2', + 'am²', + 'attometer squared', + 'attometers squared', + 'attometre squared', + 'attometres squared', + 'zm^2', + 'zm²', + 'zeptometer squared', + 'zeptometers squared', + 'zeptometre squared', + 'zeptometres squared', + 'ym^2', + 'ym²', + 'yoctometer squared', + 'yoctometers squared', + 'yoctometre squared', + 'yoctometres squared', + 'ft^2', 'ft²', 'foot squared', @@ -97,4 +197,20 @@ public function testToDecare() $area = new Area(1000, 'm^2'); $this->assertEquals(1, $area->toUnit('decare')); } + + public function testToDecimeterSquare() + { + $area = new Area(1, 'm^2'); + $this->assertEquals(100, $area->toUnit('dm^2')); + $area = new Area(100, 'm^2'); + $this->assertEquals(10000, $area->toUnit('dm^2')); + } + + public function testToMillimeterSquare() + { + $area = new Area(1, 'm^2'); + $this->assertEquals(1e6, $area->toUnit('mm^2')); + $area = new Area(100, 'm^2'); + $this->assertEquals(1e8, $area->toUnit('mm^2')); + } } From 4579f2b65a60c522dde47e371447ed183d8d68d6 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 18 Apr 2019 19:14:56 +0300 Subject: [PATCH 07/55] Added energy units electronvolt, calorie --- CHANGELOG.md | 1 + source/PhysicalQuantity/Energy.php | 32 +++++++++++++++++++++++++++ tests/PhysicalQuantity/EnergyTest.php | 26 ++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38fbc7e..7455923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## v2.2.0 (April XXth, 2019) - Added option power factor for Si factor, ie for square (2) and cubic (3) - Added Si prefixes for area unit m^2 +- Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec diff --git a/source/PhysicalQuantity/Energy.php b/source/PhysicalQuantity/Energy.php index 4eb8706..a39da06 100644 --- a/source/PhysicalQuantity/Energy.php +++ b/source/PhysicalQuantity/Energy.php @@ -29,6 +29,22 @@ protected static function initialize() ] ); + // Electronvolt + $ev = UnitOfMeasure::linearUnitFactory('eV', 1.6021766208e-19); + $ev->addAlias('electronvolt'); + $ev->addAlias('electronvolts'); + static::addUnit($ev); + + static::addMissingSIPrefixedUnits( + $ev, + 1, + '%peV', + [ + '%Pelectronvolt', + '%Pelectronvolts', + ] + ); + // Watt hour $wattHour = UnitOfMeasure::linearUnitFactory('Wh', 3600); $wattHour->addAlias('watt hour'); @@ -44,5 +60,21 @@ protected static function initialize() '%Pwatt hours', ] ); + + // Calorie + $calorie = UnitOfMeasure::linearUnitFactory('cal', 4.184); + $calorie->addAlias('calorie'); + $calorie->addAlias('calories'); + static::addUnit($calorie); + + static::addMissingSIPrefixedUnits( + $calorie, + 1, + '%pcal', + [ + '%Pcalorie', + '%Pcalories', + ] + ); } } diff --git a/tests/PhysicalQuantity/EnergyTest.php b/tests/PhysicalQuantity/EnergyTest.php index d1f8a47..c666f9a 100644 --- a/tests/PhysicalQuantity/EnergyTest.php +++ b/tests/PhysicalQuantity/EnergyTest.php @@ -56,6 +56,20 @@ class EnergyTest extends AbstractPhysicalQuantityTestCase 'PJ', 'petajoule', 'petajoules', + + 'eV', + 'electronvolt', + 'electronvolts', + 'MeV', + 'megaelectronvolt', + 'megaelectronvolts', + + 'cal', + 'calorie', + 'calories', + 'Mcal', + 'megacalorie', + 'megacalories', ]; protected function instantiateTestQuantity() @@ -86,4 +100,16 @@ public function testToJoule() $quantity = new Energy(1, 'Wh'); $this->assertEquals(3600, $quantity->toUnit('joule')); } + + public function testToMegaCal() + { + $quantity = new Energy(1, 'kWh'); + $this->assertEquals(0.8604206500956023, $quantity->toUnit('Mcal')); + } + + public function testToMegaElectronvolt() + { + $quantity = new Energy(1, 'kWh'); + $this->assertEquals(2.2469432853179728e19, $quantity->toUnit('MeV')); + } } From a44609025ff2081168719edc4b42ac7b6c1b3cae Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 00:14:58 +0300 Subject: [PATCH 08/55] Added Si prefixes for volume unit cubic meter. --- CHANGELOG.md | 3 +- source/PhysicalQuantity/Volume.php | 54 ++++++++++----------------- tests/PhysicalQuantity/VolumeTest.php | 32 ++++++++++++++++ 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7455923..a777c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## v2.2.0 (April XXth, 2019) - Added option power factor for Si factor, ie for square (2) and cubic (3) -- Added Si prefixes for area unit m^2 +- Added Si prefixes for area unit square meter (m^2) +- Added Si prefixes for volume unit cubic meter (m^3) - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index c679ea3..67bb851 100644 --- a/source/PhysicalQuantity/Volume.php +++ b/source/PhysicalQuantity/Volume.php @@ -2,10 +2,13 @@ namespace PhpUnitsOfMeasure\PhysicalQuantity; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; +use PhpUnitsOfMeasure\HasSIUnitsTrait; use PhpUnitsOfMeasure\UnitOfMeasure; class Volume extends AbstractPhysicalQuantity { + use HasSIUnitsTrait; + protected static $unitDefinitions; protected static function initialize() @@ -19,41 +22,19 @@ protected static function initialize() $cubicmeter->addAlias('cubic metres'); static::addUnit($cubicmeter); - // Cubic millimeter - $newUnit = UnitOfMeasure::linearUnitFactory('mm^3', 1e-9); - $newUnit->addAlias('mm³'); - $newUnit->addAlias('cubic millimeter'); - $newUnit->addAlias('cubic millimeters'); - $newUnit->addAlias('cubic millimetre'); - $newUnit->addAlias('cubic millimetres'); - static::addUnit($newUnit); - - // Cubic centimeter - $newUnit = UnitOfMeasure::linearUnitFactory('cm^3', 1e-6); - $newUnit->addAlias('cm³'); - $newUnit->addAlias('cubic centimeter'); - $newUnit->addAlias('cubic centimeters'); - $newUnit->addAlias('cubic centimetre'); - $newUnit->addAlias('cubic centimetres'); - static::addUnit($newUnit); - - // Cubic decimeter - $newUnit = UnitOfMeasure::linearUnitFactory('dm^3', 1e-3); - $newUnit->addAlias('dm³'); - $newUnit->addAlias('cubic decimeter'); - $newUnit->addAlias('cubic decimeters'); - $newUnit->addAlias('cubic decimetre'); - $newUnit->addAlias('cubic decimetres'); - static::addUnit($newUnit); - - // Cubic kilometer - $newUnit = UnitOfMeasure::linearUnitFactory('km^3', 1e9); - $newUnit->addAlias('km³'); - $newUnit->addAlias('cubic kilometer'); - $newUnit->addAlias('cubic kilometers'); - $newUnit->addAlias('cubic kilometre'); - $newUnit->addAlias('cubic kilometres'); - static::addUnit($newUnit); + static::addMissingSIPrefixedUnits( + $cubicmeter, + 1, + '%pm^3', + [ + '%pm³', + 'cubic %Pmeter', + 'cubic %Pmeters', + 'cubic %Pmetre', + 'cubic %Pmetres' + ], + 3 // square power factor + ); // Cubic foot $newUnit = UnitOfMeasure::linearUnitFactory('ft^3', 2.831685e-2); @@ -148,6 +129,7 @@ protected static function initialize() $newUnit->addAlias('gallons'); $newUnit->addAlias('us gal'); static::addUnit($newUnit); + // Quart $newUnit = UnitOfMeasure::linearUnitFactory('qt', 9.4635295e-4); $newUnit->addAlias('quart'); @@ -155,6 +137,7 @@ protected static function initialize() $newUnit->addAlias('qts'); $newUnit->addAlias('liq qt'); static::addUnit($newUnit); + // Fluid Ounce $newUnit = UnitOfMeasure::linearUnitFactory('fl oz', 2.957353e-5); $newUnit->addAlias('fluid ounce'); @@ -163,6 +146,7 @@ protected static function initialize() $newUnit->addAlias('fl. oz.'); $newUnit->addAlias('oz. fl.'); static::addUnit($newUnit); + // Pint $newUnit = UnitOfMeasure::linearUnitFactory('pt', 4.73176475e-4); $newUnit->addAlias('pint'); diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 3b6493e..403546b 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -88,4 +88,36 @@ protected function instantiateTestQuantity() { return new Volume(1, 'm^3'); } + + public function testToCubicDecimeter() + { + $area = new Volume(1, 'm^3'); + $this->assertEquals(1000, $area->toUnit('dm^3')); + $area = new Volume(100, 'm^3'); + $this->assertEquals(100000, $area->toUnit('dm^3')); + } + + public function testToCubicMillimeter() + { + $area = new Volume(1, 'm^3'); + $this->assertEquals(1e9, $area->toUnit('mm^3'), '', 0.000001); + $area = new Volume(100, 'm^3'); + $this->assertEquals(1e11, $area->toUnit('mm^3')); + } + + public function testToLitres() + { + $area = new Volume(1, 'm^3'); + $this->assertEquals(1000, $area->toUnit('l')); + $area = new Volume(100, 'm^3'); + $this->assertEquals(100000, $area->toUnit('l')); + } + + public function testToGallon() + { + $area = new Volume(1, 'm^3'); + $this->assertEquals(264.172051, $area->toUnit('gal'), '', 0.000001); + $area = new Volume(100, 'm^3'); + $this->assertEquals(26417.205124, $area->toUnit('gal'), '', 0.000001); + } } From 0bd6aed94ac6869082dce4eba9b1e1cbe7bb9919 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 00:54:01 +0300 Subject: [PATCH 09/55] Added Si prefixes for volume unit liter --- CHANGELOG.md | 2 +- source/PhysicalQuantity/Volume.php | 62 ++++++++------------------- tests/PhysicalQuantity/VolumeTest.php | 24 +++++++++++ 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a777c14..51f1e7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## v2.2.0 (April XXth, 2019) - Added option power factor for Si factor, ie for square (2) and cubic (3) - Added Si prefixes for area unit square meter (m^2) -- Added Si prefixes for volume unit cubic meter (m^3) +- Added Si prefixes for volume unit cubic meter (m^3) and litres - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index 67bb851..dca43bb 100644 --- a/source/PhysicalQuantity/Volume.php +++ b/source/PhysicalQuantity/Volume.php @@ -57,53 +57,25 @@ protected static function initialize() $newUnit->addAlias('cubic yards'); static::addUnit($newUnit); - // Milliliters - $newUnit = UnitOfMeasure::linearUnitFactory('ml', 1e-6); - $newUnit->addAlias('milliliter'); - $newUnit->addAlias('milliliters'); - $newUnit->addAlias('millilitre'); - $newUnit->addAlias('millilitres'); - static::addUnit($newUnit); - - // Centiliters - $newUnit = UnitOfMeasure::linearUnitFactory('cl', 1e-5); - $newUnit->addAlias('centiliter'); - $newUnit->addAlias('centiliters'); - $newUnit->addAlias('centilitre'); - $newUnit->addAlias('centilitres'); - static::addUnit($newUnit); - - // Deciliter - $newUnit = UnitOfMeasure::linearUnitFactory('dl', 1e-4); - $newUnit->addAlias('deciliter'); - $newUnit->addAlias('deciliters'); - $newUnit->addAlias('decilitre'); - $newUnit->addAlias('decilitres'); - static::addUnit($newUnit); - // Liter - $newUnit = UnitOfMeasure::linearUnitFactory('l', 1e-3); - $newUnit->addAlias('liter'); - $newUnit->addAlias('liters'); - $newUnit->addAlias('litre'); - $newUnit->addAlias('litres'); - static::addUnit($newUnit); + $liter = UnitOfMeasure::linearUnitFactory('l', 1e-3); + $liter->addAlias('liter'); + $liter->addAlias('liters'); + $liter->addAlias('litre'); + $liter->addAlias('litres'); + static::addUnit($liter); - // Decaliter - $newUnit = UnitOfMeasure::linearUnitFactory('dal', 1e-2); - $newUnit->addAlias('decaliter'); - $newUnit->addAlias('decaliters'); - $newUnit->addAlias('decalitre'); - $newUnit->addAlias('decalitres'); - static::addUnit($newUnit); - - // Hectoliter - $newUnit = UnitOfMeasure::linearUnitFactory('hl', 1e-1); - $newUnit->addAlias('hectoliter'); - $newUnit->addAlias('hectoliters'); - $newUnit->addAlias('hectolitre'); - $newUnit->addAlias('hectolitres'); - static::addUnit($newUnit); + static::addMissingSIPrefixedUnits( + $cubicmeter, + 1e-3, + '%pl', + [ + '%Pliter', + '%Pliters', + '%Plitre', + '%Plitres' + ] + ); // Cup $newUnit = UnitOfMeasure::linearUnitFactory('cup', 2.365882e-4); diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 403546b..f10be9c 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -113,6 +113,30 @@ public function testToLitres() $this->assertEquals(100000, $area->toUnit('l')); } + public function testToMilliLitres() + { + $area = new Volume(1, 'l'); + $this->assertEquals(1000, $area->toUnit('ml')); + $area = new Volume(100, 'l'); + $this->assertEquals(100000, $area->toUnit('ml')); + } + + public function testToHectoLitres() + { + $area = new Volume(1, 'l'); + $this->assertEquals(0.01, $area->toUnit('hl')); + $area = new Volume(100, 'l'); + $this->assertEquals(1, $area->toUnit('hl')); + } + + public function testToKiloLitres() + { + $area = new Volume(1, 'l'); + $this->assertEquals(0.001, $area->toUnit('kl')); + $area = new Volume(100, 'l'); + $this->assertEquals(0.1, $area->toUnit('kl')); + } + public function testToGallon() { $area = new Volume(1, 'm^3'); From cbf73dfc36820cf6536c0b79e8ccc144677ea204 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 00:56:12 +0300 Subject: [PATCH 10/55] Fixed liter Si prefixes --- source/PhysicalQuantity/Volume.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index dca43bb..0b1a957 100644 --- a/source/PhysicalQuantity/Volume.php +++ b/source/PhysicalQuantity/Volume.php @@ -66,8 +66,8 @@ protected static function initialize() static::addUnit($liter); static::addMissingSIPrefixedUnits( - $cubicmeter, - 1e-3, + $liter, + 1, '%pl', [ '%Pliter', From ac53b1a79c98d690a82646636b58e90a3eb68e8e Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 01:22:38 +0300 Subject: [PATCH 11/55] Added units for mass: Hundredweight, US short Ton, US long Ton, Asian picul --- CHANGELOG.md | 2 + source/PhysicalQuantity/Mass.php | 73 +++++++++++++++++++++++------ tests/PhysicalQuantity/MassTest.php | 40 +++++++++++++++- 3 files changed, 99 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f1e7a..adea592 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Added Si prefixes for volume unit cubic meter (m^3) and litres - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec +- Additional units for mass: Hundredweight, US short Ton, US long Ton, Asian picul +- Fixed native unit for mass as gram ## v2.1.0 (July 24th, 2016) diff --git a/source/PhysicalQuantity/Mass.php b/source/PhysicalQuantity/Mass.php index 6b877fe..7debbac 100644 --- a/source/PhysicalQuantity/Mass.php +++ b/source/PhysicalQuantity/Mass.php @@ -13,15 +13,15 @@ class Mass extends AbstractPhysicalQuantity protected static function initialize() { - // Kilogram - $kilogram = UnitOfMeasure::nativeUnitFactory('kg'); - $kilogram->addAlias('kilogram'); - $kilogram->addAlias('kilograms'); - static::addUnit($kilogram); + // Gram + $gram = UnitOfMeasure::nativeUnitFactory('g'); + $gram->addAlias('gram'); + $gram->addAlias('grams'); + static::addUnit($gram); static::addMissingSIPrefixedUnits( - $kilogram, - 1e-3, + $gram, + 1, '%pg', [ '%Pgram', @@ -30,30 +30,73 @@ protected static function initialize() ); // Tonne (metric) - $newUnit = UnitOfMeasure::linearUnitFactory('t', 1e3); - $newUnit->addAlias('ton'); - $newUnit->addAlias('tons'); - $newUnit->addAlias('tonne'); - $newUnit->addAlias('tonnes'); + $tonne = UnitOfMeasure::linearUnitFactory('t', 1e6); + $tonne->addAlias('T'); + $tonne->addAlias('ton'); + $tonne->addAlias('tons'); + $tonne->addAlias('tonne'); + $tonne->addAlias('tonnes'); + static::addUnit($tonne); + + static::addMissingSIPrefixedUnits( + $tonne, + 1, + '%pt', + [ + '%pT', + '%Pton', + '%Ptons', + '%Ptonne', + '%Ptonnes', + ] + ); + + // Grain + $newUnit = UnitOfMeasure::linearUnitFactory('gr', 453.59237 / (16 * 437.5)); + $newUnit->addAlias('grain'); + $newUnit->addAlias('grains'); static::addUnit($newUnit); // Pound - $newUnit = UnitOfMeasure::linearUnitFactory('lb', 4.5359237e-1); + $newUnit = UnitOfMeasure::linearUnitFactory('lb', 453.59237); $newUnit->addAlias('lbs'); $newUnit->addAlias('pound'); $newUnit->addAlias('pounds'); static::addUnit($newUnit); // Ounce - $newUnit = UnitOfMeasure::linearUnitFactory('oz', 4.5359237e-1 / 16); + $newUnit = UnitOfMeasure::linearUnitFactory('oz', 453.59237 / 16); $newUnit->addAlias('ounce'); $newUnit->addAlias('ounces'); static::addUnit($newUnit); // Stone - $newUnit = UnitOfMeasure::linearUnitFactory('st', 4.5359237e-1 * 14); + $newUnit = UnitOfMeasure::linearUnitFactory('st', 453.59237 * 14); $newUnit->addAlias('stone'); $newUnit->addAlias('stones'); static::addUnit($newUnit); + + // Hundredweight + $newUnit = UnitOfMeasure::linearUnitFactory('cwt', 453.59237 * 112); + $newUnit->addAlias('hundredweight'); + $newUnit->addAlias('hundredweights'); + static::addUnit($newUnit); + + // US short Ton + $newUnit = UnitOfMeasure::linearUnitFactory('ust', 453.59237 * 2000); + $newUnit->addAlias('us short ton'); + $newUnit->addAlias('us short tons'); + static::addUnit($newUnit); + + // US long Ton + $newUnit = UnitOfMeasure::linearUnitFactory('ukt', 453.59237 * 2240); + $newUnit->addAlias('uk long ton'); + $newUnit->addAlias('uk long tons'); + static::addUnit($newUnit); + + // Asian picul + $newUnit = UnitOfMeasure::linearUnitFactory('picul', 60478.982); + $newUnit->addAlias('tam'); + static::addUnit($newUnit); } } diff --git a/tests/PhysicalQuantity/MassTest.php b/tests/PhysicalQuantity/MassTest.php index 35cd302..12b65e8 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -71,10 +71,13 @@ class MassTest extends AbstractPhysicalQuantityTestCase 'yoctogram', 'yoctograms', 't', + 'T', 'ton', 'tons', 'tonne', 'tonnes', + 'MT', + 'mT', 'lb', 'lbs', 'pound', @@ -85,11 +88,22 @@ class MassTest extends AbstractPhysicalQuantityTestCase 'st', 'stone', 'stones', + 'cwt', + 'hundredweight', + 'hundredweights', + 'ukt', + 'uk long ton', + 'uk long tons', + 'ust', + 'us short ton', + 'us short tons', + 'picul', + 'tam', ]; protected function instantiateTestQuantity() { - return new Mass(1, 'kg'); + return new Mass(1, 'g'); } public function testToGrams() @@ -115,4 +129,28 @@ public function testToStones() $quantity = new Mass(14, 'pound'); $this->assertEquals(1, $quantity->toUnit('st')); } + + public function testToHundredweight() + { + $quantity = new Mass(5, 'kg'); + $this->assertEquals(0.098420653, $quantity->toUnit('cwt'), '', 0.00000001); + } + + public function testToUSshortTon() + { + $quantity = new Mass(5, 'kg'); + $this->assertEquals(0.0055115566, $quantity->toUnit('ust'), '', 0.000000001); + } + + public function testToUSlongTon() + { + $quantity = new Mass(5, 'kg'); + $this->assertEquals(0.0049210326, $quantity->toUnit('ukt'), '', 0.000000001); + } + + public function testToPicul() + { + $quantity = new Mass(5, 'kg'); + $this->assertEquals(0.08267335, $quantity->toUnit('picul'), '', 0.00000001); + } } From bc265c5e19d150df4d87eaaeb4604c02401ec1e1 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 01:23:01 +0300 Subject: [PATCH 12/55] Added alias for volume litres --- source/PhysicalQuantity/Volume.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index 0b1a957..44adabf 100644 --- a/source/PhysicalQuantity/Volume.php +++ b/source/PhysicalQuantity/Volume.php @@ -59,6 +59,7 @@ protected static function initialize() // Liter $liter = UnitOfMeasure::linearUnitFactory('l', 1e-3); + $liter->addAlias('L'); $liter->addAlias('liter'); $liter->addAlias('liters'); $liter->addAlias('litre'); @@ -70,6 +71,7 @@ protected static function initialize() 1, '%pl', [ + '%pL', '%Pliter', '%Pliters', '%Plitre', From 5fe93d348e8dd720b3419651bc29cac6a48132c5 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 01:34:35 +0300 Subject: [PATCH 13/55] Additional units for pressure: Torr, Technical atmosphere, Kilo and Mega pound per Square Inch --- CHANGELOG.md | 1 + source/PhysicalQuantity/Pressure.php | 31 +++++++++++++++++++++---- tests/PhysicalQuantity/PressureTest.php | 3 +++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adea592..ba2d4a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec - Additional units for mass: Hundredweight, US short Ton, US long Ton, Asian picul +- Additional units for pressure: Torr, Technical atmosphere, Kilo and Mega pound per Square Inch - Fixed native unit for mass as gram diff --git a/source/PhysicalQuantity/Pressure.php b/source/PhysicalQuantity/Pressure.php index 61630e2..6dac955 100644 --- a/source/PhysicalQuantity/Pressure.php +++ b/source/PhysicalQuantity/Pressure.php @@ -16,6 +16,7 @@ protected static function initialize() // Pascal $pascal = UnitOfMeasure::nativeUnitFactory('Pa'); $pascal->addAlias('pascal'); + $pascal->addAlias('pascals'); static::addUnit($pascal); static::addMissingSIPrefixedUnits( @@ -24,15 +25,26 @@ protected static function initialize() '%pPa', [ '%Ppascal', + '%Ppascals', ] ); - // Atmosphere + // Standard atmosphere $newUnit = UnitOfMeasure::linearUnitFactory('atm', 101325); $newUnit->addAlias('atmosphere'); $newUnit->addAlias('atmospheres'); static::addUnit($newUnit); + // Technical atmosphere + $newUnit = UnitOfMeasure::linearUnitFactory('at', 98066.5); + $newUnit->addAlias('technical atmosphere'); + $newUnit->addAlias('technical atmospheres'); + static::addUnit($newUnit); + + // Torr + $newUnit = UnitOfMeasure::linearUnitFactory('torr', 101325 / 760); + static::addUnit($newUnit); + // Bar $bar = UnitOfMeasure::linearUnitFactory('bar', 1e5); static::addUnit($bar); @@ -44,20 +56,29 @@ protected static function initialize() ); // Inch of Mercury - $newUnit = UnitOfMeasure::linearUnitFactory('inHg', 3.386389e3); + $newUnit = UnitOfMeasure::linearUnitFactory('inHg', 1 / 0.00029530); $newUnit->addAlias('inches of mercury'); static::addUnit($newUnit); // Millimeter of Mercury - $newUnit = UnitOfMeasure::linearUnitFactory('mmHg', 133.3224); + $newUnit = UnitOfMeasure::linearUnitFactory('mmHg', 133.322387415); $newUnit->addAlias('millimeters of mercury'); $newUnit->addAlias('millimetres of mercury'); - $newUnit->addAlias('torr'); static::addUnit($newUnit); // Pound per Square Inch - $newUnit = UnitOfMeasure::linearUnitFactory('psi', 6.894757e3); + $newUnit = UnitOfMeasure::linearUnitFactory('psi', 6894.757293168); $newUnit->addAlias('pounds per square inch'); static::addUnit($newUnit); + + // Kilo-pound per Square Inch + $newUnit = UnitOfMeasure::linearUnitFactory('ksi', 6894757.293168); + $newUnit->addAlias('kilopounds per square inch'); + static::addUnit($newUnit); + + // Kilo-pound per Square Inch + $newUnit = UnitOfMeasure::linearUnitFactory('Mpsi', 6894757293.168); + $newUnit->addAlias('megapounds per square inch'); + static::addUnit($newUnit); } } diff --git a/tests/PhysicalQuantity/PressureTest.php b/tests/PhysicalQuantity/PressureTest.php index c8a4ab7..06e8a62 100644 --- a/tests/PhysicalQuantity/PressureTest.php +++ b/tests/PhysicalQuantity/PressureTest.php @@ -49,6 +49,7 @@ class PressureTest extends AbstractPhysicalQuantityTestCase 'zeptopascal', 'yPa', 'yoctopascal', + 'at', 'atm', 'atmosphere', 'atmospheres', @@ -80,6 +81,8 @@ class PressureTest extends AbstractPhysicalQuantityTestCase 'millimetres of mercury', 'torr', 'psi', + 'ksi', + 'Mpsi', 'pounds per square inch', ]; From 3eb79a201ce1107082240ccab6cec4d7b428fe60 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 11:20:27 +0300 Subject: [PATCH 14/55] Added Si prefixes for velocity units, added new velocity units. --- CHANGELOG.md | 2 + source/PhysicalQuantity/Velocity.php | 96 ++++++++++++++++++++++--- tests/PhysicalQuantity/VelocityTest.php | 10 ++- 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2d4a5..2c5709a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,12 @@ - Added option power factor for Si factor, ie for square (2) and cubic (3) - Added Si prefixes for area unit square meter (m^2) - Added Si prefixes for volume unit cubic meter (m^3) and litres +- Added Si prefixes for velocity unit m/s, m/min and m/h - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec - Additional units for mass: Hundredweight, US short Ton, US long Ton, Asian picul - Additional units for pressure: Torr, Technical atmosphere, Kilo and Mega pound per Square Inch +- Additional units for velocity: Mach, meters/min, feet per minute, miles per second - Fixed native unit for mass as gram diff --git a/source/PhysicalQuantity/Velocity.php b/source/PhysicalQuantity/Velocity.php index 0b6ba30..d7a7d3a 100644 --- a/source/PhysicalQuantity/Velocity.php +++ b/source/PhysicalQuantity/Velocity.php @@ -2,10 +2,13 @@ namespace PhpUnitsOfMeasure\PhysicalQuantity; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; +use PhpUnitsOfMeasure\HasSIUnitsTrait; use PhpUnitsOfMeasure\UnitOfMeasure; class Velocity extends AbstractPhysicalQuantity { + use HasSIUnitsTrait; + protected static $unitDefinitions; protected static function initialize() @@ -19,23 +22,93 @@ protected static function initialize() $meterpersecond->addAlias('metre per second'); static::addUnit($meterpersecond); - // kilometers per hour - $newUnit = UnitOfMeasure::linearUnitFactory('km/h', 0.277778); - $newUnit->addAlias('km/hour'); - $newUnit->addAlias('kilometer per hour'); - $newUnit->addAlias('kilometers per hour'); - $newUnit->addAlias('kilometre per hour'); - $newUnit->addAlias('kilometres per hour'); - static::addUnit($newUnit); + static::addMissingSIPrefixedUnits( + $meterpersecond, + 1, + '%pm/s', + [ + '%Pmeters/sec', + '%Pmeter per second', + '%Pmeters per second', + '%Pmetre per second', + '%Pmetres per second' + ] + ); + + // meter per minute + $meterperminute = UnitOfMeasure::linearUnitFactory('m/min', 1 / 60); + $meterperminute->addAlias('meters/min'); + $meterperminute->addAlias('meters per minute'); + $meterperminute->addAlias('meter per minute'); + $meterperminute->addAlias('metres per minute'); + $meterperminute->addAlias('metre per minute'); + static::addUnit($meterperminute); + + static::addMissingSIPrefixedUnits( + $meterperminute, + 1, + '%pm/min', + [ + '%Pmeters/min', + '%Pmeter per minute', + '%Pmeters per minute', + '%Pmetre per minute', + '%Pmetres per minute' + ] + ); + + // meters per hour + $meterperhour = UnitOfMeasure::linearUnitFactory('m/h', 1 / 3600); + $meterperhour->addAlias('m/hr'); + $meterperhour->addAlias('m/hour'); + $meterperhour->addAlias('meters/hour'); + $meterperhour->addAlias('meter per hour'); + $meterperhour->addAlias('meters per hour'); + $meterperhour->addAlias('metre per hour'); + $meterperhour->addAlias('metres per hour'); + static::addUnit($meterperhour); + + static::addMissingSIPrefixedUnits( + $meterperhour, + 1, + '%pm/h', + [ + '%pm/hr', + '%pm/hour', + '%Pmeters/hour', + '%Pmeter per hour', + '%Pmeters per hour', + '%Pmetre per hour', + '%Pmetres per hour' + ] + ); // feet per second $newUnit = UnitOfMeasure::linearUnitFactory('ft/s', 0.3048); + $newUnit->addAlias('fps'); + $newUnit->addAlias('FPS'); $newUnit->addAlias('feet/sec'); $newUnit->addAlias('feet per second'); static::addUnit($newUnit); + // feet per minute + $newUnit = UnitOfMeasure::linearUnitFactory('ft/min', 0.3048 / 60); + $newUnit->addAlias('fpm'); + $newUnit->addAlias('FPM'); + $newUnit->addAlias('feet/min'); + $newUnit->addAlias('feet per minute'); + static::addUnit($newUnit); + + // miles per second + $newUnit = UnitOfMeasure::linearUnitFactory('mps', 1609.344); + $newUnit->addAlias('MPS'); + $newUnit->addAlias('miles/sec'); + $newUnit->addAlias('miles per second'); + static::addUnit($newUnit); + // miles per hour - $newUnit = UnitOfMeasure::linearUnitFactory('mph', 0.44704); + $newUnit = UnitOfMeasure::linearUnitFactory('mph', 1609.344 / 3600); + $newUnit->addAlias('MPH'); $newUnit->addAlias('miles/hour'); $newUnit->addAlias('miles per hour'); static::addUnit($newUnit); @@ -44,5 +117,10 @@ protected static function initialize() $newUnit = UnitOfMeasure::linearUnitFactory('knot', 0.514444); $newUnit->addAlias('knots'); static::addUnit($newUnit); + + // Supersonic speed + $newUnit = UnitOfMeasure::linearUnitFactory('mach', 344); + $newUnit->addAlias('speed of sound'); + static::addUnit($newUnit); } } diff --git a/tests/PhysicalQuantity/VelocityTest.php b/tests/PhysicalQuantity/VelocityTest.php index d2dc96a..e40d024 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -37,7 +37,7 @@ protected function instantiateTestQuantity() public function testToKilometersPerHour() { $speed = new Velocity(1, 'km/h'); - $this->assertEquals(0.277778, $speed->toUnit('m/s')); + $this->assertEquals(0.277778, $speed->toUnit('m/s'), '', 0.000001); } public function testToFeetPerSecond() @@ -49,7 +49,7 @@ public function testToFeetPerSecond() public function testToKmPerHour() { $speed = new Velocity(2, 'mph'); - $this->assertEquals(3.2186854250516594, $speed->toUnit('km/h')); + $this->assertEquals(3.218688, $speed->toUnit('km/h'), '', 0.000001); } public function testToKnot() @@ -57,4 +57,10 @@ public function testToKnot() $speed = new Velocity(2, 'm/s'); $this->assertEquals(3.8876923435786983, $speed->toUnit('knot')); } + + public function testToMach() + { + $speed = new Velocity(1000, 'm/s'); + $this->assertEquals(2.906977, $speed->toUnit('mach'), '', 0.000001); + } } From 15f238c3ad70391622471a7f4a477adc8e0e0623 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 11:41:39 +0300 Subject: [PATCH 15/55] Added lowercase aliases for temperature units --- CHANGELOG.md | 1 + source/PhysicalQuantity/Temperature.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c5709a..fe0bd3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v2.2.0 (April XXth, 2019) - Added option power factor for Si factor, ie for square (2) and cubic (3) +- Added lowercase aliases for temperature units - Added Si prefixes for area unit square meter (m^2) - Added Si prefixes for volume unit cubic meter (m^3) and litres - Added Si prefixes for velocity unit m/s, m/min and m/h diff --git a/source/PhysicalQuantity/Temperature.php b/source/PhysicalQuantity/Temperature.php index 75d2f3f..f8df428 100644 --- a/source/PhysicalQuantity/Temperature.php +++ b/source/PhysicalQuantity/Temperature.php @@ -15,6 +15,7 @@ protected static function initialize() { // Kelvin $kelvin = UnitOfMeasure::nativeUnitFactory('K'); + $kelvin->addAlias('k'); $kelvin->addAlias('°K'); $kelvin->addAlias('kelvin'); static::addUnit($kelvin); @@ -24,6 +25,7 @@ protected static function initialize() 1, '%pK', [ + '%pk', '%Pkelvin', ] ); @@ -38,6 +40,7 @@ function ($x) { return $x + 273.15; } ); + $newUnit->addAlias('c'); $newUnit->addAlias('C'); $newUnit->addAlias('celsius'); static::addUnit($newUnit); @@ -52,13 +55,18 @@ function ($x) { return ($x + 459.67) * 5/9; } ); + $newUnit->addAlias('f'); $newUnit->addAlias('F'); $newUnit->addAlias('fahrenheit'); static::addUnit($newUnit); // Degree Rankine - $newUnit = UnitOfMeasure::linearUnitFactory('°R', 5/9); + $newUnit = UnitOfMeasure::linearUnitFactory('°Ra', 5/9); + $newUnit->addAlias('°R'); + $newUnit->addAlias('r'); $newUnit->addAlias('R'); + $newUnit->addAlias('ra'); + $newUnit->addAlias('Ra'); $newUnit->addAlias('rankine'); static::addUnit($newUnit); @@ -72,6 +80,10 @@ function ($x) { return 373.15 - $x * 2/3; } ); + $newUnit->addAlias('°D'); + $newUnit->addAlias('D'); + $newUnit->addAlias('d'); + $newUnit->addAlias('de'); $newUnit->addAlias('De'); $newUnit->addAlias('delisle'); static::addUnit($newUnit); @@ -102,6 +114,7 @@ function ($x) { ); $newUnit->addAlias('°Re'); $newUnit->addAlias('Ré'); + $newUnit->addAlias('re'); $newUnit->addAlias('Re'); $newUnit->addAlias('réaumur'); $newUnit->addAlias('reaumur'); @@ -119,6 +132,7 @@ function ($x) { ); $newUnit->addAlias('°Ro'); $newUnit->addAlias('Rø'); + $newUnit->addAlias('ro'); $newUnit->addAlias('Ro'); $newUnit->addAlias('rømer'); $newUnit->addAlias('romer'); From c45d530f487748c484b581b79ffe4d462cdcf76f Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 11:42:12 +0300 Subject: [PATCH 16/55] Added unittests for termperature units --- tests/PhysicalQuantity/TemperatureTest.php | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index 62f7d90..ff28ea3 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -83,4 +83,45 @@ protected function instantiateTestQuantity() { return new Temperature(1, 'K'); } + + public function testToCelsius() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(40, $value->toUnit('C'), '', 0.000001); + } + + public function testToFahrenheit() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(104, $value->toUnit('F'), '', 0.000001); + } + + public function testToRankine() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(563.67, $value->toUnit('Ra'), '', 0.000001); + } + public function testToDelisle() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(90, $value->toUnit('D'), '', 0.000001); + } + + public function testToNewton() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(13.2, $value->toUnit('N'), '', 0.000001); + } + + public function testToReaumur() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(32, $value->toUnit('Re'), '', 0.000001); + } + + public function testToRomer() + { + $value = new Temperature(313.15, 'K'); + $this->assertEquals(28.5, $value->toUnit('Ro'), '', 0.000001); + } } From bef50b658bdc039f855671d53f972421396f7152 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 11:50:47 +0300 Subject: [PATCH 17/55] Added alias for knot velocity unit --- source/PhysicalQuantity/Velocity.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/PhysicalQuantity/Velocity.php b/source/PhysicalQuantity/Velocity.php index d7a7d3a..7c5c6a6 100644 --- a/source/PhysicalQuantity/Velocity.php +++ b/source/PhysicalQuantity/Velocity.php @@ -114,7 +114,8 @@ protected static function initialize() static::addUnit($newUnit); // knot - $newUnit = UnitOfMeasure::linearUnitFactory('knot', 0.514444); + $newUnit = UnitOfMeasure::linearUnitFactory('kn', 0.514444); + $newUnit->addAlias('knot'); $newUnit->addAlias('knots'); static::addUnit($newUnit); From fdce81249e7ccf3fcee3ea9c9a46ff4600fb5778 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 12:12:19 +0300 Subject: [PATCH 18/55] Added Si prefixes for acceleration unit m/s^2 --- CHANGELOG.md | 5 +++-- source/PhysicalQuantity/Acceleration.php | 16 ++++++++++++++++ tests/PhysicalQuantity/AccelerationTest.php | 12 ++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe0bd3e..507caa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ - Added option power factor for Si factor, ie for square (2) and cubic (3) - Added lowercase aliases for temperature units - Added Si prefixes for area unit square meter (m^2) -- Added Si prefixes for volume unit cubic meter (m^3) and litres -- Added Si prefixes for velocity unit m/s, m/min and m/h +- Added Si prefixes for volume units cubic meter (m^3) and litres +- Added Si prefixes for velocity units m/s, m/min and m/h +- Added Si prefixes for acceleration unit m/s^2 - Additional units for energy: electronvolt, calorie - Additional units for length: light year, parsec - Additional units for mass: Hundredweight, US short Ton, US long Ton, Asian picul diff --git a/source/PhysicalQuantity/Acceleration.php b/source/PhysicalQuantity/Acceleration.php index 470a634..d341147 100644 --- a/source/PhysicalQuantity/Acceleration.php +++ b/source/PhysicalQuantity/Acceleration.php @@ -2,10 +2,13 @@ namespace PhpUnitsOfMeasure\PhysicalQuantity; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; +use PhpUnitsOfMeasure\HasSIUnitsTrait; use PhpUnitsOfMeasure\UnitOfMeasure; class Acceleration extends AbstractPhysicalQuantity { + use HasSIUnitsTrait; + protected static $unitDefinitions; protected static function initialize() @@ -18,5 +21,18 @@ protected static function initialize() $meterpersecondsquared->addAlias('metre per second squared'); $meterpersecondsquared->addAlias('metres per second squared'); static::addUnit($meterpersecondsquared); + + static::addMissingSIPrefixedUnits( + $meterpersecondsquared, + 1, + '%pm/s^2', + [ + '%pm/s²', + '%Pmeter per second squared', + '%Pmeters per second squared', + '%Pmetre per second squared', + '%Pmetres per second squared', + ] + ); } } diff --git a/tests/PhysicalQuantity/AccelerationTest.php b/tests/PhysicalQuantity/AccelerationTest.php index 380132a..410c895 100644 --- a/tests/PhysicalQuantity/AccelerationTest.php +++ b/tests/PhysicalQuantity/AccelerationTest.php @@ -19,4 +19,16 @@ protected function instantiateTestQuantity() { return new Acceleration(1, 'm/s^2'); } + + public function testToKiloMeterPerSecond() + { + $value = new Acceleration(1000, 'm/s^2'); + $this->assertEquals(1, $value->toUnit('km/s^2')); + } + + public function testToMilliMeterPerSecond() + { + $value = new Acceleration(1, 'm/s^2'); + $this->assertEquals(1000, $value->toUnit('mm/s^2')); + } } From 3fde6bb3db4cc40c3a9a48e9898fc40b6db3da58 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 12:37:10 +0300 Subject: [PATCH 19/55] Additional unit for power: dBm --- CHANGELOG.md | 1 + source/PhysicalQuantity/Power.php | 14 ++++++++++++++ tests/PhysicalQuantity/PowerTest.php | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 507caa3..92e2587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Additional units for mass: Hundredweight, US short Ton, US long Ton, Asian picul - Additional units for pressure: Torr, Technical atmosphere, Kilo and Mega pound per Square Inch - Additional units for velocity: Mach, meters/min, feet per minute, miles per second +- Additional unit for power: dBm - Fixed native unit for mass as gram diff --git a/source/PhysicalQuantity/Power.php b/source/PhysicalQuantity/Power.php index 0ec2a70..d6f5d15 100644 --- a/source/PhysicalQuantity/Power.php +++ b/source/PhysicalQuantity/Power.php @@ -28,5 +28,19 @@ protected static function initialize() '%Pwatts', ] ); + + // decibels-milliwatt + $newUnit = new UnitOfMeasure( + 'dBm', + function ($x) { + return 10 * log10($x) + 30; + }, + function ($x) { + return pow(10, ($x / 10)) / 1000; + } + ); + $newUnit->addAlias('dbm'); + $newUnit->addAlias('decibels-milliwatt'); + static::addUnit($newUnit); } } diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index b66e777..8e872ac 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -49,4 +49,24 @@ public function testToWatt() $quantity = new Power(1, 'kW'); $this->assertEquals(1000, $quantity->toUnit('W')); } + + public function testToDecibelsMilliWatt() + { + $quantity = new Power(0.01, 'mW'); + $this->assertEquals(-20, $quantity->toUnit('dBm')); + $quantity = new Power(1, 'mW'); + $this->assertEquals(0, $quantity->toUnit('dBm')); + $quantity = new Power(10, 'kW'); + $this->assertEquals(70, $quantity->toUnit('dBm')); + } + + public function testFromDecibelsMilliWatt() + { + $quantity = new Power(-20, 'dBm'); + $this->assertEquals(0.00001, $quantity->toUnit('W')); + $quantity = new Power(0, 'dBm'); + $this->assertEquals(0.001, $quantity->toUnit('W')); + $quantity = new Power(70, 'dBm'); + $this->assertEquals(10000, $quantity->toUnit('W')); + } } From efa49fb0768f223425094816d2778c3dcaf9aa6e Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 15:08:51 +0300 Subject: [PATCH 20/55] More accurate volume units factor --- source/PhysicalQuantity/Volume.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index 44adabf..e70897b 100644 --- a/source/PhysicalQuantity/Volume.php +++ b/source/PhysicalQuantity/Volume.php @@ -33,25 +33,25 @@ protected static function initialize() 'cubic %Pmetre', 'cubic %Pmetres' ], - 3 // square power factor + 3 // cubic power factor ); // Cubic foot - $newUnit = UnitOfMeasure::linearUnitFactory('ft^3', 2.831685e-2); + $newUnit = UnitOfMeasure::linearUnitFactory('ft^3', pow(0.3048, 3)); $newUnit->addAlias('ft³'); $newUnit->addAlias('cubic foot'); $newUnit->addAlias('cubic feet'); static::addUnit($newUnit); // Cubic inch - $newUnit = UnitOfMeasure::linearUnitFactory('in^3', 1.638706e-5); + $newUnit = UnitOfMeasure::linearUnitFactory('in^3', pow(0.0254, 3)); $newUnit->addAlias('in³'); $newUnit->addAlias('cubic inch'); $newUnit->addAlias('cubic inches'); static::addUnit($newUnit); // Cubic yard - $newUnit = UnitOfMeasure::linearUnitFactory('yd^3', 7.645549e-1); + $newUnit = UnitOfMeasure::linearUnitFactory('yd^3', pow(0.9144, 3)); $newUnit->addAlias('yd³'); $newUnit->addAlias('cubic yard'); $newUnit->addAlias('cubic yards'); From 88b994c3c95bb39ea6dc9f9e64fc5ad429fd4512 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 15:09:12 +0300 Subject: [PATCH 21/55] Added new Physical Quantity volume flow with commonly used units --- CHANGELOG.md | 1 + source/PhysicalQuantity/VolumeFlow.php | 222 ++++++++++++ tests/PhysicalQuantity/VolumeFlowTest.php | 404 ++++++++++++++++++++++ 3 files changed, 627 insertions(+) create mode 100644 source/PhysicalQuantity/VolumeFlow.php create mode 100644 tests/PhysicalQuantity/VolumeFlowTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e2587..986ede8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v2.2.0 (April XXth, 2019) - Added option power factor for Si factor, ie for square (2) and cubic (3) +- Added new Physical Quantity volume flow with commonly used units - Added lowercase aliases for temperature units - Added Si prefixes for area unit square meter (m^2) - Added Si prefixes for volume units cubic meter (m^3) and litres diff --git a/source/PhysicalQuantity/VolumeFlow.php b/source/PhysicalQuantity/VolumeFlow.php new file mode 100644 index 0000000..cde9db4 --- /dev/null +++ b/source/PhysicalQuantity/VolumeFlow.php @@ -0,0 +1,222 @@ +addAlias('m³/s'); + $cubicmeterpersecond->addAlias('cubic meter per second'); + $cubicmeterpersecond->addAlias('cubic meters per second'); + $cubicmeterpersecond->addAlias('cubic metre per second'); + $cubicmeterpersecond->addAlias('cubic metres per second'); + static::addUnit($cubicmeterpersecond); + + static::addMissingSIPrefixedUnits( + $cubicmeterpersecond, + 1, + '%pm^3/s', + [ + '%pm³/s', + 'cubic %Pmeter per second', + 'cubic %Pmeters per second', + 'cubic %Pmetre per second', + 'cubic %Pmetres per second' + ], + 3 // cubic power factor + ); + + // Cubic meter per minute + $newUnit = UnitOfMeasure::linearUnitFactory('m^3/min', 1 / 60); + $newUnit->addAlias('m³/min'); + $newUnit->addAlias('cmm'); + $newUnit->addAlias('CMM'); + $newUnit->addAlias('cubic meter per minute'); + $newUnit->addAlias('cubic meters per minute'); + static::addUnit($newUnit); + + // Cubic meter per hour + $newUnit = UnitOfMeasure::linearUnitFactory('m^3/h', 1 / 3600); + $newUnit->addAlias('m³/h'); + $newUnit->addAlias('cmh'); + $newUnit->addAlias('CMH'); + $newUnit->addAlias('cubic meter per hour'); + $newUnit->addAlias('cubic meters per hour'); + static::addUnit($newUnit); + + // Liter per second + $literpersecond = UnitOfMeasure::linearUnitFactory('l/s', 1e-3); + $literpersecond->addAlias('L/s'); + $literpersecond->addAlias('liter per second'); + $literpersecond->addAlias('liters per second'); + $literpersecond->addAlias('litre per second'); + $literpersecond->addAlias('litres per second'); + static::addUnit($literpersecond); + + static::addMissingSIPrefixedUnits( + $literpersecond, + 1, + '%pl/s', + [ + '%pL/s', + '%Pliter per second', + '%Pliters per second', + '%Plitre per second', + '%Plitres per second' + ] + ); + + // Liter per minute + $newUnit = UnitOfMeasure::linearUnitFactory('l/min', 1e-3 / 60); + $newUnit->addAlias('L/min'); + $newUnit->addAlias('lpm'); + $newUnit->addAlias('LPM'); + $newUnit->addAlias('liter per minute'); + $newUnit->addAlias('liters per minute'); + $newUnit->addAlias('litre per minute'); + $newUnit->addAlias('litres per minute'); + static::addUnit($newUnit); + + // Liter per hour + $newUnit = UnitOfMeasure::linearUnitFactory('l/h', 1e-3 / 3600); + $newUnit->addAlias('L/h'); + $newUnit->addAlias('lph'); + $newUnit->addAlias('LPH'); + $newUnit->addAlias('liter per hour'); + $newUnit->addAlias('liters per hour'); + $newUnit->addAlias('litre per hour'); + $newUnit->addAlias('litres per hour'); + static::addUnit($newUnit); + + // Cubic foot per second + $newUnit = UnitOfMeasure::linearUnitFactory('ft^3/s', pow(0.3048, 3)); + $newUnit->addAlias('cfs'); + $newUnit->addAlias('CFS'); + $newUnit->addAlias('ft³/s'); + $newUnit->addAlias('cubic foot per second'); + $newUnit->addAlias('cubic feet per second'); + static::addUnit($newUnit); + + // Cubic foot per minute + $newUnit = UnitOfMeasure::linearUnitFactory('ft^3/min', pow(0.3048, 3) / 60); + $newUnit->addAlias('cfm'); + $newUnit->addAlias('CFM'); + $newUnit->addAlias('ft³/min'); + $newUnit->addAlias('cubic foot per minute'); + $newUnit->addAlias('cubic feet per minute'); + static::addUnit($newUnit); + + // Cubic foot per hour + $newUnit = UnitOfMeasure::linearUnitFactory('ft^3/h', pow(0.3048, 3) / 3600); + $newUnit->addAlias('cfh'); + $newUnit->addAlias('CFH'); + $newUnit->addAlias('ft³/h'); + $newUnit->addAlias('cubic foot per hour'); + $newUnit->addAlias('cubic feet per hour'); + static::addUnit($newUnit); + + // Gallon per second + $newUnit = UnitOfMeasure::linearUnitFactory('gal/s', 3.7854118e-3); + $newUnit->addAlias('gps'); + $newUnit->addAlias('GPS'); + $newUnit->addAlias('gallon per second'); + $newUnit->addAlias('gallons per second'); + $newUnit->addAlias('us gal/s'); + static::addUnit($newUnit); + + // Gallon per minute + $newUnit = UnitOfMeasure::linearUnitFactory('gal/min', 3.7854118e-3 / 60); + $newUnit->addAlias('gpm'); + $newUnit->addAlias('GPM'); + $newUnit->addAlias('gallon per minute'); + $newUnit->addAlias('gallons per minute'); + $newUnit->addAlias('us gal/min'); + static::addUnit($newUnit); + + // Gallon per hour + $newUnit = UnitOfMeasure::linearUnitFactory('gal/h', 3.7854118e-3 / 3600); + $newUnit->addAlias('gph'); + $newUnit->addAlias('GPH'); + $newUnit->addAlias('gallon per hour'); + $newUnit->addAlias('gallons per hour'); + $newUnit->addAlias('us gal/h'); + static::addUnit($newUnit); + + // Gallon per day + $newUnit = UnitOfMeasure::linearUnitFactory('gal/d', 3.7854118e-3 / 86400); + $newUnit->addAlias('gpd'); + $newUnit->addAlias('GPD'); + $newUnit->addAlias('gallon per day'); + $newUnit->addAlias('gallons per day'); + $newUnit->addAlias('us gal/d'); + static::addUnit($newUnit); + + /* + // Cubic inch per second + $newUnit = UnitOfMeasure::linearUnitFactory('in^3', pow(0.0254, 3)); + $newUnit->addAlias('in³'); + $newUnit->addAlias('cubic inch per second'); + $newUnit->addAlias('cubic inches per second'); + static::addUnit($newUnit); + + // Cubic yard per second + $newUnit = UnitOfMeasure::linearUnitFactory('yd^3', pow(0.9144, 3)); + $newUnit->addAlias('yd³'); + $newUnit->addAlias('cubic yard per second'); + $newUnit->addAlias('cubic yards per second'); + static::addUnit($newUnit); + + // Cup + $newUnit = UnitOfMeasure::linearUnitFactory('cup', 2.365882e-4); + $newUnit->addAlias('cup'); + $newUnit->addAlias('cups'); + static::addUnit($newUnit); + + // teaspoon + $newUnit = UnitOfMeasure::linearUnitFactory('tsp', 0.00000492892); + $newUnit->addAlias('teaspoon'); + $newUnit->addAlias('teaspoons'); + static::addUnit($newUnit); + + // tablespoon + $newUnit = UnitOfMeasure::linearUnitFactory('tbsp', 0.00001478676); + $newUnit->addAlias('tablespoon'); + $newUnit->addAlias('tablespoons'); + static::addUnit($newUnit); + + // Quart + $newUnit = UnitOfMeasure::linearUnitFactory('qt', 9.4635295e-4); + $newUnit->addAlias('quart'); + $newUnit->addAlias('quarts'); + $newUnit->addAlias('qts'); + $newUnit->addAlias('liq qt'); + static::addUnit($newUnit); + + // Fluid Ounce + $newUnit = UnitOfMeasure::linearUnitFactory('fl oz', 2.957353e-5); + $newUnit->addAlias('fluid ounce'); + $newUnit->addAlias('fluid ounces'); + $newUnit->addAlias('fluid oz'); + $newUnit->addAlias('fl. oz.'); + $newUnit->addAlias('oz. fl.'); + static::addUnit($newUnit); + + // Pint + $newUnit = UnitOfMeasure::linearUnitFactory('pt', 4.73176475e-4); + $newUnit->addAlias('pint'); + $newUnit->addAlias('pints'); + $newUnit->addAlias('liq pt'); + static::addUnit($newUnit); + */ + } +} diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php new file mode 100644 index 0000000..b46fb57 --- /dev/null +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -0,0 +1,404 @@ +listAllUnits() as $unit => $aliases) { + echo(" '$unit',\n"); + foreach ($aliases as $alias) { + echo(" '$alias',\n"); + } + } + */ + $this->assertEquals(1000, $area->toUnit('dm^3/s')); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEquals(100000, $area->toUnit('dm^3/s')); + } + + + public function testToCubicMillimeterSecond() + { + $area = new VolumeFlow(1, 'm^3/s'); + $this->assertEquals(1e9, $area->toUnit('mm^3/s'), '', 0.000001); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEquals(1e11, $area->toUnit('mm^3/s')); + } + + public function testToLitresSecond() + { + $area = new VolumeFlow(1, 'm^3/s'); + $this->assertEquals(1000, $area->toUnit('l/s')); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEquals(100000, $area->toUnit('l/s')); + } + + public function testToMilliLitresSecond() + { + $area = new VolumeFlow(1, 'l/s'); + $this->assertEquals(1000, $area->toUnit('ml/s')); + $area = new VolumeFlow(100, 'l/s'); + $this->assertEquals(100000, $area->toUnit('ml/s')); + } + + public function testToHectoLitresSecond() + { + $area = new VolumeFlow(1, 'l/s'); + $this->assertEquals(0.01, $area->toUnit('hl/s')); + $area = new VolumeFlow(100, 'l/s'); + $this->assertEquals(1, $area->toUnit('hl/s')); + } + + public function testToKiloLitresSecond() + { + $area = new VolumeFlow(1, 'l/s'); + $this->assertEquals(0.001, $area->toUnit('kl/s')); + $area = new VolumeFlow(100, 'l/s'); + $this->assertEquals(0.1, $area->toUnit('kl/s')); + } + + public function testToCFM() + { + $area = new VolumeFlow(1, 'CMM'); + $this->assertEquals(35.314667, $area->toUnit('CFM'), '', 0.000001); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEquals(211888.000328, $area->toUnit('CFM'), '', 0.000001); + } + +} From eef6f7d800854fc49737d22d250961087c437b51 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 19 Apr 2019 20:37:16 +0300 Subject: [PATCH 22/55] Increased minimum php version to 5.6 --- .travis.yml | 1 - composer.json | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 953655f..af55a7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false dist: trusty php: - - '5.5' - '5.6' - '7.0' - '7.1' diff --git a/composer.json b/composer.json index e16c60c..ca6fda7 100644 --- a/composer.json +++ b/composer.json @@ -27,11 +27,11 @@ }, "require": { - "php": ">=5.5.0" + "php": ">=5.6.0" }, "require-dev": { - "phpunit/phpunit": "4.8.*", + "phpunit/phpunit": "5.*", "squizlabs/php_codesniffer": "2.2.*" }, From 6361446257125295c8a2071d5a43e42352ba4c3f Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Sat, 20 Apr 2019 16:40:35 +0300 Subject: [PATCH 23/55] Fixed PHP CS tests --- source/HasSIUnitsTrait.php | 2 +- tests/PhysicalQuantity/AreaTest.php | 2 +- tests/PhysicalQuantity/StorageTest.php | 1 + tests/PhysicalQuantity/VolumeFlowTest.php | 1 - 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/HasSIUnitsTrait.php b/source/HasSIUnitsTrait.php index fdeac69..5b2f46f 100644 --- a/source/HasSIUnitsTrait.php +++ b/source/HasSIUnitsTrait.php @@ -43,7 +43,7 @@ protected static function addMissingSIPrefixedUnits( $toBaseSiUnitFactor, $namePattern, array $aliasPatterns = [], - $powerFactor = NULL + $powerFactor = null ) { /** * The standard set of SI prefixes diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 782c8f0..deb16ff 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -86,7 +86,7 @@ class AreaTest extends AbstractPhysicalQuantityTestCase 'centimeter squared', 'centimeters squared', 'centimetre squared', - 'centimetres squared', + 'centimetres squared', 'mm^2', 'mm²', 'millimeter squared', diff --git a/tests/PhysicalQuantity/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php index 3f52cba..da1c574 100644 --- a/tests/PhysicalQuantity/StorageTest.php +++ b/tests/PhysicalQuantity/StorageTest.php @@ -9,6 +9,7 @@ class StorageTest extends AbstractPhysicalQuantityTestCase protected function instantiateTestQuantity() { + return new Storage(1, 'byte'); } public function testByteToByte() diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index b46fb57..1f02ba9 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -400,5 +400,4 @@ public function testToCFM() $area = new VolumeFlow(100, 'm^3/s'); $this->assertEquals(211888.000328, $area->toUnit('CFM'), '', 0.000001); } - } From fcf010d723b39011da5bb7921090ec710e955e43 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 12:56:32 +0300 Subject: [PATCH 24/55] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index af7f565..85b2eb8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # PHP Units of Measure -master: [![Build Status](https://travis-ci.org/PhpUnitsOfMeasure/php-units-of-measure.png?branch=master)](https://travis-ci.org/PhpUnitsOfMeasure/php-units-of-measure) +master: [![Build Status](https://travis-ci.org/landy2005/php-units-of-measure.png?branch=master)](https://travis-ci.org/landy2005/php-units-of-measure) ## Introduction This is a PHP library for representing and converting physical units of measure. The utility of this library is in encapsulating physical quantities in such a way that you don't have to keep track of which unit they're represented in. For instance: From 8dec0f35a84bc2c669c4428525ea8a73e93b0859 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 13:04:27 +0300 Subject: [PATCH 25/55] Update .travis.yml Update php versions up to 8.0 --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index af55a7b..4ee5208 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,10 @@ php: - '7.0' - '7.1' - '7.2' + - '7.3' + - '7.4' + - '8.0' - nightly - - hhvm before_script: - composer self-update @@ -23,7 +25,5 @@ notifications: matrix: allow_failures: - - php: '5.5' - php: nightly - - php: hhvm From 1eb60a0c72364f776d78fc6045c5cec5bf70a340 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 13:15:05 +0300 Subject: [PATCH 26/55] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 85b2eb8..402723a 100644 --- a/README.md +++ b/README.md @@ -319,4 +319,4 @@ composer phpcs ### Continuous Integration The above tests are automatically run against Github commits with Travis-CI. -- https://travis-ci.org/PhpUnitsOfMeasure/php-units-of-measure +- https://travis-ci.org/landy2005/php-units-of-measure From f7f2876259ba245ebb2b7e5712e8489c9735cfb6 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 13:24:55 +0300 Subject: [PATCH 27/55] Fix Xdebug coverage error. --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ca6fda7..ef9d335 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,10 @@ }, "scripts": { - "test": "phpunit -c ./tests/phpunit.xml.dist ./tests", + "test": [ + "@putenv XDEBUG_MODE=coverage", + "phpunit -c ./tests/phpunit.xml.dist ./tests" + ], "phpcs": "phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./" }, From 28ac980b0deb1c28c3337e84ba532641e1ef5862 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 13:55:39 +0300 Subject: [PATCH 28/55] Update phpcs version. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ef9d335..7f5604f 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "require-dev": { "phpunit/phpunit": "5.*", - "squizlabs/php_codesniffer": "2.2.*" + "squizlabs/php_codesniffer": "3.5.*" }, "replace": { From 2a80a22aad6fe0cc9bad1eba8540aebab0e6e518 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 13:59:27 +0300 Subject: [PATCH 29/55] Fix dist for php 8.0 --- .travis.yml | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ee5208..07d6951 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,14 @@ language: php -sudo: false dist: trusty php: - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - '7.3' - - '7.4' - - '8.0' + - 5.6 + - 7.0 + - 7.1 + - 7.2 + - 7.3 + - 7.4 + - 8.0 - nightly before_script: @@ -23,7 +22,10 @@ script: notifications: email: false -matrix: +jobs: + include: + - php: 8.0 + dist: xenial allow_failures: - php: nightly From 1435e5f351d025177e81eced62e988218885067d Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:02:24 +0300 Subject: [PATCH 30/55] Fix travis job for php 8.0 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 07d6951..b91b8ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ php: - 7.2 - 7.3 - 7.4 - - 8.0 - nightly before_script: From 2df340ffa47eccd3a818fce02fdf93e8f80b1489 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:04:50 +0300 Subject: [PATCH 31/55] Fix phpcs error --- source/PhysicalQuantity/Storage.php | 1 - 1 file changed, 1 deletion(-) diff --git a/source/PhysicalQuantity/Storage.php b/source/PhysicalQuantity/Storage.php index 7543354..dd1789b 100644 --- a/source/PhysicalQuantity/Storage.php +++ b/source/PhysicalQuantity/Storage.php @@ -46,6 +46,5 @@ protected static function initialize() $unit = UnitOfMeasure::linearUnitFactory('petabyte', 1125899906842624); $unit->addAlias('PB'); static::addUnit($unit); - } } From 0440ea80c494a4d7a4625285c66c81cb3101db91 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:10:41 +0300 Subject: [PATCH 32/55] Update dist for nightly php. --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b91b8ae..a94feaf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ php: - 7.2 - 7.3 - 7.4 - - nightly before_script: - composer self-update @@ -25,6 +24,8 @@ jobs: include: - php: 8.0 dist: xenial + - php: nightly + dist: xenial allow_failures: - php: nightly From ee465a941ceb6faf99d40d84117bffd5c72a69b2 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:11:23 +0300 Subject: [PATCH 33/55] Switch to phpunit 6. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7f5604f..1e92638 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { - "phpunit/phpunit": "5.*", + "phpunit/phpunit": "6.*", "squizlabs/php_codesniffer": "3.5.*" }, From b26c2f7805cbac2138c40604ca7937220707bd3c Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:36:13 +0300 Subject: [PATCH 34/55] Try different phpunit on php 8.0 --- .travis.yml | 2 ++ composer.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a94feaf..f2f1948 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,6 +24,8 @@ jobs: include: - php: 8.0 dist: xenial + before_script: + - composer require "phpunit/phpunit":"^6.5.14" - php: nightly dist: xenial allow_failures: diff --git a/composer.json b/composer.json index 1e92638..7f5604f 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { - "phpunit/phpunit": "6.*", + "phpunit/phpunit": "5.*", "squizlabs/php_codesniffer": "3.5.*" }, From 1730633bb3d9f889d30d83b770276484dffc8ec0 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:49:24 +0300 Subject: [PATCH 35/55] Try different phpunit on php 8.0 --- .travis.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index f2f1948..4121a57 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,6 @@ language: php dist: trusty -php: - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 7.4 - before_script: - composer self-update - composer install --verbose --prefer-dist --no-interaction -o @@ -22,12 +14,20 @@ notifications: jobs: include: +# - php: 5.6 +# - php: 7.0 +# - php: 7.1 +# - php: 7.2 +# - php: 7.3 + - php: 7.4 - php: 8.0 dist: xenial before_script: - composer require "phpunit/phpunit":"^6.5.14" - php: nightly dist: xenial + before_script: + - composer require "phpunit/phpunit":"^6.5.14" allow_failures: - php: nightly From bc231e7711336d82e24a46f10adf6f7f627c500e Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:50:40 +0300 Subject: [PATCH 36/55] Try different phpunit on php 8.0 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4121a57..336dbe3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,11 +23,11 @@ jobs: - php: 8.0 dist: xenial before_script: - - composer require "phpunit/phpunit":"^6.5.14" + - composer require "phpunit/phpunit":"9.*" - php: nightly dist: xenial before_script: - - composer require "phpunit/phpunit":"^6.5.14" + - composer require "phpunit/phpunit":"9.*" allow_failures: - php: nightly From 2daf0be524e43ae6198314c54bbace547751046b Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 14:54:29 +0300 Subject: [PATCH 37/55] Try different phpunit on php 8.0 and 5.6 --- .travis.yml | 6 ++++-- composer.json | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 336dbe3..f1d595e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,9 @@ notifications: jobs: include: -# - php: 5.6 + - php: 5.6 + before_script: + - composer require "phpunit/phpunit":"5.*" # - php: 7.0 # - php: 7.1 # - php: 7.2 @@ -23,7 +25,7 @@ jobs: - php: 8.0 dist: xenial before_script: - - composer require "phpunit/phpunit":"9.*" + - composer require "phpunit/phpunit":"8.*" - php: nightly dist: xenial before_script: diff --git a/composer.json b/composer.json index 7f5604f..1e92638 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ }, "require-dev": { - "phpunit/phpunit": "5.*", + "phpunit/phpunit": "6.*", "squizlabs/php_codesniffer": "3.5.*" }, From c7b74251aca24b4dbe64daf099213a9c94e07c5e Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 15:03:06 +0300 Subject: [PATCH 38/55] Fix phpunit tests --- tests/AbstractPhysicalQuantityTest.php | 4 ++-- tests/DemonstrationTests.php | 4 ++-- tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php | 4 ++-- tests/UnitOfMeasureTest.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index bbda2ed..058e3ce 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -2,7 +2,7 @@ namespace PhpUnitsOfMeasureTest; -use PHPUnit_Framework_TestCase; +use \PHPUnit\Framework\TestCase; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; use PhpUnitsOfMeasure\UnitOfMeasureInterface; use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch; @@ -20,7 +20,7 @@ * * @runTestsInSeparateProcesses */ -class AbstractPhysicalQuantityTest extends PHPUnit_Framework_TestCase +class AbstractPhysicalQuantityTest extends \PHPUnit\Framework\TestCase { protected function getTestUnitOfMeasure($name, $aliases = []) { diff --git a/tests/DemonstrationTests.php b/tests/DemonstrationTests.php index 95db723..626c03b 100644 --- a/tests/DemonstrationTests.php +++ b/tests/DemonstrationTests.php @@ -2,7 +2,7 @@ namespace PhpUnitsOfMeasureTest; -use PHPUnit_Framework_TestCase; +use \PHPUnit\Framework\TestCase; use PhpUnitsOfMeasure\UnitOfMeasure; use PhpUnitsOfMeasure\AbstractDerivedPhysicalQuantity; @@ -32,7 +32,7 @@ * * @runTestsInSeparateProcesses */ -class DemonstrationTests extends PHPUnit_Framework_TestCase +class DemonstrationTests extends \PHPUnit\Framework\TestCase { public function testInstantiatingQuantities() { diff --git a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php index aeaf9a0..e8b2650 100644 --- a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php +++ b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php @@ -2,12 +2,12 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; -use PHPUnit_Framework_TestCase; +use \PHPUnit\Framework\TestCase; /** * This is a parent class for all the PhysicalQuantity childrens' test cases. */ -abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCase +abstract class AbstractPhysicalQuantityTestCase extends \PHPUnit\Framework\TestCase { protected $supportedUnitsWithAliases = []; diff --git a/tests/UnitOfMeasureTest.php b/tests/UnitOfMeasureTest.php index 41c8b21..03d2075 100644 --- a/tests/UnitOfMeasureTest.php +++ b/tests/UnitOfMeasureTest.php @@ -4,7 +4,7 @@ use PhpUnitsOfMeasure\UnitOfMeasure; -class UnitOfMeasureTest extends \PHPUnit_Framework_TestCase +class UnitOfMeasureTest extends \PHPUnit\Framework\TestCase { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::__construct From 2053365163b39706e0bbb3842870906a1266ac1b Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 15:23:25 +0300 Subject: [PATCH 39/55] Fix phpunit tests --- tests/AbstractPhysicalQuantityTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index 058e3ce..a74c277 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -178,7 +178,7 @@ public function testAdd( $diffString ) { if ($shouldThrowException) { - $this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); + $this->expectException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); } $sum = $firstValue->add($secondValue); @@ -200,7 +200,7 @@ public function testSubtract( $diffString ) { if ($shouldThrowException) { - $this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); + $this->expectException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); } $difference = $firstValue->subtract($secondValue); From 29065ea4265e21e2d8dad2762636a0b3b60cc5c0 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Wed, 17 Feb 2021 15:32:42 +0300 Subject: [PATCH 40/55] Enable phpunit tests for all php versions. --- .travis.yml | 8 ++++---- tests/phpunit.xml.dist | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index f1d595e..69f3391 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,10 +17,10 @@ jobs: - php: 5.6 before_script: - composer require "phpunit/phpunit":"5.*" -# - php: 7.0 -# - php: 7.1 -# - php: 7.2 -# - php: 7.3 + - php: 7.0 + - php: 7.1 + - php: 7.2 + - php: 7.3 - php: 7.4 - php: 8.0 dist: xenial diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index 6e066ca..a561687 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -13,9 +13,9 @@ - + ../vendor - + ../source From 676a6439dde9b318171f8c703c54f47c790b0c24 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sat, 4 Feb 2023 15:21:00 +0100 Subject: [PATCH 41/55] feat: modernise, drop support for php < 7.4, migrate tests and config for phpUnit 9 --- .gitignore | 3 + .travis.yml | 18 +---- CHANGELOG.md | 4 + README.md | 10 +-- composer.json | 6 +- tests/AbstractPhysicalQuantityTest.php | 76 ++++++++++--------- tests/DemonstrationTests.php | 42 +++++----- tests/Fixtures/PhysicalQuantity/Wigginess.php | 2 +- tests/Fixtures/PhysicalQuantity/Wonkicity.php | 2 +- tests/Fixtures/PhysicalQuantity/Woogosity.php | 2 +- .../AbstractPhysicalQuantityTestCase.php | 15 ++-- tests/PhysicalQuantity/AccelerationTest.php | 5 +- tests/PhysicalQuantity/AngleTest.php | 9 ++- tests/PhysicalQuantity/AreaTest.php | 13 ++-- .../PhysicalQuantity/ElectricCurrentTest.php | 5 +- tests/PhysicalQuantity/EnergyTest.php | 13 ++-- tests/PhysicalQuantity/LengthTest.php | 17 +++-- .../LuminousIntensityTest.php | 5 +- tests/PhysicalQuantity/MassTest.php | 13 ++-- tests/PhysicalQuantity/PowerTest.php | 9 ++- tests/PhysicalQuantity/PressureTest.php | 5 +- tests/PhysicalQuantity/QuantityTest.php | 5 +- tests/PhysicalQuantity/SolidAngleTest.php | 5 +- tests/PhysicalQuantity/TemperatureTest.php | 5 +- tests/PhysicalQuantity/TimeTest.php | 19 ++--- tests/PhysicalQuantity/VelocityTest.php | 13 ++-- tests/PhysicalQuantity/VolumeTest.php | 5 +- tests/UnitOfMeasureTest.php | 36 ++++----- tests/phpunit.xml.dist | 32 ++++---- 29 files changed, 202 insertions(+), 192 deletions(-) diff --git a/.gitignore b/.gitignore index 4430eed..7e4c780 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ Desktop.ini # Vagrant files /.vagrant/ + +.phpunit.result.cache +tests/clover.xml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 545a1d5..aa47bfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,10 @@ sudo: false dist: trusty php: - - '5.5' - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - nightly - - hhvm + - '7.4' + - '8.0' + - '8.1' + - '8.2' before_script: - composer self-update @@ -21,10 +18,3 @@ script: notifications: email: false - -matrix: - allow_failures: - - php: '5.5' - - php: nightly - - php: hhvm - diff --git a/CHANGELOG.md b/CHANGELOG.md index 86baa65..010fa55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.2.0 (xxx) + +- Drop support of php < 7.4 + ## v2.1.0 (July 24th, 2016) - Added getUnitDefinitions() method to PhysicalQuantity classes, to get a raw list of UnitofMeasure objects defined on that quantity - Added UPPERCASE templating support for autogenerated metric units to support names like MEGA_METERS, for example. diff --git a/README.md b/README.md index 2a7e6b1..585de6c 100644 --- a/README.md +++ b/README.md @@ -43,17 +43,17 @@ isTooTallToRideThisTrain( new Length(2, 'm') ); ``` ## Installation -This library is best included in your projects via Composer. See the [Composer website](http://getcomposer.org/) for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/php-units-of-measure/php-units-of-measure). +This library is best included in your projects via Composer. See the [Composer website](https://getcomposer.org/) for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/php-units-of-measure/php-units-of-measure). -If you'd prefer to manually include this library as a dependency in your project, then it is recommended that you use a [PSR-4](http://www.php-fig.org/psr/psr-4/) compliant PHP autoloader. The mapping between this project's root namespace and its base directory is: +If you'd prefer to manually include this library as a dependency in your project, then it is recommended that you use a [PSR-4](https://www.php-fig.org/psr/psr-4/) compliant PHP autoloader. The mapping between this project's root namespace and its base directory is: - vendor namespace 'PhpUnitsOfMeasure\' maps to the library's base directory 'source/' See the documentation of your autoloader for further instructions. ### Project Tags and Versions -This project follows the guidelines set out in [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html). In general, versions are of the form 'X.Y.Z', and increments to X denote backward-incompatible major changes. +This project follows the guidelines set out in [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html). In general, versions are of the form 'X.Y.Z', and increments to X denote backward-incompatible major changes. -It is recommended that if your project includes this project as a dependency and you are using an automated dependency management tool such as [Composer](http://getcomposer.org/), then you should 'pin' the major version (X) and allow only variations in 'Y' (minor changes) and 'Z' (bugfixes). See the documentation of your dependency manager for more details. +It is recommended that if your project includes this project as a dependency and you are using an automated dependency management tool such as [Composer](https://getcomposer.org/), then you should 'pin' the major version (X) and allow only variations in 'Y' (minor changes) and 'Z' (bugfixes). See the documentation of your dependency manager for more details. ## Use @@ -239,7 +239,7 @@ class Length extends AbstractPhysicalQuantity Now any program which uses `Length` will start with the cubits unit already built in. Note that here we used the more concise linear unit factory method, but the result is equivalent to the expanded form calling the `UnitOfMeasure` constructor, as used above. Also, notice that the `static` keyword was used instead of the class name, though either would be acceptable in this case. ### Adding New Physical Quantities -[Physical quantities](http://en.wikipedia.org/wiki/Physical_quantity) are categories of measurable values, like mass, length, force, etc. +[Physical quantities](https://en.wikipedia.org/wiki/Physical_quantity) are categories of measurable values, like mass, length, force, etc. For physical quantities that are not already present in this library, it will be necessary to write a class to support a new one. All physical quantities implement the `\PhpUnitsOfMeasure\PhysicalQuantityInterface` interface, typically extend the `\PhpUnitsOfMeasure\AbstractPhysicalQuantity` class, and typically have only an `initialize()` method which creates the quantity's units of measure. See above for typical examples of physical quantity classes and of how to add new units to a quantity class. diff --git a/composer.json b/composer.json index fbb6c28..15e22cd 100644 --- a/composer.json +++ b/composer.json @@ -27,12 +27,12 @@ }, "require": { - "php": ">=5.5.0" + "php": ">=7.4" }, "require-dev": { - "phpunit/phpunit": "4.8.*", - "squizlabs/php_codesniffer": "2.8.1" + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^2.8.1" }, "replace": { diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index bbda2ed..d424f1b 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -2,14 +2,10 @@ namespace PhpUnitsOfMeasureTest; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; use PhpUnitsOfMeasure\UnitOfMeasureInterface; use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch; -use PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias; -use PhpUnitsOfMeasure\Exception\NonNumericValue; -use PhpUnitsOfMeasure\Exception\NonStringUnitName; -use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure; use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wonkicity; use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Woogosity; @@ -20,11 +16,11 @@ * * @runTestsInSeparateProcesses */ -class AbstractPhysicalQuantityTest extends PHPUnit_Framework_TestCase +class AbstractPhysicalQuantityTest extends TestCase { protected function getTestUnitOfMeasure($name, $aliases = []) { - $newUnit = $this->getMockBuilder('PhpUnitsOfMeasure\UnitOfMeasureInterface') + $newUnit = $this->getMockBuilder(UnitOfMeasureInterface::class) ->getMock(); $newUnit->method('getName') ->willReturn($name); @@ -43,7 +39,7 @@ function ($value) use ($aliases) { /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit */ - public function testAddUnit() + public function testAddUnit(): void { $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict']); @@ -53,10 +49,10 @@ public function testAddUnit() /** * @dataProvider exceptionProducingUnitsProvider * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit - * @expectedException \PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias */ - public function testAddUnitFailsOnNameCollision($unitName, $unitAliases) + public function testAddUnitFailsOnNameCollision($unitName, $unitAliases): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias::class); $newUnit = $this->getTestUnitOfMeasure($unitName, $unitAliases); Wonkicity::addUnit($newUnit); @@ -65,7 +61,7 @@ public function testAddUnitFailsOnNameCollision($unitName, $unitAliases) /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::getUnit */ - public function testGetUnit() + public function testGetUnit(): void { $unit = Wonkicity::getUnit('u'); @@ -74,49 +70,50 @@ public function testGetUnit() /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::getUnit - * @expectedException \PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure */ - public function testGetUnitFailsOnUnknownUnit() + public function testGetUnitFailsOnUnknownUnit(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class); Wonkicity::getUnit('someUnknownUnit'); } /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__construct */ - public function testInstantiateNewUnit() + public function testInstantiateNewUnit(): void { $value = new Wonkicity(1.234, 'quatloos'); } /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__construct - * @expectedException \PhpUnitsOfMeasure\Exception\NonNumericValue */ - public function testInstantiateNewUnitNonNumericValue() + public function testInstantiateNewUnitNonNumericValue(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonNumericValue::class); $value = new Wonkicity('string', 'quatloos'); } /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__construct - * @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName */ - public function testInstantiateNewUnitNonStringUnit() + public function testInstantiateNewUnitNonStringUnit(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class); $value = new Wonkicity(1.234, 42); } /** * @dataProvider quantityConversionsProvider * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::toUnit - * @expectedException \PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure */ public function testConvertToUnknownUnitThrowsException( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void + { + $this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class); $value->toUnit('someUnknownUnit'); } @@ -128,7 +125,8 @@ public function testUnitConvertsToArbitraryUnit( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void + { $this->assertSame($valueInArbitraryUnit, $value->toUnit($arbitraryUnit)); } @@ -136,20 +134,20 @@ public function testUnitConvertsToArbitraryUnit( * @dataProvider toStringProvider * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::__toString */ - public function testToString(AbstractPhysicalQuantity $value, $string) + public function testToString(AbstractPhysicalQuantity $value, $string): void { $this->assertSame($string, (string) $value); } /** - * * @dataProvider quantityConversionsProvider */ public function testSerialize( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void + { serialize($value); } @@ -160,7 +158,8 @@ public function testUnserialize( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void + { $unserializedValue = unserialize(serialize($value)); $this->assertSame($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit)); @@ -176,9 +175,10 @@ public function testAdd( AbstractPhysicalQuantity $secondValue, $sumString, $diffString - ) { + ): void + { if ($shouldThrowException) { - $this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); + $this->expectException(PhysicalQuantityMismatch::class); } $sum = $firstValue->add($secondValue); @@ -198,9 +198,11 @@ public function testSubtract( AbstractPhysicalQuantity $secondValue, $sumString, $diffString - ) { + ): void + { if ($shouldThrowException) { - $this->setExpectedException('PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch'); + $this->expectException(PhysicalQuantityMismatch::class); + } $difference = $firstValue->subtract($secondValue); @@ -213,11 +215,11 @@ public function testSubtract( /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::getUnitDefinitions */ - public function testGetAllUnits() + public function testGetAllUnits(): void { $array = Wonkicity::getUnitDefinitions(); - $this->assertTrue(is_array($array)); + $this->assertIsArray($array); $expected = array(Wonkicity::getUnit('u'), Wonkicity::getUnit('v')); $this->assertEquals($array, $expected); @@ -226,7 +228,7 @@ public function testGetAllUnits() /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::isUnitDefined */ - public function testIsUnitDefined() + public function testIsUnitDefined(): void { $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); Wonkicity::addUnit($newUnit); @@ -245,7 +247,7 @@ public function testIsUnitDefined() /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::listAllUnits */ - public function testListAllUnits() + public function testListAllUnits(): void { $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); Wonkicity::addUnit($newUnit); @@ -263,7 +265,7 @@ public function testListAllUnits() * 1) The name of the new unit to test * 2) The set of aliases for the new unit to test */ - public function exceptionProducingUnitsProvider() + public function exceptionProducingUnitsProvider(): array { return [ ['u', []], // new name / existing name collision @@ -279,7 +281,7 @@ public function exceptionProducingUnitsProvider() * 2) The unit name to which to convert * 3) The expected resulting value of the conversion */ - public function quantityConversionsProvider() + public function quantityConversionsProvider(): array { return [ [new Wonkicity(2, 'u'), 'u', 2], @@ -296,7 +298,7 @@ public function quantityConversionsProvider() * 1) The object which will be cast to a string * 2) the expected resulting string from the conversion */ - public function toStringProvider() + public function toStringProvider(): array { return [ [new Wonkicity(2, 'u'), '2 u'], @@ -316,7 +318,7 @@ public function toStringProvider() * 4) The string-cast result of a sum operation with the two operands (ignored for errors) * 5) The string-cast result of a subtraction operation with the two operands (ignored for errors) */ - public function arithmeticProvider() + public function arithmeticProvider(): array { return [ [false, new Wonkicity(2, 'u'), new Wonkicity(2.5, 'u'), '4.5 u', '-0.5 u'], diff --git a/tests/DemonstrationTests.php b/tests/DemonstrationTests.php index 95db723..b8c915b 100644 --- a/tests/DemonstrationTests.php +++ b/tests/DemonstrationTests.php @@ -2,16 +2,10 @@ namespace PhpUnitsOfMeasureTest; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; use PhpUnitsOfMeasure\UnitOfMeasure; -use PhpUnitsOfMeasure\AbstractDerivedPhysicalQuantity; -use PhpUnitsOfMeasure\DerivedPhysicalQuantityFactory; -use PhpUnitsOfMeasure\PhysicalQuantity\UnknownDerivedPhysicalQuantity; -use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Plooposity; -use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Pumpalumpiness; use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess; -use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wonkicity; use PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Woogosity; /** @@ -32,9 +26,9 @@ * * @runTestsInSeparateProcesses */ -class DemonstrationTests extends PHPUnit_Framework_TestCase +class DemonstrationTests extends TestCase { - public function testInstantiatingQuantities() + public function testInstantiatingQuantities(): void { // New quantities are created like this, with // the class name being the physical quantity that @@ -44,25 +38,25 @@ public function testInstantiatingQuantities() // Note that each unit can be referred to by several names // (for example sopee, sopees, s). $a = new Wigginess(1, 'sopee'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); $a = new Wigginess(2.123, 'sopees'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); $a = new Wigginess(2.6, 's'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); $a = new Wigginess(1, 'tumpet'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); $a = new Wigginess(2.123, 'tumpets'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); $a = new Wigginess(2.6, 't'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $a); + $this->assertInstanceOf(Wigginess::class, $a); } - public function testStringConversionForQuantities() + public function testStringConversionForQuantities(): void { // Casting physical quantity objects to strings will // produce a reasonable string describing the quantity. @@ -73,7 +67,7 @@ public function testStringConversionForQuantities() $this->assertSame('21.123 t', (string) $a); } - public function testUnitConversionForQuantities() + public function testUnitConversionForQuantities(): void { // Creating equivalent quantities from existing // quantities in different units of measure is done @@ -90,7 +84,7 @@ public function testUnitConversionForQuantities() $this->assertSame(21/2.5, $a->toUnit('t')); } - public function testCreatingNewUnitsForQuantities() + public function testCreatingNewUnitsForQuantities(): void { // New units of measure can be defined and registered // with physical quantities on the fly. @@ -150,7 +144,7 @@ function ($valueInThisUnit) { $this->assertSame((21*2.5)-100, $b->toUnit('eeee')); } - public function testAutomaticSIUnitsForQuantities() + public function testAutomaticSIUnitsForQuantities(): void { // SI units have a standard prefix naming convention to easily // provide powers-of-ten versions of a standard unit. For instance, @@ -164,14 +158,14 @@ public function testAutomaticSIUnitsForQuantities() // for an example of how this is done. $a = new Woogosity(21, 'plurp'); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Woogosity', $a); + $this->assertInstanceOf(Woogosity::class, $a); $this->assertSame(21*4.5 * 1e3, $a->toUnit('millilupees')); $this->assertSame(21*4.5 * 1e3, $a->toUnit('ml')); $this->assertSame(21*4.5 / 1e6, $a->toUnit('megalupees')); $this->assertSame(21*4.5 / 1e6, $a->toUnit('Ml')); } - public function testAddQuantities() + public function testAddQuantities(): void { // Two quantities of equivalent value can be summed // by calling the add method. @@ -179,11 +173,11 @@ public function testAddQuantities() $b = new Wigginess(2, 'tumpet'); $c = $a->add($b); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $c); + $this->assertInstanceOf(Wigginess::class, $c); $this->assertSame(3 + (2*2.5), $c->toUnit('sopee')); } - public function testSubtractQuantities() + public function testSubtractQuantities(): void { // Similar to the add method, subtract called on the // left-hand-side operand object will subtract the @@ -192,7 +186,7 @@ public function testSubtractQuantities() $b = new Wigginess(2, 'tumpet'); $c = $a->subtract($b); - $this->assertInstanceOf('PhpUnitsOfMeasureTest\Fixtures\PhysicalQuantity\Wigginess', $c); + $this->assertInstanceOf(Wigginess::class, $c); $this->assertSame(3 - (2*2.5), $c->toUnit('sopee')); } } diff --git a/tests/Fixtures/PhysicalQuantity/Wigginess.php b/tests/Fixtures/PhysicalQuantity/Wigginess.php index 6102d5a..cc2ba7d 100644 --- a/tests/Fixtures/PhysicalQuantity/Wigginess.php +++ b/tests/Fixtures/PhysicalQuantity/Wigginess.php @@ -9,7 +9,7 @@ class Wigginess extends AbstractPhysicalQuantity { protected static $unitDefinitions; - protected static function initialize() + protected static function initialize(): void { $native = UnitOfMeasure::nativeUnitFactory('s'); $native->addAlias('sopee'); diff --git a/tests/Fixtures/PhysicalQuantity/Wonkicity.php b/tests/Fixtures/PhysicalQuantity/Wonkicity.php index ff06a4d..5ff59ef 100644 --- a/tests/Fixtures/PhysicalQuantity/Wonkicity.php +++ b/tests/Fixtures/PhysicalQuantity/Wonkicity.php @@ -9,7 +9,7 @@ class Wonkicity extends AbstractPhysicalQuantity { protected static $unitDefinitions; - protected static function initialize() + protected static function initialize(): void { $native = UnitOfMeasure::nativeUnitFactory('u'); $native->addAlias('uvee'); diff --git a/tests/Fixtures/PhysicalQuantity/Woogosity.php b/tests/Fixtures/PhysicalQuantity/Woogosity.php index 0b47a64..1974ce1 100644 --- a/tests/Fixtures/PhysicalQuantity/Woogosity.php +++ b/tests/Fixtures/PhysicalQuantity/Woogosity.php @@ -12,7 +12,7 @@ class Woogosity extends AbstractPhysicalQuantity protected static $unitDefinitions; - protected static function initialize() + protected static function initialize(): void { $native = UnitOfMeasure::nativeUnitFactory('l'); $native->addAlias('lupee'); diff --git a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php index aeaf9a0..d956eb3 100644 --- a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php +++ b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php @@ -2,19 +2,20 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; /** * This is a parent class for all the PhysicalQuantity childrens' test cases. */ -abstract class AbstractPhysicalQuantityTestCase extends PHPUnit_Framework_TestCase +abstract class AbstractPhysicalQuantityTestCase extends TestCase { - protected $supportedUnitsWithAliases = []; + protected array $supportedUnitsWithAliases = []; /** * Verify that the object instantiates without error. */ - public function testConstructorSucceeds() + public function testConstructorSucceeds(): void { $this->instantiateTestQuantity(); } @@ -23,7 +24,7 @@ public function testConstructorSucceeds() * Convert to each of the known supported units, verifying that no * exceptions are thrown. */ - public function testSupportedUnits() + public function testSupportedUnits(): void { $quantity = $this->instantiateTestQuantity(); @@ -34,8 +35,6 @@ public function testSupportedUnits() /** * Build a test object of the target physical quantity. - * - * @return PhysicalQuantityInterface */ - abstract protected function instantiateTestQuantity(); + abstract protected function instantiateTestQuantity(): PhysicalQuantityInterface; } diff --git a/tests/PhysicalQuantity/AccelerationTest.php b/tests/PhysicalQuantity/AccelerationTest.php index 380132a..285b120 100644 --- a/tests/PhysicalQuantity/AccelerationTest.php +++ b/tests/PhysicalQuantity/AccelerationTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Acceleration; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AccelerationTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm/s^2', 'm/s²', 'meter per second squared', @@ -15,7 +16,7 @@ class AccelerationTest extends AbstractPhysicalQuantityTestCase 'metres per second squared', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Acceleration(1, 'm/s^2'); } diff --git a/tests/PhysicalQuantity/AngleTest.php b/tests/PhysicalQuantity/AngleTest.php index 63ed22b..281c9d2 100644 --- a/tests/PhysicalQuantity/AngleTest.php +++ b/tests/PhysicalQuantity/AngleTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Angle; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AngleTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'rad', 'radian', 'radians', @@ -249,18 +250,18 @@ class AngleTest extends AbstractPhysicalQuantityTestCase 'yas', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Angle(1, 'deg'); } - public function testToDegrees() + public function testToDegrees(): void { $angle = new Angle(2 * M_PI, 'rad'); $this->assertEquals(360, $angle->toUnit('deg')); } - public function testToRadians() + public function testToRadians(): void { $angle = new Angle(720, 'degree'); $this->assertEquals(M_PI * 4, $angle->toUnit('rad')); diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 2647184..ff9e21a 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Area; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AreaTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm^2', 'm²', 'meter squared', @@ -61,12 +62,12 @@ class AreaTest extends AbstractPhysicalQuantityTestCase 'acres', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Area(1, 'm^2'); } - public function testToHectares() + public function testToHectares(): void { $area = new Area(3, 'ha'); @@ -77,7 +78,7 @@ public function testToHectares() * There aren't lots of super nice conversions between ac -> m^2, * so we'll check that it's close. */ - public function testToAcres() + public function testToAcres(): void { $area = new Area(13, 'ac'); @@ -86,13 +87,13 @@ public function testToAcres() $this->assertEquals(52609.133491, $area); } - public function testToAre() + public function testToAre(): void { $area = new Area(100, 'm^2'); $this->assertEquals(1, $area->toUnit('are')); } - public function testToDecare() + public function testToDecare(): void { $area = new Area(1000, 'm^2'); $this->assertEquals(1, $area->toUnit('decare')); diff --git a/tests/PhysicalQuantity/ElectricCurrentTest.php b/tests/PhysicalQuantity/ElectricCurrentTest.php index 7c0cb35..0c3d65a 100644 --- a/tests/PhysicalQuantity/ElectricCurrentTest.php +++ b/tests/PhysicalQuantity/ElectricCurrentTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class ElectricCurrentTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'A', 'amp', 'amps', @@ -114,7 +115,7 @@ class ElectricCurrentTest extends AbstractPhysicalQuantityTestCase 'yoctoamperes', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new ElectricCurrent(1, 'A'); } diff --git a/tests/PhysicalQuantity/EnergyTest.php b/tests/PhysicalQuantity/EnergyTest.php index d1f8a47..dc50c1d 100644 --- a/tests/PhysicalQuantity/EnergyTest.php +++ b/tests/PhysicalQuantity/EnergyTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Energy; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class EnergyTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'Wh', 'watt hour', 'watt hours', @@ -58,30 +59,30 @@ class EnergyTest extends AbstractPhysicalQuantityTestCase 'petajoules', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Energy(1, 'Wh'); } - public function testToKilowattHour() + public function testToKilowattHour(): void { $quantity = new Energy(1000, 'Wh'); $this->assertEquals(1, $quantity->toUnit('kWh')); } - public function testToWattHour() + public function testToWattHour(): void { $quantity = new Energy(1, 'kWh'); $this->assertEquals(1000, $quantity->toUnit('Wh')); } - public function testToMegaJoule() + public function testToMegaJoule(): void { $quantity = new Energy(1, 'kWh'); $this->assertEquals(3.6, $quantity->toUnit('megajoule')); } - public function testToJoule() + public function testToJoule(): void { $quantity = new Energy(1, 'Wh'); $this->assertEquals(3600, $quantity->toUnit('joule')); diff --git a/tests/PhysicalQuantity/LengthTest.php b/tests/PhysicalQuantity/LengthTest.php index 27b27f0..74cb2a0 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Length; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class LengthTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm', 'meter', 'meters', @@ -135,42 +136,42 @@ class LengthTest extends AbstractPhysicalQuantityTestCase 'astronomical units', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Length(1, 'm'); } - public function testToMillimeters() + public function testToMillimeters(): void { $quantity = new Length(5, 'm'); $this->assertEquals(5000, $quantity->toUnit('mm')); } - public function testToMegameters() + public function testToMegameters(): void { $quantity = new Length(5, 'm'); $this->assertEquals(5/1e6, $quantity->toUnit('Mm')); } - public function testToInches() + public function testToInches(): void { $quantity = new Length(2, 'ft'); $this->assertEquals(24, $quantity->toUnit('in')); } - public function testToNauticalMiles() + public function testToNauticalMiles(): void { $quantity = new Length(3704, 'm'); $this->assertEquals(2, $quantity->toUnit('nmi')); } - public function testToScandinavianMil() + public function testToScandinavianMil(): void { $quantity = new Length(20000, 'm'); $this->assertEquals(2, $quantity->toUnit('mil')); } - public function testToAstronomicalUnit() + public function testToAstronomicalUnit(): void { $quantity = new Length(150000000, 'km'); $this->assertEquals(1.0026880683402668, $quantity->toUnit('AU')); diff --git a/tests/PhysicalQuantity/LuminousIntensityTest.php b/tests/PhysicalQuantity/LuminousIntensityTest.php index 204158d..b62ba6d 100644 --- a/tests/PhysicalQuantity/LuminousIntensityTest.php +++ b/tests/PhysicalQuantity/LuminousIntensityTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class LuminousIntensityTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'cd', 'candela', 'Ycd', @@ -51,7 +52,7 @@ class LuminousIntensityTest extends AbstractPhysicalQuantityTestCase 'yoctocandela', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new LuminousIntensity(1, 'cd'); } diff --git a/tests/PhysicalQuantity/MassTest.php b/tests/PhysicalQuantity/MassTest.php index 35cd302..0e79f4f 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Mass; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class MassTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'kg', 'kilogram', 'kilograms', @@ -87,30 +88,30 @@ class MassTest extends AbstractPhysicalQuantityTestCase 'stones', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Mass(1, 'kg'); } - public function testToGrams() + public function testToGrams(): void { $quantity = new Mass(5, 'kg'); $this->assertEquals(5000, $quantity->toUnit('g')); } - public function testToMilligrams() + public function testToMilligrams(): void { $quantity = new Mass(5, 'kg'); $this->assertEquals(5*1e6, $quantity->toUnit('mg')); } - public function testToPounds() + public function testToPounds(): void { $quantity = new Mass(16, 'oz'); $this->assertEquals(1, $quantity->toUnit('pound')); } - public function testToStones() + public function testToStones(): void { $quantity = new Mass(14, 'pound'); $this->assertEquals(1, $quantity->toUnit('st')); diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index b66e777..56fa576 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Power; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class PowerTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'W', 'watt', 'watts', @@ -33,18 +34,18 @@ class PowerTest extends AbstractPhysicalQuantityTestCase 'petawatts', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Power(1, 'W'); } - public function testToKilowatt() + public function testToKilowatt(): void { $quantity = new Power(1000, 'W'); $this->assertEquals(1, $quantity->toUnit('kW')); } - public function testToWatt() + public function testToWatt(): void { $quantity = new Power(1, 'kW'); $this->assertEquals(1000, $quantity->toUnit('W')); diff --git a/tests/PhysicalQuantity/PressureTest.php b/tests/PhysicalQuantity/PressureTest.php index c8a4ab7..b97677b 100644 --- a/tests/PhysicalQuantity/PressureTest.php +++ b/tests/PhysicalQuantity/PressureTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Pressure; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class PressureTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'Pa', 'pascal', 'YPa', @@ -83,7 +84,7 @@ class PressureTest extends AbstractPhysicalQuantityTestCase 'pounds per square inch', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Pressure(1, 'Pa'); } diff --git a/tests/PhysicalQuantity/QuantityTest.php b/tests/PhysicalQuantity/QuantityTest.php index 29ef59c..958c5c5 100644 --- a/tests/PhysicalQuantity/QuantityTest.php +++ b/tests/PhysicalQuantity/QuantityTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Quantity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class QuantityTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'mol', 'mole', 'moles', @@ -72,7 +73,7 @@ class QuantityTest extends AbstractPhysicalQuantityTestCase 'yoctomoles', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Quantity(1, 'mol'); } diff --git a/tests/PhysicalQuantity/SolidAngleTest.php b/tests/PhysicalQuantity/SolidAngleTest.php index a282eed..8c920e6 100644 --- a/tests/PhysicalQuantity/SolidAngleTest.php +++ b/tests/PhysicalQuantity/SolidAngleTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\SolidAngle; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class SolidAngleTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'sr', 'steradian', 'steradians', @@ -72,7 +73,7 @@ class SolidAngleTest extends AbstractPhysicalQuantityTestCase 'yoctosteradians', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new SolidAngle(1, 'steradian'); } diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index 62f7d90..23907cc 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Temperature; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class TemperatureTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'K', '°K', 'kelvin', @@ -79,7 +80,7 @@ class TemperatureTest extends AbstractPhysicalQuantityTestCase 'romer', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Temperature(1, 'K'); } diff --git a/tests/PhysicalQuantity/TimeTest.php b/tests/PhysicalQuantity/TimeTest.php index 957d1d8..9a677f2 100644 --- a/tests/PhysicalQuantity/TimeTest.php +++ b/tests/PhysicalQuantity/TimeTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Time; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class TimeTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 's', 'sec', 'secs', @@ -140,48 +141,48 @@ class TimeTest extends AbstractPhysicalQuantityTestCase 'julian years', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Time(1, 'sec'); } - public function testToSeconds() + public function testToSeconds(): void { $angle = new Time(5, 'm'); $this->assertEquals(300, $angle->toUnit('seconds')); } - public function testToMinutes() + public function testToMinutes(): void { $angle = new Time(360, 'sec'); $this->assertEquals(6, $angle->toUnit('min')); } - public function testToHours() + public function testToHours(): void { $angle = new Time(120, 'mins'); $this->assertEquals(2, $angle->toUnit('hrs')); } - public function testToDays() + public function testToDays(): void { $angle = new Time(72, 'hours'); $this->assertEquals(3, $angle->toUnit('days')); } - public function testToWeeks() + public function testToWeeks(): void { $angle = new Time(14, 'd'); $this->assertEquals(2, $angle->toUnit('week')); } - public function testToGregorianYears() + public function testToGregorianYears(): void { $angle = new Time(365.2425, 'd'); $this->assertEquals(1, $angle->toUnit('yr')); } - public function testToJulianYears() + public function testToJulianYears(): void { $angle = new Time(365.25, 'd'); $this->assertEquals(1, $angle->toUnit('jyr')); diff --git a/tests/PhysicalQuantity/VelocityTest.php b/tests/PhysicalQuantity/VelocityTest.php index d2dc96a..09dd8ca 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Velocity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class VelocityTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm/s', 'meters/sec', 'meters per second', @@ -29,30 +30,30 @@ class VelocityTest extends AbstractPhysicalQuantityTestCase 'knots', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Velocity(1, 'm/s'); } - public function testToKilometersPerHour() + public function testToKilometersPerHour(): void { $speed = new Velocity(1, 'km/h'); $this->assertEquals(0.277778, $speed->toUnit('m/s')); } - public function testToFeetPerSecond() + public function testToFeetPerSecond(): void { $speed = new Velocity(2, 'm/s'); $this->assertEquals(6.561679790026246, $speed->toUnit('ft/s')); } - public function testToKmPerHour() + public function testToKmPerHour(): void { $speed = new Velocity(2, 'mph'); $this->assertEquals(3.2186854250516594, $speed->toUnit('km/h')); } - public function testToKnot() + public function testToKnot(): void { $speed = new Velocity(2, 'm/s'); $this->assertEquals(3.8876923435786983, $speed->toUnit('knot')); diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 3b6493e..1b00df0 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Volume; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class VolumeTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm^3', 'm³', 'cubic meter', @@ -84,7 +85,7 @@ class VolumeTest extends AbstractPhysicalQuantityTestCase 'cups', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Volume(1, 'm^3'); } diff --git a/tests/UnitOfMeasureTest.php b/tests/UnitOfMeasureTest.php index 41c8b21..d9bee8b 100644 --- a/tests/UnitOfMeasureTest.php +++ b/tests/UnitOfMeasureTest.php @@ -2,15 +2,16 @@ namespace PhpUnitsOfMeasureTest; +use PHPUnit\Framework\TestCase; use PhpUnitsOfMeasure\UnitOfMeasure; -class UnitOfMeasureTest extends \PHPUnit_Framework_TestCase +class UnitOfMeasureTest extends TestCase { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::__construct * @covers \PhpUnitsOfMeasure\UnitOfMeasure::getName */ - public function testGetName() + public function testGetName(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -27,10 +28,10 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::__construct - * @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName */ - public function testConstructWithNonStringName() + public function testConstructWithNonStringName(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class); $uom = new UnitOfMeasure( 42, function ($valueInNativeUnit) { @@ -46,7 +47,7 @@ function ($valueInThisUnit) { * @covers \PhpUnitsOfMeasure\UnitOfMeasure::addAlias * @covers \PhpUnitsOfMeasure\UnitOfMeasure::getAliases */ - public function testGetAliases() + public function testGetAliases(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -69,10 +70,10 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::addAlias - * @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName */ - public function testAddAliasWithNonStringAlias() + public function testAddAliasWithNonStringAlias(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class); $uom = new UnitOfMeasure( 'quatloos', function ($valueInNativeUnit) { @@ -89,7 +90,7 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::isAliasOf */ - public function testIsAliasOf() + public function testIsAliasOf(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -109,7 +110,7 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::isAliasOf */ - public function testIsNotAliasOf() + public function testIsNotAliasOf(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -128,10 +129,11 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::isAliasOf - * @expectedException \PhpUnitsOfMeasure\Exception\NonStringUnitName + * */ - public function testIsAliasOfWithNonStringAlias() + public function testIsAliasOfWithNonStringAlias(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonStringUnitName::class); $uom = new UnitOfMeasure( 'quatloos', function ($valueInNativeUnit) { @@ -150,7 +152,7 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::convertValueFromNativeUnitOfMeasure */ - public function testConvertValueFromNativeUnitOfMeasure() + public function testConvertValueFromNativeUnitOfMeasure(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -167,10 +169,10 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::convertValueFromNativeUnitOfMeasure - * @expectedException \PhpUnitsOfMeasure\Exception\NonNumericValue */ - public function testConvertValueFromNativeUnitOfMeasureWithNonNumericalValue() + public function testConvertValueFromNativeUnitOfMeasureWithNonNumericalValue(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonNumericValue::class); $uom = new UnitOfMeasure( 'quatloos', function ($valueInNativeUnit) { @@ -187,7 +189,7 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::convertValueToNativeUnitOfMeasure */ - public function testConvertValueToNativeUnitOfMeasure() + public function testConvertValueToNativeUnitOfMeasure(): void { $uom = new UnitOfMeasure( 'quatloos', @@ -204,10 +206,10 @@ function ($valueInThisUnit) { /** * @covers \PhpUnitsOfMeasure\UnitOfMeasure::convertValueToNativeUnitOfMeasure - * @expectedException \PhpUnitsOfMeasure\Exception\NonNumericValue */ - public function testConvertValueToNativeUnitOfMeasureWithNonNumericalValue() + public function testConvertValueToNativeUnitOfMeasureWithNonNumericalValue(): void { + $this->expectException(\PhpUnitsOfMeasure\Exception\NonNumericValue::class); $uom = new UnitOfMeasure( 'quatloos', function ($valueInNativeUnit) { diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index 6e066ca..cf73b4d 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -1,10 +1,10 @@ - - + @@ -12,16 +12,16 @@ - - - ../vendor - - + + ../source - - + + + ../vendor + + + + + - - - From 2884e02793ce7935787fe0e14a0b73480edd2f7a Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sat, 4 Feb 2023 15:35:47 +0100 Subject: [PATCH 42/55] feat: modernise, drop support for php < 7.4, migrate tests and config for phpUnit 9, replace travis with github actions --- .github/workflows/ci.yaml | 57 +++++++++++++++++++++++++++++++++++++++ .travis.yml | 20 -------------- 2 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/ci.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..c1606df --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,57 @@ +# .github/workflows/code_checks.yaml +name: Code_Checks + +on: ["push", "pull_request"] + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: ['7.4'] + stability: [ prefer-stable ] + experimental: [false] + include: + - php: '7.4' + stability: prefer-lowest + - php: '8.0' + - php: '8.1' + - php: '8.2' + + name: PHP ${{ matrix.php }} - ${{ matrix.stability }} tests + steps: + # basically git clone + - uses: actions/checkout@v3 + + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.composer/cache/files + key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + # use PHP of specific version + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: pcov + coverage: pcov + + - name: Install dependencies + run: | + composer install --verbose --prefer-dist --no-interaction -o + + - name: Execute tests + run: vendor/bin/phpunit -c ./tests/phpunit.xml.dist ./tests --verbose + + cs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + coverage: none # disable xdebug, pcov + - run: composer install --no-progress + - run: ./vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./ + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index aa47bfc..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: php -sudo: false -dist: trusty - -php: - - '7.4' - - '8.0' - - '8.1' - - '8.2' - -before_script: - - composer self-update - - composer install --verbose --prefer-dist --no-interaction -o - -script: - - ./vendor/bin/phpunit -c ./tests/phpunit.xml.dist ./tests - - ./vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./ - -notifications: - email: false From 30ce2392507145788523710dd26af0fc11fb7397 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sat, 4 Feb 2023 15:40:28 +0100 Subject: [PATCH 43/55] feat: modernise, drop support for php < 7.4, migrate tests and config for phpUnit 9, replace travis with github actions --- composer.json | 2 +- tests/AbstractPhysicalQuantityTest.php | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 15e22cd..0652ddb 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "require-dev": { "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^2.8.1" + "squizlabs/php_codesniffer": "^3.7" }, "replace": { diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index d424f1b..239e364 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -111,8 +111,7 @@ public function testConvertToUnknownUnitThrowsException( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ): void - { + ): void { $this->expectException(\PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure::class); $value->toUnit('someUnknownUnit'); } @@ -125,8 +124,7 @@ public function testUnitConvertsToArbitraryUnit( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ): void - { + ): void { $this->assertSame($valueInArbitraryUnit, $value->toUnit($arbitraryUnit)); } @@ -146,8 +144,7 @@ public function testSerialize( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ): void - { + ): void { serialize($value); } @@ -158,8 +155,7 @@ public function testUnserialize( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ): void - { + ): void { $unserializedValue = unserialize(serialize($value)); $this->assertSame($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit)); @@ -175,8 +171,7 @@ public function testAdd( AbstractPhysicalQuantity $secondValue, $sumString, $diffString - ): void - { + ): void { if ($shouldThrowException) { $this->expectException(PhysicalQuantityMismatch::class); } @@ -198,11 +193,9 @@ public function testSubtract( AbstractPhysicalQuantity $secondValue, $sumString, $diffString - ): void - { + ): void { if ($shouldThrowException) { $this->expectException(PhysicalQuantityMismatch::class); - } $difference = $firstValue->subtract($secondValue); From 58e5ccd96da2aacb857f97ea117212645ed5e0ba Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 10:46:53 +0300 Subject: [PATCH 44/55] Create HasIECUnitsTrait.php --- source/HasIECUnitsTrait.php | 192 ++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 source/HasIECUnitsTrait.php diff --git a/source/HasIECUnitsTrait.php b/source/HasIECUnitsTrait.php new file mode 100644 index 0000000..e91d766 --- /dev/null +++ b/source/HasIECUnitsTrait.php @@ -0,0 +1,192 @@ + 'Y', + 'long_prefix' => 'yotta', + 'factor' => 1e24 + ], + [ + 'abbr_prefix' => 'Z', + 'long_prefix' => 'zetta', + 'factor' => 1e21 + ], + [ + 'abbr_prefix' => 'E', + 'long_prefix' => 'exa', + 'factor' => 1e18 + ], + [ + 'abbr_prefix' => 'P', + 'long_prefix' => 'peta', + 'factor' => 1e15 + ], + [ + 'abbr_prefix' => 'T', + 'long_prefix' => 'tera', + 'factor' => 1e12 + ], + [ + 'abbr_prefix' => 'G', + 'long_prefix' => 'giga', + 'factor' => 1e9 + ], + [ + 'abbr_prefix' => 'M', + 'long_prefix' => 'mega', + 'factor' => 1e6 + ], + [ + 'abbr_prefix' => 'k', + 'long_prefix' => 'kilo', + 'factor' => 1e3 + ], + [ + 'abbr_prefix' => '', + 'long_prefix' => '', + 'factor' => 1 + ], + ]; + + /** + * The standard set of IEC prefixes + * http://members.optus.net/alexey/prefBin.xhtml + */ + $iecPrefixes = [ + [ + 'abbr_prefix' => 'Yi', + 'long_prefix' => 'yobi', + 'factor' => 1208925819614629174706176 + ], + [ + 'abbr_prefix' => 'Zi', + 'long_prefix' => 'zebi', + 'factor' => 1180591620717411303424 + ], + [ + 'abbr_prefix' => 'Ei', + 'long_prefix' => 'exbi', + 'factor' => 1152921504606846976 + ], + [ + 'abbr_prefix' => 'Pi', + 'long_prefix' => 'pebi', + 'factor' => 1125899906842624 + ], + [ + 'abbr_prefix' => 'Ti', + 'long_prefix' => 'tebi', + 'factor' => 1099511627776 + ], + [ + 'abbr_prefix' => 'Gi', + 'long_prefix' => 'gibi', + 'factor' => 1073741824 + ], + [ + 'abbr_prefix' => 'Mi', + 'long_prefix' => 'mebi', + 'factor' => 1048576 + ], + [ + 'abbr_prefix' => 'Ki', + 'long_prefix' => 'kibi', + 'factor' => 1024 + ], + [ + 'abbr_prefix' => '', + 'long_prefix' => '', + 'factor' => 1 + ], + ]; + + // Determine the conversion factor from the no-prefix IEC unit to the physical quantity's native unit + $noPrefixToNativeUnitFactor = $siUnit->convertValueToNativeUnitOfMeasure(1) * $toBaseIecUnitFactor; + + // For each of the standard IEC prefixes, attempt to register a new unit of measure + foreach ($siPrefixes as $prefixDefinition) { + // Build a function for resolving a pattern into a unit name + $parsePattern = function ($pattern) use ($prefixDefinition) { + return strtr( + $pattern, + [ + '%p' => $prefixDefinition['abbr_prefix'], + '%P' => $prefixDefinition['long_prefix'], + '%U' => strtoupper($prefixDefinition['long_prefix']) + ] + ); + }; + + // Generate the base name of the new unit + $name = $parsePattern($namePattern); + + // Determine the factor that converts the new unit into the physical quantity's + // native unit of measure. + if (is_int($powerFactor) && $powerFactor != 0) { + $prefixDefinition['factor'] = pow($prefixDefinition['factor'], $powerFactor); + } + $toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor']; + + // Instantiate the new unit of measure + $newUnit = UnitOfMeasure::linearUnitFactory($name, $toNativeUnitFactor); + + // Generate the aliases of the new unit + foreach ($aliasPatterns as $aliasPattern) { + $newUnitAlias = $parsePattern($aliasPattern); + $newUnit->addAlias($newUnitAlias); + } + + // If the unit doesn't conflict with any of the already-existing units, register it + if (!static::unitNameOrAliasesAlreadyRegistered($newUnit)) { + static::addUnit($newUnit); + } + } + } +} From 6b175c4a424c092d7cbc00a2da438220cfc22873 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 12:14:11 +0300 Subject: [PATCH 45/55] Fixed UnitTests. --- CHANGELOG.md | 4 +-- tests/PhysicalQuantity/AccelerationTest.php | 4 +-- tests/PhysicalQuantity/AreaTest.php | 4 +-- tests/PhysicalQuantity/EnergyTest.php | 4 +-- tests/PhysicalQuantity/LengthTest.php | 6 ++--- tests/PhysicalQuantity/MassTest.php | 16 ++++++------ tests/PhysicalQuantity/PowerTest.php | 4 +-- tests/PhysicalQuantity/StorageTest.php | 17 +++++++------ tests/PhysicalQuantity/TemperatureTest.php | 28 ++++++++++----------- tests/PhysicalQuantity/VelocityTest.php | 8 +++--- tests/PhysicalQuantity/VolumeFlowTest.php | 23 +++++++++-------- tests/PhysicalQuantity/VolumeTest.php | 20 +++++++-------- 12 files changed, 70 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 540e019..9ea690e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ -## v2.2.1 (xxx) +## v2.2.1 (November 2nd, 2023) - Drop support of php < 7.4 -## v2.2.0 (Feb 17th, 2021) +## v2.2.0 (February 17th, 2021) - Added support for php 8.0 - Added option power factor for Si factor, ie for square (2) and cubic (3) - Added new Physical Quantity volume flow with commonly used units diff --git a/tests/PhysicalQuantity/AccelerationTest.php b/tests/PhysicalQuantity/AccelerationTest.php index 5732ef2..76bf800 100644 --- a/tests/PhysicalQuantity/AccelerationTest.php +++ b/tests/PhysicalQuantity/AccelerationTest.php @@ -21,13 +21,13 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface return new Acceleration(1, 'm/s^2'); } - public function testToKiloMeterPerSecond() + public function testToKiloMeterPerSecond(): void { $value = new Acceleration(1000, 'm/s^2'); $this->assertEquals(1, $value->toUnit('km/s^2')); } - public function testToMilliMeterPerSecond() + public function testToMilliMeterPerSecond(): void { $value = new Acceleration(1, 'm/s^2'); $this->assertEquals(1000, $value->toUnit('mm/s^2')); diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 260245b..9d8f3dc 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -199,7 +199,7 @@ public function testToDecare(): void $this->assertEquals(1, $area->toUnit('decare')); } - public function testToDecimeterSquare() + public function testToDecimeterSquare(): void { $area = new Area(1, 'm^2'); $this->assertEquals(100, $area->toUnit('dm^2')); @@ -207,7 +207,7 @@ public function testToDecimeterSquare() $this->assertEquals(10000, $area->toUnit('dm^2')); } - public function testToMillimeterSquare() + public function testToMillimeterSquare(): void { $area = new Area(1, 'm^2'); $this->assertEquals(1e6, $area->toUnit('mm^2')); diff --git a/tests/PhysicalQuantity/EnergyTest.php b/tests/PhysicalQuantity/EnergyTest.php index 1a46534..bbbba7f 100644 --- a/tests/PhysicalQuantity/EnergyTest.php +++ b/tests/PhysicalQuantity/EnergyTest.php @@ -102,13 +102,13 @@ public function testToJoule(): void $this->assertEquals(3600, $quantity->toUnit('joule')); } - public function testToMegaCal() + public function testToMegaCal(): void { $quantity = new Energy(1, 'kWh'); $this->assertEquals(0.8604206500956023, $quantity->toUnit('Mcal')); } - public function testToMegaElectronvolt() + public function testToMegaElectronvolt(): void { $quantity = new Energy(1, 'kWh'); $this->assertEquals(2.2469432853179728e19, $quantity->toUnit('MeV')); diff --git a/tests/PhysicalQuantity/LengthTest.php b/tests/PhysicalQuantity/LengthTest.php index db4dd61..5901248 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -245,19 +245,19 @@ public function testToAstronomicalUnit(): void $this->assertEquals(1.0026880683402668, $quantity->toUnit('AU')); } - public function testToLightYear() + public function testToLightYear(): void { $quantity = new Length(150000000000000, 'km'); $this->assertEquals(15.855012510369232, $quantity->toUnit('ly')); } - public function testToParSec() + public function testToParSec(): void { $quantity = new Length(150000000000000, 'km'); $this->assertEquals(4.861168934166548, $quantity->toUnit('pc')); } - public function testToMilliParSec() + public function testToMilliParSec(): void { $quantity = new Length(150000000000000, 'km'); $this->assertEquals(4861.168934166548, $quantity->toUnit('mpc')); diff --git a/tests/PhysicalQuantity/MassTest.php b/tests/PhysicalQuantity/MassTest.php index 8126f41..9387ecf 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -131,27 +131,27 @@ public function testToStones(): void $this->assertEquals(1, $quantity->toUnit('st')); } - public function testToHundredweight() + public function testToHundredweight(): void { $quantity = new Mass(5, 'kg'); - $this->assertEquals(0.098420653, $quantity->toUnit('cwt'), '', 0.00000001); + $this->assertEqualsWithDelta(0.098420653, $quantity->toUnit('cwt'), '', 0.00000001); } - public function testToUSshortTon() + public function testToUSshortTon(): void { $quantity = new Mass(5, 'kg'); - $this->assertEquals(0.0055115566, $quantity->toUnit('ust'), '', 0.000000001); + $this->assertEqualsWithDelta(0.0055115566, $quantity->toUnit('ust'), '', 0.000000001); } - public function testToUSlongTon() + public function testToUSlongTon(): void { $quantity = new Mass(5, 'kg'); - $this->assertEquals(0.0049210326, $quantity->toUnit('ukt'), '', 0.000000001); + $this->assertEqualsWithDelta(0.0049210326, $quantity->toUnit('ukt'), '', 0.000000001); } - public function testToPicul() + public function testToPicul(): void { $quantity = new Mass(5, 'kg'); - $this->assertEquals(0.08267335, $quantity->toUnit('picul'), '', 0.00000001); + $this->assertEqualsWithDelta(0.08267335, $quantity->toUnit('picul'), '', 0.00000001); } } diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index 3a52a1a..455fd5d 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -51,7 +51,7 @@ public function testToWatt(): void $this->assertEquals(1000, $quantity->toUnit('W')); } - public function testToDecibelsMilliWatt() + public function testToDecibelsMilliWatt(): void { $quantity = new Power(0.01, 'mW'); $this->assertEquals(-20, $quantity->toUnit('dBm')); @@ -61,7 +61,7 @@ public function testToDecibelsMilliWatt() $this->assertEquals(70, $quantity->toUnit('dBm')); } - public function testFromDecibelsMilliWatt() + public function testFromDecibelsMilliWatt(): void { $quantity = new Power(-20, 'dBm'); $this->assertEquals(0.00001, $quantity->toUnit('W')); diff --git a/tests/PhysicalQuantity/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php index da1c574..9c55558 100644 --- a/tests/PhysicalQuantity/StorageTest.php +++ b/tests/PhysicalQuantity/StorageTest.php @@ -3,46 +3,47 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Storage; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class StorageTest extends AbstractPhysicalQuantityTestCase { - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Storage(1, 'byte'); } - public function testByteToByte() + public function testByteToByte(): void { $this->assertEquals(1, (new Storage(1, 'byte'))->toUnit('B')); } - public function testByteToBit() + public function testByteToBit(): void { $this->assertEquals(40, (new Storage(5, 'byte'))->toUnit('bit')); } - public function testBitToByte() + public function testBitToByte(): void { $this->assertEquals(1.25, (new Storage(10, 'b'))->toUnit('B')); } - public function testMegabyteToByte() + public function testMegabyteToByte(): void { $this->assertEquals(1048576, (new Storage(1, 'MB'))->toUnit('B')); } - public function testGigabyteToByte() + public function testGigabyteToByte(): void { $this->assertEquals(1073741824, (new Storage(1, 'gigabyte'))->toUnit('byte')); } - public function testTerabyteToByte() + public function testTerabyteToByte(): void { $this->assertEquals(1099511627776, (new Storage(1, 'TB'))->toUnit('byte')); } - public function testPetabyteToByte() + public function testPetabyteToByte(): void { $this->assertEquals(3377699720527872, (new Storage(3, 'PB'))->toUnit('byte')); } diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index f8a65cf..7d9c49a 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -85,44 +85,44 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface return new Temperature(1, 'K'); } - public function testToCelsius() + public function testToCelsius(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(40, $value->toUnit('C'), '', 0.000001); + $this->assertEqualsWithDelta(40, $value->toUnit('C'), '', 0.000001); } - public function testToFahrenheit() + public function testToFahrenheit(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(104, $value->toUnit('F'), '', 0.000001); + $this->assertEqualsWithDelta(104, $value->toUnit('F'), '', 0.000001); } - public function testToRankine() + public function testToRankine(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(563.67, $value->toUnit('Ra'), '', 0.000001); + $this->assertEqualsWithDelta(563.67, $value->toUnit('Ra'), '', 0.000001); } - public function testToDelisle() + public function testToDelisle(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(90, $value->toUnit('D'), '', 0.000001); + $this->assertEqualsWithDelta(90, $value->toUnit('D'), '', 0.000001); } - public function testToNewton() + public function testToNewton(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(13.2, $value->toUnit('N'), '', 0.000001); + $this->assertEqualsWithDelta(13.2, $value->toUnit('N'), '', 0.000001); } - public function testToReaumur() + public function testToReaumur(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(32, $value->toUnit('Re'), '', 0.000001); + $this->assertEqualsWithDelta(32, $value->toUnit('Re'), '', 0.000001); } - public function testToRomer() + public function testToRomer(): void { $value = new Temperature(313.15, 'K'); - $this->assertEquals(28.5, $value->toUnit('Ro'), '', 0.000001); + $this->assertEqualsWithDelta(28.5, $value->toUnit('Ro'), '', 0.000001); } } diff --git a/tests/PhysicalQuantity/VelocityTest.php b/tests/PhysicalQuantity/VelocityTest.php index ffaa9bc..30df26b 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -38,7 +38,7 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface public function testToKilometersPerHour(): void { $speed = new Velocity(1, 'km/h'); - $this->assertEquals(0.277778, $speed->toUnit('m/s'), '', 0.000001); + $this->assertEqualsWithDelta(0.277778, $speed->toUnit('m/s'), '', 0.000001); } public function testToFeetPerSecond(): void @@ -50,7 +50,7 @@ public function testToFeetPerSecond(): void public function testToKmPerHour(): void { $speed = new Velocity(2, 'mph'); - $this->assertEquals(3.218688, $speed->toUnit('km/h'), '', 0.000001); + $this->assertEqualsWithDelta(3.218688, $speed->toUnit('km/h'), '', 0.000001); } public function testToKnot(): void @@ -59,9 +59,9 @@ public function testToKnot(): void $this->assertEquals(3.8876923435786983, $speed->toUnit('knot')); } - public function testToMach() + public function testToMach(): void { $speed = new Velocity(1000, 'm/s'); - $this->assertEquals(2.906977, $speed->toUnit('mach'), '', 0.000001); + $this->assertEqualsWithDelta(2.906977, $speed->toUnit('mach'), '', 0.000001); } } diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index 1f02ba9..34f28f0 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -3,10 +3,11 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\VolumeFlow; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class VolumeFlowTest extends AbstractPhysicalQuantityTestCase { - protected $supportedUnitsWithAliases = [ + protected array $supportedUnitsWithAliases = [ 'm^3/s', 'm³/s', 'cubic meter per second', @@ -331,12 +332,12 @@ class VolumeFlowTest extends AbstractPhysicalQuantityTestCase 'us gal/d' ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new VolumeFlow(1, 'm^3/s'); } - public function testToCubicDecimeterSecond() + public function testToCubicDecimeterSecond(): void { $area = new VolumeFlow(1, 'm^3/s'); /* @@ -353,15 +354,15 @@ public function testToCubicDecimeterSecond() } - public function testToCubicMillimeterSecond() + public function testToCubicMillimeterSecond(): void { $area = new VolumeFlow(1, 'm^3/s'); - $this->assertEquals(1e9, $area->toUnit('mm^3/s'), '', 0.000001); + $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3/s'), '', 0.000001); $area = new VolumeFlow(100, 'm^3/s'); $this->assertEquals(1e11, $area->toUnit('mm^3/s')); } - public function testToLitresSecond() + public function testToLitresSecond(): void { $area = new VolumeFlow(1, 'm^3/s'); $this->assertEquals(1000, $area->toUnit('l/s')); @@ -369,7 +370,7 @@ public function testToLitresSecond() $this->assertEquals(100000, $area->toUnit('l/s')); } - public function testToMilliLitresSecond() + public function testToMilliLitresSecond(): void { $area = new VolumeFlow(1, 'l/s'); $this->assertEquals(1000, $area->toUnit('ml/s')); @@ -377,7 +378,7 @@ public function testToMilliLitresSecond() $this->assertEquals(100000, $area->toUnit('ml/s')); } - public function testToHectoLitresSecond() + public function testToHectoLitresSecond(): void { $area = new VolumeFlow(1, 'l/s'); $this->assertEquals(0.01, $area->toUnit('hl/s')); @@ -385,7 +386,7 @@ public function testToHectoLitresSecond() $this->assertEquals(1, $area->toUnit('hl/s')); } - public function testToKiloLitresSecond() + public function testToKiloLitresSecond(): void { $area = new VolumeFlow(1, 'l/s'); $this->assertEquals(0.001, $area->toUnit('kl/s')); @@ -396,8 +397,8 @@ public function testToKiloLitresSecond() public function testToCFM() { $area = new VolumeFlow(1, 'CMM'); - $this->assertEquals(35.314667, $area->toUnit('CFM'), '', 0.000001); + $this->assertEqualsWithDelta(35.314667, $area->toUnit('CFM'), '', 0.000001); $area = new VolumeFlow(100, 'm^3/s'); - $this->assertEquals(211888.000328, $area->toUnit('CFM'), '', 0.000001); + $this->assertEqualsWithDelta(211888.000328, $area->toUnit('CFM'), '', 0.000001); } } diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 4be4708..c247a8c 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -90,7 +90,7 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface return new Volume(1, 'm^3'); } - public function testToCubicDecimeter() + public function testToCubicDecimeter(): void { $area = new Volume(1, 'm^3'); $this->assertEquals(1000, $area->toUnit('dm^3')); @@ -98,15 +98,15 @@ public function testToCubicDecimeter() $this->assertEquals(100000, $area->toUnit('dm^3')); } - public function testToCubicMillimeter() + public function testToCubicMillimeter(): void { $area = new Volume(1, 'm^3'); - $this->assertEquals(1e9, $area->toUnit('mm^3'), '', 0.000001); + $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3'), '', 0.000001); $area = new Volume(100, 'm^3'); $this->assertEquals(1e11, $area->toUnit('mm^3')); } - public function testToLitres() + public function testToLitres(): void { $area = new Volume(1, 'm^3'); $this->assertEquals(1000, $area->toUnit('l')); @@ -114,7 +114,7 @@ public function testToLitres() $this->assertEquals(100000, $area->toUnit('l')); } - public function testToMilliLitres() + public function testToMilliLitres(): void { $area = new Volume(1, 'l'); $this->assertEquals(1000, $area->toUnit('ml')); @@ -122,7 +122,7 @@ public function testToMilliLitres() $this->assertEquals(100000, $area->toUnit('ml')); } - public function testToHectoLitres() + public function testToHectoLitres(): void { $area = new Volume(1, 'l'); $this->assertEquals(0.01, $area->toUnit('hl')); @@ -130,7 +130,7 @@ public function testToHectoLitres() $this->assertEquals(1, $area->toUnit('hl')); } - public function testToKiloLitres() + public function testToKiloLitres(): void { $area = new Volume(1, 'l'); $this->assertEquals(0.001, $area->toUnit('kl')); @@ -138,11 +138,11 @@ public function testToKiloLitres() $this->assertEquals(0.1, $area->toUnit('kl')); } - public function testToGallon() + public function testToGallon(): void { $area = new Volume(1, 'm^3'); - $this->assertEquals(264.172051, $area->toUnit('gal'), '', 0.000001); + $this->assertEqualsWithDelta(264.172051, $area->toUnit('gal'), '', 0.000001); $area = new Volume(100, 'm^3'); - $this->assertEquals(26417.205124, $area->toUnit('gal'), '', 0.000001); + $this->assertEqualsWithDelta(26417.205124, $area->toUnit('gal'), '', 0.000001); } } From d2d46cb707c049dd0efbcb9d36dc447400fca610 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 12:53:38 +0300 Subject: [PATCH 46/55] Fixed assertEqualsWithDelta() assets. --- tests/PhysicalQuantity/MassTest.php | 8 ++++---- tests/PhysicalQuantity/TemperatureTest.php | 14 +++++++------- tests/PhysicalQuantity/VelocityTest.php | 6 +++--- tests/PhysicalQuantity/VolumeFlowTest.php | 6 +++--- tests/PhysicalQuantity/VolumeTest.php | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/PhysicalQuantity/MassTest.php b/tests/PhysicalQuantity/MassTest.php index 9387ecf..6936fda 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -134,24 +134,24 @@ public function testToStones(): void public function testToHundredweight(): void { $quantity = new Mass(5, 'kg'); - $this->assertEqualsWithDelta(0.098420653, $quantity->toUnit('cwt'), '', 0.00000001); + $this->assertEqualsWithDelta(0.098420653, (float)$quantity->toUnit('cwt'), 0.00000001); } public function testToUSshortTon(): void { $quantity = new Mass(5, 'kg'); - $this->assertEqualsWithDelta(0.0055115566, $quantity->toUnit('ust'), '', 0.000000001); + $this->assertEqualsWithDelta(0.0055115566, $quantity->toUnit('ust'), 0.000000001); } public function testToUSlongTon(): void { $quantity = new Mass(5, 'kg'); - $this->assertEqualsWithDelta(0.0049210326, $quantity->toUnit('ukt'), '', 0.000000001); + $this->assertEqualsWithDelta(0.0049210326, $quantity->toUnit('ukt'), 0.000000001); } public function testToPicul(): void { $quantity = new Mass(5, 'kg'); - $this->assertEqualsWithDelta(0.08267335, $quantity->toUnit('picul'), '', 0.00000001); + $this->assertEqualsWithDelta(0.08267335, $quantity->toUnit('picul'), 0.00000001); } } diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index 7d9c49a..f9dd032 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -88,41 +88,41 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface public function testToCelsius(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(40, $value->toUnit('C'), '', 0.000001); + $this->assertEqualsWithDelta(40, $value->toUnit('C'), 0.000001); } public function testToFahrenheit(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(104, $value->toUnit('F'), '', 0.000001); + $this->assertEqualsWithDelta(104, $value->toUnit('F'), 0.000001); } public function testToRankine(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(563.67, $value->toUnit('Ra'), '', 0.000001); + $this->assertEqualsWithDelta(563.67, $value->toUnit('Ra'), 0.000001); } public function testToDelisle(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(90, $value->toUnit('D'), '', 0.000001); + $this->assertEqualsWithDelta(90, $value->toUnit('D'), 0.000001); } public function testToNewton(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(13.2, $value->toUnit('N'), '', 0.000001); + $this->assertEqualsWithDelta(13.2, $value->toUnit('N'), 0.000001); } public function testToReaumur(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(32, $value->toUnit('Re'), '', 0.000001); + $this->assertEqualsWithDelta(32, $value->toUnit('Re'), 0.000001); } public function testToRomer(): void { $value = new Temperature(313.15, 'K'); - $this->assertEqualsWithDelta(28.5, $value->toUnit('Ro'), '', 0.000001); + $this->assertEqualsWithDelta(28.5, $value->toUnit('Ro'), 0.000001); } } diff --git a/tests/PhysicalQuantity/VelocityTest.php b/tests/PhysicalQuantity/VelocityTest.php index 30df26b..6b94a96 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -38,7 +38,7 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface public function testToKilometersPerHour(): void { $speed = new Velocity(1, 'km/h'); - $this->assertEqualsWithDelta(0.277778, $speed->toUnit('m/s'), '', 0.000001); + $this->assertEqualsWithDelta(0.277778, $speed->toUnit('m/s'), 0.000001); } public function testToFeetPerSecond(): void @@ -50,7 +50,7 @@ public function testToFeetPerSecond(): void public function testToKmPerHour(): void { $speed = new Velocity(2, 'mph'); - $this->assertEqualsWithDelta(3.218688, $speed->toUnit('km/h'), '', 0.000001); + $this->assertEqualsWithDelta(3.218688, $speed->toUnit('km/h'), 0.000001); } public function testToKnot(): void @@ -62,6 +62,6 @@ public function testToKnot(): void public function testToMach(): void { $speed = new Velocity(1000, 'm/s'); - $this->assertEqualsWithDelta(2.906977, $speed->toUnit('mach'), '', 0.000001); + $this->assertEqualsWithDelta(2.906977, $speed->toUnit('mach'), 0.000001); } } diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index 34f28f0..9e9c529 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -357,7 +357,7 @@ public function testToCubicDecimeterSecond(): void public function testToCubicMillimeterSecond(): void { $area = new VolumeFlow(1, 'm^3/s'); - $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3/s'), '', 0.000001); + $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3/s'), 0.000001); $area = new VolumeFlow(100, 'm^3/s'); $this->assertEquals(1e11, $area->toUnit('mm^3/s')); } @@ -397,8 +397,8 @@ public function testToKiloLitresSecond(): void public function testToCFM() { $area = new VolumeFlow(1, 'CMM'); - $this->assertEqualsWithDelta(35.314667, $area->toUnit('CFM'), '', 0.000001); + $this->assertEqualsWithDelta(35.314667, $area->toUnit('CFM'), 0.000001); $area = new VolumeFlow(100, 'm^3/s'); - $this->assertEqualsWithDelta(211888.000328, $area->toUnit('CFM'), '', 0.000001); + $this->assertEqualsWithDelta(211888.000328, $area->toUnit('CFM'), 0.000001); } } diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index c247a8c..e8d582a 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -101,7 +101,7 @@ public function testToCubicDecimeter(): void public function testToCubicMillimeter(): void { $area = new Volume(1, 'm^3'); - $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3'), '', 0.000001); + $this->assertEqualsWithDelta(1e9, $area->toUnit('mm^3'), 0.000001); $area = new Volume(100, 'm^3'); $this->assertEquals(1e11, $area->toUnit('mm^3')); } @@ -141,8 +141,8 @@ public function testToKiloLitres(): void public function testToGallon(): void { $area = new Volume(1, 'm^3'); - $this->assertEqualsWithDelta(264.172051, $area->toUnit('gal'), '', 0.000001); + $this->assertEqualsWithDelta(264.172051, $area->toUnit('gal'), 0.000001); $area = new Volume(100, 'm^3'); - $this->assertEqualsWithDelta(26417.205124, $area->toUnit('gal'), '', 0.000001); + $this->assertEqualsWithDelta(26417.205124, $area->toUnit('gal'), 0.000001); } } From 1c4c0cb01bbc7117da18fad5f58479c1a2f67511 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 13:03:05 +0300 Subject: [PATCH 47/55] More fixes for assertEqualsWithDelta() assets. --- tests/PhysicalQuantity/AreaTest.php | 2 +- tests/PhysicalQuantity/LengthTest.php | 2 +- tests/PhysicalQuantity/VolumeFlowTest.php | 4 ++-- tests/PhysicalQuantity/VolumeTest.php | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 9d8f3dc..7f43102 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -202,7 +202,7 @@ public function testToDecare(): void public function testToDecimeterSquare(): void { $area = new Area(1, 'm^2'); - $this->assertEquals(100, $area->toUnit('dm^2')); + $this->assertEqualsWithDelta(100, $area->toUnit('dm^2'), 0.000001); $area = new Area(100, 'm^2'); $this->assertEquals(10000, $area->toUnit('dm^2')); } diff --git a/tests/PhysicalQuantity/LengthTest.php b/tests/PhysicalQuantity/LengthTest.php index 5901248..4a7dae0 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -224,7 +224,7 @@ public function testToMegameters(): void public function testToInches(): void { $quantity = new Length(2, 'ft'); - $this->assertEquals(24, $quantity->toUnit('in')); + $this->assertEqualsWithDelta(24, $quantity->toUnit('in'), 0.000001); } public function testToNauticalMiles(): void diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index 9e9c529..65c0b55 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -348,7 +348,7 @@ public function testToCubicDecimeterSecond(): void } } */ - $this->assertEquals(1000, $area->toUnit('dm^3/s')); + $this->assertEqualsWithDelta(1000, $area->toUnit('dm^3/s'), 0.000001); $area = new VolumeFlow(100, 'm^3/s'); $this->assertEquals(100000, $area->toUnit('dm^3/s')); } @@ -373,7 +373,7 @@ public function testToLitresSecond(): void public function testToMilliLitresSecond(): void { $area = new VolumeFlow(1, 'l/s'); - $this->assertEquals(1000, $area->toUnit('ml/s')); + $this->assertEqualsWithDelta(1000, $area->toUnit('ml/s'), 0.000001); $area = new VolumeFlow(100, 'l/s'); $this->assertEquals(100000, $area->toUnit('ml/s')); } diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index e8d582a..95cc80f 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -93,7 +93,7 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface public function testToCubicDecimeter(): void { $area = new Volume(1, 'm^3'); - $this->assertEquals(1000, $area->toUnit('dm^3')); + $this->assertEqualsWithDelta(1000, $area->toUnit('dm^3'), 0.000001); $area = new Volume(100, 'm^3'); $this->assertEquals(100000, $area->toUnit('dm^3')); } @@ -117,7 +117,7 @@ public function testToLitres(): void public function testToMilliLitres(): void { $area = new Volume(1, 'l'); - $this->assertEquals(1000, $area->toUnit('ml')); + $this->assertEqualsWithDelta(1000, $area->toUnit('ml'), 0.000001); $area = new Volume(100, 'l'); $this->assertEquals(100000, $area->toUnit('ml')); } From 819ab69a7fb8ddfb80268000451c4c162ea74867 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 13:17:52 +0300 Subject: [PATCH 48/55] More fixes for assertEqualsWithDelta() assets. --- tests/PhysicalQuantity/AreaTest.php | 2 +- tests/PhysicalQuantity/VolumeFlowTest.php | 4 ++-- tests/PhysicalQuantity/VolumeTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 7f43102..951b1e1 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -204,7 +204,7 @@ public function testToDecimeterSquare(): void $area = new Area(1, 'm^2'); $this->assertEqualsWithDelta(100, $area->toUnit('dm^2'), 0.000001); $area = new Area(100, 'm^2'); - $this->assertEquals(10000, $area->toUnit('dm^2')); + $this->assertEqualsWithDelta(10000, $area->toUnit('dm^2'), 0.000001); } public function testToMillimeterSquare(): void diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index 65c0b55..59bc8de 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -350,7 +350,7 @@ public function testToCubicDecimeterSecond(): void */ $this->assertEqualsWithDelta(1000, $area->toUnit('dm^3/s'), 0.000001); $area = new VolumeFlow(100, 'm^3/s'); - $this->assertEquals(100000, $area->toUnit('dm^3/s')); + $this->assertEqualsWithDelta(100000, $area->toUnit('dm^3/s'), 0.000001); } @@ -375,7 +375,7 @@ public function testToMilliLitresSecond(): void $area = new VolumeFlow(1, 'l/s'); $this->assertEqualsWithDelta(1000, $area->toUnit('ml/s'), 0.000001); $area = new VolumeFlow(100, 'l/s'); - $this->assertEquals(100000, $area->toUnit('ml/s')); + $this->assertEqualsWithDelta(100000, $area->toUnit('ml/s'), 0.000001); } public function testToHectoLitresSecond(): void diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 95cc80f..e7399e6 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -95,7 +95,7 @@ public function testToCubicDecimeter(): void $area = new Volume(1, 'm^3'); $this->assertEqualsWithDelta(1000, $area->toUnit('dm^3'), 0.000001); $area = new Volume(100, 'm^3'); - $this->assertEquals(100000, $area->toUnit('dm^3')); + $this->assertEqualsWithDelta(100000, $area->toUnit('dm^3'), 0.000001); } public function testToCubicMillimeter(): void @@ -119,7 +119,7 @@ public function testToMilliLitres(): void $area = new Volume(1, 'l'); $this->assertEqualsWithDelta(1000, $area->toUnit('ml'), 0.000001); $area = new Volume(100, 'l'); - $this->assertEquals(100000, $area->toUnit('ml')); + $this->assertEqualsWithDelta(100000, $area->toUnit('ml'), 0.000001); } public function testToHectoLitres(): void From 55184edd0e7a408f3f4442107306637f66e988de Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 13:30:50 +0300 Subject: [PATCH 49/55] Reduce minimum php to 7.2. --- .github/workflows/ci.yaml | 6 ++++-- composer.json | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c1606df..1e721c7 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,12 +9,14 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.4'] + php: ['7.2'] stability: [ prefer-stable ] experimental: [false] include: - - php: '7.4' + - php: '7.2' stability: prefer-lowest + - php: '7.3' + - php: '7.4' - php: '8.0' - php: '8.1' - php: '8.2' diff --git a/composer.json b/composer.json index 925b9c8..dfca5dd 100644 --- a/composer.json +++ b/composer.json @@ -27,11 +27,11 @@ }, "require": { - "php": ">=7.4" + "php": ">=7.2" }, "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^8.5", "squizlabs/php_codesniffer": "^3.7" }, From e76538bb6d9117e35478b2bfe51956d69002cdea Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 13:51:24 +0300 Subject: [PATCH 50/55] Reduce minimum php to 7.2 for UnitTests. --- tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php | 2 +- tests/PhysicalQuantity/AccelerationTest.php | 2 +- tests/PhysicalQuantity/AngleTest.php | 2 +- tests/PhysicalQuantity/AreaTest.php | 2 +- tests/PhysicalQuantity/ElectricCurrentTest.php | 2 +- tests/PhysicalQuantity/EnergyTest.php | 2 +- tests/PhysicalQuantity/LengthTest.php | 2 +- tests/PhysicalQuantity/LuminousIntensityTest.php | 2 +- tests/PhysicalQuantity/MassTest.php | 2 +- tests/PhysicalQuantity/PowerTest.php | 2 +- tests/PhysicalQuantity/PressureTest.php | 2 +- tests/PhysicalQuantity/QuantityTest.php | 2 +- tests/PhysicalQuantity/SolidAngleTest.php | 2 +- tests/PhysicalQuantity/TemperatureTest.php | 2 +- tests/PhysicalQuantity/TimeTest.php | 2 +- tests/PhysicalQuantity/VelocityTest.php | 2 +- tests/PhysicalQuantity/VolumeFlowTest.php | 2 +- tests/PhysicalQuantity/VolumeTest.php | 2 +- 18 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php index d956eb3..76b0ea0 100644 --- a/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php +++ b/tests/PhysicalQuantity/AbstractPhysicalQuantityTestCase.php @@ -10,7 +10,7 @@ */ abstract class AbstractPhysicalQuantityTestCase extends TestCase { - protected array $supportedUnitsWithAliases = []; + protected $supportedUnitsWithAliases = []; /** * Verify that the object instantiates without error. diff --git a/tests/PhysicalQuantity/AccelerationTest.php b/tests/PhysicalQuantity/AccelerationTest.php index 76bf800..7a891f4 100644 --- a/tests/PhysicalQuantity/AccelerationTest.php +++ b/tests/PhysicalQuantity/AccelerationTest.php @@ -7,7 +7,7 @@ class AccelerationTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm/s^2', 'm/s²', 'meter per second squared', diff --git a/tests/PhysicalQuantity/AngleTest.php b/tests/PhysicalQuantity/AngleTest.php index 281c9d2..3d43286 100644 --- a/tests/PhysicalQuantity/AngleTest.php +++ b/tests/PhysicalQuantity/AngleTest.php @@ -7,7 +7,7 @@ class AngleTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'rad', 'radian', 'radians', diff --git a/tests/PhysicalQuantity/AreaTest.php b/tests/PhysicalQuantity/AreaTest.php index 951b1e1..cd5ca1d 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -7,7 +7,7 @@ class AreaTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm^2', 'm²', 'meter squared', diff --git a/tests/PhysicalQuantity/ElectricCurrentTest.php b/tests/PhysicalQuantity/ElectricCurrentTest.php index 0c3d65a..e521a80 100644 --- a/tests/PhysicalQuantity/ElectricCurrentTest.php +++ b/tests/PhysicalQuantity/ElectricCurrentTest.php @@ -7,7 +7,7 @@ class ElectricCurrentTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'A', 'amp', 'amps', diff --git a/tests/PhysicalQuantity/EnergyTest.php b/tests/PhysicalQuantity/EnergyTest.php index bbbba7f..c0c9125 100644 --- a/tests/PhysicalQuantity/EnergyTest.php +++ b/tests/PhysicalQuantity/EnergyTest.php @@ -7,7 +7,7 @@ class EnergyTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'Wh', 'watt hour', 'watt hours', diff --git a/tests/PhysicalQuantity/LengthTest.php b/tests/PhysicalQuantity/LengthTest.php index 4a7dae0..1b8912a 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -7,7 +7,7 @@ class LengthTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm', 'meter', 'meters', diff --git a/tests/PhysicalQuantity/LuminousIntensityTest.php b/tests/PhysicalQuantity/LuminousIntensityTest.php index b62ba6d..1bcbfde 100644 --- a/tests/PhysicalQuantity/LuminousIntensityTest.php +++ b/tests/PhysicalQuantity/LuminousIntensityTest.php @@ -7,7 +7,7 @@ class LuminousIntensityTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'cd', 'candela', 'Ycd', diff --git a/tests/PhysicalQuantity/MassTest.php b/tests/PhysicalQuantity/MassTest.php index 6936fda..91b3a92 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -7,7 +7,7 @@ class MassTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'kg', 'kilogram', 'kilograms', diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index 455fd5d..b490435 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -7,7 +7,7 @@ class PowerTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'W', 'watt', 'watts', diff --git a/tests/PhysicalQuantity/PressureTest.php b/tests/PhysicalQuantity/PressureTest.php index a79d07b..00e2be6 100644 --- a/tests/PhysicalQuantity/PressureTest.php +++ b/tests/PhysicalQuantity/PressureTest.php @@ -7,7 +7,7 @@ class PressureTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'Pa', 'pascal', 'YPa', diff --git a/tests/PhysicalQuantity/QuantityTest.php b/tests/PhysicalQuantity/QuantityTest.php index 958c5c5..ba943e7 100644 --- a/tests/PhysicalQuantity/QuantityTest.php +++ b/tests/PhysicalQuantity/QuantityTest.php @@ -7,7 +7,7 @@ class QuantityTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'mol', 'mole', 'moles', diff --git a/tests/PhysicalQuantity/SolidAngleTest.php b/tests/PhysicalQuantity/SolidAngleTest.php index 8c920e6..008af47 100644 --- a/tests/PhysicalQuantity/SolidAngleTest.php +++ b/tests/PhysicalQuantity/SolidAngleTest.php @@ -7,7 +7,7 @@ class SolidAngleTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'sr', 'steradian', 'steradians', diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index f9dd032..57c7003 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -7,7 +7,7 @@ class TemperatureTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'K', '°K', 'kelvin', diff --git a/tests/PhysicalQuantity/TimeTest.php b/tests/PhysicalQuantity/TimeTest.php index 9a677f2..019f678 100644 --- a/tests/PhysicalQuantity/TimeTest.php +++ b/tests/PhysicalQuantity/TimeTest.php @@ -7,7 +7,7 @@ class TimeTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 's', 'sec', 'secs', diff --git a/tests/PhysicalQuantity/VelocityTest.php b/tests/PhysicalQuantity/VelocityTest.php index 6b94a96..a42d484 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -7,7 +7,7 @@ class VelocityTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm/s', 'meters/sec', 'meters per second', diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php index 59bc8de..ee9980a 100644 --- a/tests/PhysicalQuantity/VolumeFlowTest.php +++ b/tests/PhysicalQuantity/VolumeFlowTest.php @@ -7,7 +7,7 @@ class VolumeFlowTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm^3/s', 'm³/s', 'cubic meter per second', diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index e7399e6..384af3e 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -7,7 +7,7 @@ class VolumeTest extends AbstractPhysicalQuantityTestCase { - protected array $supportedUnitsWithAliases = [ + protected $supportedUnitsWithAliases = [ 'm^3', 'm³', 'cubic meter', From 7976b059fc0b7710efdaf8e6565b346e58a99932 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Thu, 2 Nov 2023 13:56:26 +0300 Subject: [PATCH 51/55] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ea690e..4c47550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## v2.2.1 (November 2nd, 2023) -- Drop support of php < 7.4 +- Drop support of php < 7.2 ## v2.2.0 (February 17th, 2021) - Added support for php 8.0 From 84e439831b28773eb1b3c242613f14ce18ab2686 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 3 Nov 2023 16:21:26 +0300 Subject: [PATCH 52/55] Removed pow() function --- source/AbstractPhysicalQuantity.php | 2 +- source/HasSIUnitsTrait.php | 18 +++++++++--------- source/PhysicalQuantity/Power.php | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/source/AbstractPhysicalQuantity.php b/source/AbstractPhysicalQuantity.php index e697b98..5c37e25 100644 --- a/source/AbstractPhysicalQuantity.php +++ b/source/AbstractPhysicalQuantity.php @@ -26,7 +26,7 @@ abstract class AbstractPhysicalQuantity implements PhysicalQuantityInterface */ private static function buildUnitCacheKey($unit) { - return get_called_class() . '#' . $unit; + return static::class . '#' . $unit; } /** diff --git a/source/HasSIUnitsTrait.php b/source/HasSIUnitsTrait.php index 5b2f46f..acc6a87 100644 --- a/source/HasSIUnitsTrait.php +++ b/source/HasSIUnitsTrait.php @@ -33,17 +33,17 @@ trait HasSIUnitsTrait * then pass 1e-3 in the 2nd parameter to indicate that a gram is 1/1000 of the given unit. * * @param UnitOfMeasure $siUnit A unit in this physical quantity that is an SI unit of measure - * @param integer $toBaseSiUnitFactor The power-of-ten factor that converts the given SI unit into the not-prefixed SI base unit (ie 1e-3 for kilograms) - * @param string $namePattern The pattern to apply to the base unit's name to generate a new SI unit name + * @param float $toBaseSiUnitFactor The power-of-ten factor that converts the given SI unit into the not-prefixed SI base unit (ie 1e-3 for kilograms) + * @param string $namePattern The pattern to apply to the base unit's name to generate a new SI unit name * @param array $aliasPatterns The collection of alias patterns to use in generating a new SI unit's aliases - * @param integer $powerFactor Use power factor for squares, qubic and other multiplication of SI factor (ie. square is 2, qubic is 3) + * @param integer|null $powerFactor Use power factor for squares, qubic and other multiplication of SI factor (ie. square is 2, qubic is 3) */ protected static function addMissingSIPrefixedUnits( UnitOfMeasure $siUnit, - $toBaseSiUnitFactor, - $namePattern, - array $aliasPatterns = [], - $powerFactor = null + float $toBaseSiUnitFactor, + string $namePattern, + array $aliasPatterns = [], + int $powerFactor = null ) { /** * The standard set of SI prefixes @@ -178,8 +178,8 @@ protected static function addMissingSIPrefixedUnits( // Determine the factor that converts the new unit into the physical quantity's // native unit of measure. - if (is_int($powerFactor) && $powerFactor != 0) { - $prefixDefinition['factor'] = pow($prefixDefinition['factor'], $powerFactor); + if (is_int($powerFactor) && $powerFactor !== 0) { + $prefixDefinition['factor'] = $prefixDefinition['factor'] ** $powerFactor; } $toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor']; diff --git a/source/PhysicalQuantity/Power.php b/source/PhysicalQuantity/Power.php index d6f5d15..436551d 100644 --- a/source/PhysicalQuantity/Power.php +++ b/source/PhysicalQuantity/Power.php @@ -36,7 +36,7 @@ function ($x) { return 10 * log10($x) + 30; }, function ($x) { - return pow(10, ($x / 10)) / 1000; + return (10 ** ($x / 10)) / 1000; } ); $newUnit->addAlias('dbm'); From 60cd6cfed43dd26006de65fe041fceae68c05ce3 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 3 Nov 2023 16:22:34 +0300 Subject: [PATCH 53/55] Added HasIECUnitsTrait class. Generate all possible storage units. --- source/HasIECUnitsTrait.php | 153 +++++++++++++------------ source/PhysicalQuantity/Storage.php | 41 +++++-- tests/PhysicalQuantity/StorageTest.php | 34 ++++-- 3 files changed, 138 insertions(+), 90 deletions(-) diff --git a/source/HasIECUnitsTrait.php b/source/HasIECUnitsTrait.php index e91d766..502ca00 100644 --- a/source/HasIECUnitsTrait.php +++ b/source/HasIECUnitsTrait.php @@ -15,15 +15,15 @@ trait HasIECUnitsTrait * * Names and Aliases are created by replacing identifiers in the respective * patterns. The current allowed replacement identifiers are: - * %p = the abbreviated IEC prefix, like 'M' for 'megameter' or 'k' for 'kilogram' - * %P = the full IEC prefix, like 'mega' for 'megameter' or 'kilo' for 'kilogram' + * %p = the abbreviated IEC prefix, like 'Mi' for 'mebibits' or 'Ki' for 'kibibits' + * %P = the full IEC prefix, like 'mebi' for 'mebibits' or 'kibi' for 'kibibits' * %U = uppercase version of %P * * So for instance, in order to generate 'kg', 'mg', and 'g' names for IEC units, the * appropriate pattern would be '%pg'. Similarly, to generate 'kilogram', 'milligram', * and 'gram' aliases, the pattern would be '%Pgram'. * - * The $siUnit given in the 1st parameter must be some IEC unit in the series of units + * The $iecUnit given in the 1st parameter must be some IEC unit in the series of units * to be generated by this method. This value is necessary to establish a conversion * factor between this continuum of IEC units and the Physical Quantity's native unit. * @@ -32,114 +32,115 @@ trait HasIECUnitsTrait * if a Kilogram unit of measure was passed for the 1st parameter, it would be necessary to * then pass 1e-3 in the 2nd parameter to indicate that a gram is 1/1000 of the given unit. * - * @param UnitOfMeasure $siUnit A unit in this physical quantity that is an IEC unit of measure - * @param integer $toBaseIecUnitFactor The power-of-ten factor that converts the given IEC unit into the not-prefixed IEC base unit (ie 1e-3 for kilograms) - * @param string $namePattern The pattern to apply to the base unit's name to generate a new IEC unit name - * @param array $aliasPatterns The collection of alias patterns to use in generating a new IEC unit's aliases - * @param integer $powerFactor Use power factor for squares, qubic and other multiplication of IEC factor (ie. square is 2, qubic is 3) + * @param UnitOfMeasure $iecUnit A unit in this physical quantity that is an IEC unit of measure + * @param float $toBaseIecUnitFactor The power-of-ten factor that converts the given IEC unit into the not-prefixed IEC base unit (ie 1e-3 for kilograms) + * @param string $namePattern The pattern to apply to the base unit's name to generate a new IEC unit name + * @param array $aliasPatterns The collection of alias patterns to use in generating a new IEC unit's aliases + * @param integer|null $siBase Used base for si units (1000 or 1024) */ protected static function addMissingIECPrefixedUnits( - UnitOfMeasure $siUnit, - $toBaseIecUnitFactor, - $namePattern, - array $aliasPatterns = [] + UnitOfMeasure $iecUnit, + float $toBaseIecUnitFactor, + string $namePattern, + array $aliasPatterns = [], + int $siBase = null ) { - /** - * The standard set of SI prefixes - */ - $siPrefixes = [ - [ - 'abbr_prefix' => 'Y', - 'long_prefix' => 'yotta', - 'factor' => 1e24 - ], - [ - 'abbr_prefix' => 'Z', - 'long_prefix' => 'zetta', - 'factor' => 1e21 - ], - [ - 'abbr_prefix' => 'E', - 'long_prefix' => 'exa', - 'factor' => 1e18 - ], - [ - 'abbr_prefix' => 'P', - 'long_prefix' => 'peta', - 'factor' => 1e15 - ], - [ - 'abbr_prefix' => 'T', - 'long_prefix' => 'tera', - 'factor' => 1e12 - ], - [ - 'abbr_prefix' => 'G', - 'long_prefix' => 'giga', - 'factor' => 1e9 - ], - [ - 'abbr_prefix' => 'M', - 'long_prefix' => 'mega', - 'factor' => 1e6 - ], - [ - 'abbr_prefix' => 'k', - 'long_prefix' => 'kilo', - 'factor' => 1e3 - ], - [ - 'abbr_prefix' => '', - 'long_prefix' => '', - 'factor' => 1 - ], - ]; - /** * The standard set of IEC prefixes - * http://members.optus.net/alexey/prefBin.xhtml + * https://en.wikipedia.org/wiki/Binary_prefix */ $iecPrefixes = [ [ 'abbr_prefix' => 'Yi', 'long_prefix' => 'yobi', - 'factor' => 1208925819614629174706176 + 'factor' => 1208925819614629174706176 // 2^80 ], [ 'abbr_prefix' => 'Zi', 'long_prefix' => 'zebi', - 'factor' => 1180591620717411303424 + 'factor' => 1180591620717411303424 // 2^70 ], [ 'abbr_prefix' => 'Ei', 'long_prefix' => 'exbi', - 'factor' => 1152921504606846976 + 'factor' => 1152921504606846976 // 2^60 ], [ 'abbr_prefix' => 'Pi', 'long_prefix' => 'pebi', - 'factor' => 1125899906842624 + 'factor' => 1125899906842624 // 2^50 ], [ 'abbr_prefix' => 'Ti', 'long_prefix' => 'tebi', - 'factor' => 1099511627776 + 'factor' => 1099511627776 // 2^40 ], [ 'abbr_prefix' => 'Gi', 'long_prefix' => 'gibi', - 'factor' => 1073741824 + 'factor' => 1073741824 // 2^30 ], [ 'abbr_prefix' => 'Mi', 'long_prefix' => 'mebi', - 'factor' => 1048576 + 'factor' => 1048576 // 2^20 ], [ 'abbr_prefix' => 'Ki', 'long_prefix' => 'kibi', - 'factor' => 1024 + 'factor' => 1024 // 2^10 + ], + + // Partial list of SI prefixes (not all used for storages) + [ + 'abbr_prefix' => 'Y', + 'long_prefix' => 'yotta', + 'factor' => 1e24, + 'si' => 8 ], + [ + 'abbr_prefix' => 'Z', + 'long_prefix' => 'zetta', + 'factor' => 1e21, + 'si' => 7 + ], + [ + 'abbr_prefix' => 'E', + 'long_prefix' => 'exa', + 'factor' => 1e18, + 'si' => 6 + ], + [ + 'abbr_prefix' => 'P', + 'long_prefix' => 'peta', + 'factor' => 1e15, + 'si' => 5 + ], + [ + 'abbr_prefix' => 'T', + 'long_prefix' => 'tera', + 'factor' => 1e12, + 'si' => 4 + ], + [ + 'abbr_prefix' => 'G', + 'long_prefix' => 'giga', + 'factor' => 1e9, + 'si' => 3 + ], + [ + 'abbr_prefix' => 'M', + 'long_prefix' => 'mega', + 'factor' => 1e6, + 'si' => 2 + ], + [ + 'abbr_prefix' => 'k', + 'long_prefix' => 'kilo', + 'factor' => 1e3, + 'si' => 1 + ], + [ 'abbr_prefix' => '', 'long_prefix' => '', @@ -148,10 +149,10 @@ protected static function addMissingIECPrefixedUnits( ]; // Determine the conversion factor from the no-prefix IEC unit to the physical quantity's native unit - $noPrefixToNativeUnitFactor = $siUnit->convertValueToNativeUnitOfMeasure(1) * $toBaseIecUnitFactor; + $noPrefixToNativeUnitFactor = $iecUnit->convertValueToNativeUnitOfMeasure(1) * $toBaseIecUnitFactor; // For each of the standard IEC prefixes, attempt to register a new unit of measure - foreach ($siPrefixes as $prefixDefinition) { + foreach ($iecPrefixes as $prefixDefinition) { // Build a function for resolving a pattern into a unit name $parsePattern = function ($pattern) use ($prefixDefinition) { return strtr( @@ -169,8 +170,8 @@ protected static function addMissingIECPrefixedUnits( // Determine the factor that converts the new unit into the physical quantity's // native unit of measure. - if (is_int($powerFactor) && $powerFactor != 0) { - $prefixDefinition['factor'] = pow($prefixDefinition['factor'], $powerFactor); + if ($siBase === 1024 && isset($prefixDefinition['si'])) { + $prefixDefinition['factor'] = 1024 ** $prefixDefinition['si']; } $toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor']; diff --git a/source/PhysicalQuantity/Storage.php b/source/PhysicalQuantity/Storage.php index dd1789b..194ef17 100644 --- a/source/PhysicalQuantity/Storage.php +++ b/source/PhysicalQuantity/Storage.php @@ -4,24 +4,50 @@ use PhpUnitsOfMeasure\AbstractPhysicalQuantity; use PhpUnitsOfMeasure\UnitOfMeasure; +use PhpUnitsOfMeasure\HasIECUnitsTrait; class Storage extends AbstractPhysicalQuantity { + use HasIECUnitsTrait; protected static $unitDefinitions; protected static function initialize() { + // Bit + $bit = UnitOfMeasure::nativeUnitFactory('bit'); + $bit->addAlias('b'); + static::addUnit($bit); + + static::addMissingIECPrefixedUnits( + $bit, + 1, + '%Pbit', + [ + '%pb', + '%Pbits', + ] + ); + // Byte - $unit = UnitOfMeasure::nativeUnitFactory('byte'); - $unit->addAlias('B'); - static::addUnit($unit); + $byte = UnitOfMeasure::linearUnitFactory('byte', 8); + $byte->addAlias('B'); + static::addUnit($byte); - // Bit - $unit = UnitOfMeasure::linearUnitFactory('bit', 0.125); - $unit->addAlias('b'); - static::addUnit($unit); + static::addMissingIECPrefixedUnits( + $byte, + 1, + '%Pbyte', + [ + '%Pbytes', + '%pB', + '%pByte', + '%pBytes', + ], + 1024 + ); + /* // kilobyte (KB) $unit = UnitOfMeasure::linearUnitFactory('kilobyte', 1024); $unit->addAlias('KB'); @@ -46,5 +72,6 @@ protected static function initialize() $unit = UnitOfMeasure::linearUnitFactory('petabyte', 1125899906842624); $unit->addAlias('PB'); static::addUnit($unit); + */ } } diff --git a/tests/PhysicalQuantity/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php index 9c55558..e1cdf46 100644 --- a/tests/PhysicalQuantity/StorageTest.php +++ b/tests/PhysicalQuantity/StorageTest.php @@ -15,36 +15,56 @@ protected function instantiateTestQuantity(): PhysicalQuantityInterface public function testByteToByte(): void { - $this->assertEquals(1, (new Storage(1, 'byte'))->toUnit('B')); + $storage = new Storage(1, 'byte'); + $this->assertEquals(1, $storage->toUnit('B')); + $storage = new Storage(1, 'B'); + $this->assertEquals(0.008, $storage->toUnit('kb')); } public function testByteToBit(): void { - $this->assertEquals(40, (new Storage(5, 'byte'))->toUnit('bit')); + $storage = new Storage(5, 'byte'); + $this->assertEquals(40, $storage->toUnit('bit')); } public function testBitToByte(): void { - $this->assertEquals(1.25, (new Storage(10, 'b'))->toUnit('B')); + $storage = new Storage(10, 'b'); + $this->assertEquals(1.25, $storage->toUnit('B')); } public function testMegabyteToByte(): void { - $this->assertEquals(1048576, (new Storage(1, 'MB'))->toUnit('B')); + $storage = new Storage(1, 'MB'); + $this->assertEquals(1048576, $storage->toUnit('B')); + } + + public function testGibibyteToByte(): void + { + $storage = new Storage(1, 'gibibytes'); + $this->assertEquals(1073741824, $storage->toUnit('byte')); + $storage = new Storage(1, 'GiB'); + $this->assertEquals(1073741824, $storage->toUnit('byte')); } public function testGigabyteToByte(): void { - $this->assertEquals(1073741824, (new Storage(1, 'gigabyte'))->toUnit('byte')); + $storage = new Storage(1, 'gigabyte'); + $this->assertEquals(1073741824, $storage->toUnit('byte')); + // 6GByte + $storage = new Storage(6, 'GByte'); + $this->assertEquals(6*1024*1024*1024, $storage->toUnit('byte')); } public function testTerabyteToByte(): void { - $this->assertEquals(1099511627776, (new Storage(1, 'TB'))->toUnit('byte')); + $storage = new Storage(1, 'TB'); + $this->assertEquals(1099511627776, $storage->toUnit('byte')); } public function testPetabyteToByte(): void { - $this->assertEquals(3377699720527872, (new Storage(3, 'PB'))->toUnit('byte')); + $storage = new Storage(3, 'PB'); + $this->assertEquals(3377699720527872, $storage->toUnit('byte')); } } From 3014eb82cf13fcb8d09c9d26839fd66d808e446b Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 3 Nov 2023 16:26:29 +0300 Subject: [PATCH 54/55] Added quetta and ronna Si/IEC prefixes. --- source/HasIECUnitsTrait.php | 12 ++++++++++++ source/HasSIUnitsTrait.php | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/source/HasIECUnitsTrait.php b/source/HasIECUnitsTrait.php index 502ca00..7cadbc9 100644 --- a/source/HasIECUnitsTrait.php +++ b/source/HasIECUnitsTrait.php @@ -92,6 +92,18 @@ protected static function addMissingIECPrefixedUnits( ], // Partial list of SI prefixes (not all used for storages) + [ + 'abbr_prefix' => 'Q', + 'long_prefix' => 'quetta', + 'factor' => 1e30, + 'si' => 10 + ], + [ + 'abbr_prefix' => 'R', + 'long_prefix' => 'ronna', + 'factor' => 1e27, + 'si' => 9 + ], [ 'abbr_prefix' => 'Y', 'long_prefix' => 'yotta', diff --git a/source/HasSIUnitsTrait.php b/source/HasSIUnitsTrait.php index acc6a87..0d18ad2 100644 --- a/source/HasSIUnitsTrait.php +++ b/source/HasSIUnitsTrait.php @@ -49,6 +49,16 @@ protected static function addMissingSIPrefixedUnits( * The standard set of SI prefixes */ $siPrefixes = [ + [ + 'abbr_prefix' => 'Q', + 'long_prefix' => 'quetta', + 'factor' => 1e30 + ], + [ + 'abbr_prefix' => 'R', + 'long_prefix' => 'ronna', + 'factor' => 1e27 + ], [ 'abbr_prefix' => 'Y', 'long_prefix' => 'yotta', From 056d40b29887ca9cd37c321e2ab9cc2b9398e936 Mon Sep 17 00:00:00 2001 From: Mike Stupalov Date: Fri, 3 Nov 2023 16:31:10 +0300 Subject: [PATCH 55/55] Updated CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c47550..6f254c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v2.2.2 (November 3nd, 2023) +- Added ronna (R) and quetta (Q) si prefixes. +- Added IEC prefixed units support for Storage +- Added Storage units + ## v2.2.1 (November 2nd, 2023) - Drop support of php < 7.2