diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..1e721c7 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,59 @@ +# .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.2'] + stability: [ prefer-stable ] + experimental: [false] + include: + - php: '7.2' + stability: prefer-lowest + - php: '7.3' + - php: '7.4' + - 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/.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 deleted file mode 100644 index 545a1d5..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: php -sudo: false -dist: trusty - -php: - - '5.5' - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - nightly - - hhvm - -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 - -matrix: - allow_failures: - - php: '5.5' - - php: nightly - - php: hhvm - diff --git a/CHANGELOG.md b/CHANGELOG.md index 86baa65..6f254c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,28 @@ +## 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 + +## 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 +- 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 +- 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 +- 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 + ## 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 +32,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/README.md b/README.md index 2a7e6b1..c53b03e 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: @@ -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. @@ -307,16 +307,16 @@ 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 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 diff --git a/composer.json b/composer.json index fbb6c28..dfca5dd 100644 --- a/composer.json +++ b/composer.json @@ -27,17 +27,25 @@ }, "require": { - "php": ">=5.5.0" + "php": ">=7.2" }, "require-dev": { - "phpunit/phpunit": "4.8.*", - "squizlabs/php_codesniffer": "2.8.1" + "phpunit/phpunit": "^8.5", + "squizlabs/php_codesniffer": "^3.7" }, "replace": { "triplepoint/php-units-of-measure": "*" }, + + "scripts": { + "test": [ + "@putenv XDEBUG_MODE=coverage", + "phpunit -c ./tests/phpunit.xml.dist ./tests" + ], + "phpcs": "phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./" + }, "autoload": { "psr-4": { 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/HasIECUnitsTrait.php b/source/HasIECUnitsTrait.php new file mode 100644 index 0000000..7cadbc9 --- /dev/null +++ b/source/HasIECUnitsTrait.php @@ -0,0 +1,205 @@ + 'Yi', + 'long_prefix' => 'yobi', + 'factor' => 1208925819614629174706176 // 2^80 + ], + [ + 'abbr_prefix' => 'Zi', + 'long_prefix' => 'zebi', + 'factor' => 1180591620717411303424 // 2^70 + ], + [ + 'abbr_prefix' => 'Ei', + 'long_prefix' => 'exbi', + 'factor' => 1152921504606846976 // 2^60 + ], + [ + 'abbr_prefix' => 'Pi', + 'long_prefix' => 'pebi', + 'factor' => 1125899906842624 // 2^50 + ], + [ + 'abbr_prefix' => 'Ti', + 'long_prefix' => 'tebi', + 'factor' => 1099511627776 // 2^40 + ], + [ + 'abbr_prefix' => 'Gi', + 'long_prefix' => 'gibi', + 'factor' => 1073741824 // 2^30 + ], + [ + 'abbr_prefix' => 'Mi', + 'long_prefix' => 'mebi', + 'factor' => 1048576 // 2^20 + ], + [ + 'abbr_prefix' => 'Ki', + 'long_prefix' => 'kibi', + 'factor' => 1024 // 2^10 + ], + + // 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', + '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' => '', + 'factor' => 1 + ], + ]; + + // Determine the conversion factor from the no-prefix IEC unit to the physical quantity's native unit + $noPrefixToNativeUnitFactor = $iecUnit->convertValueToNativeUnitOfMeasure(1) * $toBaseIecUnitFactor; + + // For each of the standard IEC prefixes, attempt to register a new unit of measure + foreach ($iecPrefixes 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 ($siBase === 1024 && isset($prefixDefinition['si'])) { + $prefixDefinition['factor'] = 1024 ** $prefixDefinition['si']; + } + $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); + } + } + } +} diff --git a/source/HasSIUnitsTrait.php b/source/HasSIUnitsTrait.php index 6ec10f4..0d18ad2 100644 --- a/source/HasSIUnitsTrait.php +++ b/source/HasSIUnitsTrait.php @@ -33,20 +33,32 @@ 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|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 = [] + float $toBaseSiUnitFactor, + string $namePattern, + array $aliasPatterns = [], + int $powerFactor = null ) { /** * 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', @@ -176,6 +188,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'] = $prefixDefinition['factor'] ** $powerFactor; + } $toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor']; // Instantiate the new unit of measure 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/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/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/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/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/source/PhysicalQuantity/Power.php b/source/PhysicalQuantity/Power.php index 0ec2a70..436551d 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 (10 ** ($x / 10)) / 1000; + } + ); + $newUnit->addAlias('dbm'); + $newUnit->addAlias('decibels-milliwatt'); + static::addUnit($newUnit); } } 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/source/PhysicalQuantity/Storage.php b/source/PhysicalQuantity/Storage.php new file mode 100644 index 0000000..194ef17 --- /dev/null +++ b/source/PhysicalQuantity/Storage.php @@ -0,0 +1,77 @@ +addAlias('b'); + static::addUnit($bit); + + static::addMissingIECPrefixedUnits( + $bit, + 1, + '%Pbit', + [ + '%pb', + '%Pbits', + ] + ); + + // Byte + $byte = UnitOfMeasure::linearUnitFactory('byte', 8); + $byte->addAlias('B'); + static::addUnit($byte); + + static::addMissingIECPrefixedUnits( + $byte, + 1, + '%Pbyte', + [ + '%Pbytes', + '%pB', + '%pByte', + '%pBytes', + ], + 1024 + ); + + /* + // 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/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'); diff --git a/source/PhysicalQuantity/Velocity.php b/source/PhysicalQuantity/Velocity.php index 0b6ba30..7c5c6a6 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,30 +22,106 @@ 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); // knot - $newUnit = UnitOfMeasure::linearUnitFactory('knot', 0.514444); + $newUnit = UnitOfMeasure::linearUnitFactory('kn', 0.514444); + $newUnit->addAlias('knot'); $newUnit->addAlias('knots'); static::addUnit($newUnit); + + // Supersonic speed + $newUnit = UnitOfMeasure::linearUnitFactory('mach', 344); + $newUnit->addAlias('speed of sound'); + static::addUnit($newUnit); } } diff --git a/source/PhysicalQuantity/Volume.php b/source/PhysicalQuantity/Volume.php index c679ea3..e70897b 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,110 +22,62 @@ 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 // 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'); 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); - - // 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); + $liter = UnitOfMeasure::linearUnitFactory('l', 1e-3); + $liter->addAlias('L'); + $liter->addAlias('liter'); + $liter->addAlias('liters'); + $liter->addAlias('litre'); + $liter->addAlias('litres'); + static::addUnit($liter); + + static::addMissingSIPrefixedUnits( + $liter, + 1, + '%pl', + [ + '%pL', + '%Pliter', + '%Pliters', + '%Plitre', + '%Plitres' + ] + ); // Cup $newUnit = UnitOfMeasure::linearUnitFactory('cup', 2.365882e-4); @@ -148,6 +103,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 +111,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 +120,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/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/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index bbda2ed..239e364 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,49 @@ 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 +124,7 @@ public function testUnitConvertsToArbitraryUnit( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void { $this->assertSame($valueInArbitraryUnit, $value->toUnit($arbitraryUnit)); } @@ -136,20 +132,19 @@ 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 +155,7 @@ public function testUnserialize( AbstractPhysicalQuantity $value, $arbitraryUnit, $valueInArbitraryUnit - ) { + ): void { $unserializedValue = unserialize(serialize($value)); $this->assertSame($valueInArbitraryUnit, $unserializedValue->toUnit($arbitraryUnit)); @@ -176,9 +171,9 @@ 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 +193,9 @@ 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 +208,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 +221,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 +240,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 +258,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 +274,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 +291,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 +311,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..76b0ea0 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 = []; /** * 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..7a891f4 100644 --- a/tests/PhysicalQuantity/AccelerationTest.php +++ b/tests/PhysicalQuantity/AccelerationTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Acceleration; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AccelerationTest extends AbstractPhysicalQuantityTestCase { @@ -15,8 +16,20 @@ class AccelerationTest extends AbstractPhysicalQuantityTestCase 'metres per second squared', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Acceleration(1, 'm/s^2'); } + + public function testToKiloMeterPerSecond(): void + { + $value = new Acceleration(1000, 'm/s^2'); + $this->assertEquals(1, $value->toUnit('km/s^2')); + } + + public function testToMilliMeterPerSecond(): void + { + $value = new Acceleration(1, 'm/s^2'); + $this->assertEquals(1000, $value->toUnit('mm/s^2')); + } } diff --git a/tests/PhysicalQuantity/AngleTest.php b/tests/PhysicalQuantity/AngleTest.php index 63ed22b..3d43286 100644 --- a/tests/PhysicalQuantity/AngleTest.php +++ b/tests/PhysicalQuantity/AngleTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Angle; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AngleTest extends AbstractPhysicalQuantityTestCase { @@ -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..cd5ca1d 100644 --- a/tests/PhysicalQuantity/AreaTest.php +++ b/tests/PhysicalQuantity/AreaTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Area; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class AreaTest extends AbstractPhysicalQuantityTestCase { @@ -13,30 +14,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', @@ -61,12 +162,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 +178,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,15 +187,31 @@ 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')); } + + 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->assertEqualsWithDelta(10000, $area->toUnit('dm^2'), 0.000001); + } + + public function testToMillimeterSquare(): void + { + $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')); + } } diff --git a/tests/PhysicalQuantity/ElectricCurrentTest.php b/tests/PhysicalQuantity/ElectricCurrentTest.php index 7c0cb35..e521a80 100644 --- a/tests/PhysicalQuantity/ElectricCurrentTest.php +++ b/tests/PhysicalQuantity/ElectricCurrentTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class ElectricCurrentTest extends AbstractPhysicalQuantityTestCase { @@ -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..c0c9125 100644 --- a/tests/PhysicalQuantity/EnergyTest.php +++ b/tests/PhysicalQuantity/EnergyTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Energy; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class EnergyTest extends AbstractPhysicalQuantityTestCase { @@ -56,34 +57,60 @@ class EnergyTest extends AbstractPhysicalQuantityTestCase 'PJ', 'petajoule', 'petajoules', + + 'eV', + 'electronvolt', + 'electronvolts', + 'MeV', + 'megaelectronvolt', + 'megaelectronvolts', + + 'cal', + 'calorie', + 'calories', + 'Mcal', + 'megacalorie', + 'megacalories', ]; - 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')); } + + public function testToMegaCal(): void + { + $quantity = new Energy(1, 'kWh'); + $this->assertEquals(0.8604206500956023, $quantity->toUnit('Mcal')); + } + + 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 27b27f0..1b8912a 100644 --- a/tests/PhysicalQuantity/LengthTest.php +++ b/tests/PhysicalQuantity/LengthTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Length; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class LengthTest extends AbstractPhysicalQuantityTestCase { @@ -133,46 +134,132 @@ 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() + 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')); + $this->assertEqualsWithDelta(24, $quantity->toUnit('in'), 0.000001); } - 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')); } + + public function testToLightYear(): void + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(15.855012510369232, $quantity->toUnit('ly')); + } + + public function testToParSec(): void + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(4.861168934166548, $quantity->toUnit('pc')); + } + + public function testToMilliParSec(): void + { + $quantity = new Length(150000000000000, 'km'); + $this->assertEquals(4861.168934166548, $quantity->toUnit('mpc')); + } } diff --git a/tests/PhysicalQuantity/LuminousIntensityTest.php b/tests/PhysicalQuantity/LuminousIntensityTest.php index 204158d..1bcbfde 100644 --- a/tests/PhysicalQuantity/LuminousIntensityTest.php +++ b/tests/PhysicalQuantity/LuminousIntensityTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class LuminousIntensityTest extends AbstractPhysicalQuantityTestCase { @@ -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..91b3a92 100644 --- a/tests/PhysicalQuantity/MassTest.php +++ b/tests/PhysicalQuantity/MassTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Mass; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class MassTest extends AbstractPhysicalQuantityTestCase { @@ -71,10 +72,13 @@ class MassTest extends AbstractPhysicalQuantityTestCase 'yoctogram', 'yoctograms', 't', + 'T', 'ton', 'tons', 'tonne', 'tonnes', + 'MT', + 'mT', 'lb', 'lbs', 'pound', @@ -85,34 +89,69 @@ 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() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { - return new Mass(1, 'kg'); + return new Mass(1, 'g'); } - 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')); } + + public function testToHundredweight(): void + { + $quantity = new Mass(5, 'kg'); + $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); + } + + public function testToUSlongTon(): void + { + $quantity = new Mass(5, 'kg'); + $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); + } } diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index b66e777..b490435 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Power; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class PowerTest extends AbstractPhysicalQuantityTestCase { @@ -33,20 +34,40 @@ 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')); } + + public function testToDecibelsMilliWatt(): void + { + $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(): void + { + $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')); + } } diff --git a/tests/PhysicalQuantity/PressureTest.php b/tests/PhysicalQuantity/PressureTest.php index c8a4ab7..00e2be6 100644 --- a/tests/PhysicalQuantity/PressureTest.php +++ b/tests/PhysicalQuantity/PressureTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Pressure; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class PressureTest extends AbstractPhysicalQuantityTestCase { @@ -49,6 +50,7 @@ class PressureTest extends AbstractPhysicalQuantityTestCase 'zeptopascal', 'yPa', 'yoctopascal', + 'at', 'atm', 'atmosphere', 'atmospheres', @@ -80,10 +82,12 @@ class PressureTest extends AbstractPhysicalQuantityTestCase 'millimetres of mercury', 'torr', 'psi', + 'ksi', + 'Mpsi', '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..ba943e7 100644 --- a/tests/PhysicalQuantity/QuantityTest.php +++ b/tests/PhysicalQuantity/QuantityTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Quantity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class QuantityTest extends AbstractPhysicalQuantityTestCase { @@ -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..008af47 100644 --- a/tests/PhysicalQuantity/SolidAngleTest.php +++ b/tests/PhysicalQuantity/SolidAngleTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\SolidAngle; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class SolidAngleTest extends AbstractPhysicalQuantityTestCase { @@ -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/StorageTest.php b/tests/PhysicalQuantity/StorageTest.php new file mode 100644 index 0000000..e1cdf46 --- /dev/null +++ b/tests/PhysicalQuantity/StorageTest.php @@ -0,0 +1,70 @@ +assertEquals(1, $storage->toUnit('B')); + $storage = new Storage(1, 'B'); + $this->assertEquals(0.008, $storage->toUnit('kb')); + } + + public function testByteToBit(): void + { + $storage = new Storage(5, 'byte'); + $this->assertEquals(40, $storage->toUnit('bit')); + } + + public function testBitToByte(): void + { + $storage = new Storage(10, 'b'); + $this->assertEquals(1.25, $storage->toUnit('B')); + } + + public function testMegabyteToByte(): void + { + $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 + { + $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 + { + $storage = new Storage(1, 'TB'); + $this->assertEquals(1099511627776, $storage->toUnit('byte')); + } + + public function testPetabyteToByte(): void + { + $storage = new Storage(3, 'PB'); + $this->assertEquals(3377699720527872, $storage->toUnit('byte')); + } +} diff --git a/tests/PhysicalQuantity/TemperatureTest.php b/tests/PhysicalQuantity/TemperatureTest.php index 62f7d90..57c7003 100644 --- a/tests/PhysicalQuantity/TemperatureTest.php +++ b/tests/PhysicalQuantity/TemperatureTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Temperature; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class TemperatureTest extends AbstractPhysicalQuantityTestCase { @@ -79,8 +80,49 @@ class TemperatureTest extends AbstractPhysicalQuantityTestCase 'romer', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Temperature(1, 'K'); } + + public function testToCelsius(): void + { + $value = new Temperature(313.15, 'K'); + $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); + } + + public function testToRankine(): void + { + $value = new Temperature(313.15, 'K'); + $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); + } + + public function testToNewton(): void + { + $value = new Temperature(313.15, 'K'); + $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); + } + + public function testToRomer(): void + { + $value = new Temperature(313.15, 'K'); + $this->assertEqualsWithDelta(28.5, $value->toUnit('Ro'), 0.000001); + } } diff --git a/tests/PhysicalQuantity/TimeTest.php b/tests/PhysicalQuantity/TimeTest.php index 957d1d8..019f678 100644 --- a/tests/PhysicalQuantity/TimeTest.php +++ b/tests/PhysicalQuantity/TimeTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Time; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class TimeTest extends AbstractPhysicalQuantityTestCase { @@ -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..a42d484 100644 --- a/tests/PhysicalQuantity/VelocityTest.php +++ b/tests/PhysicalQuantity/VelocityTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Velocity; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class VelocityTest extends AbstractPhysicalQuantityTestCase { @@ -29,32 +30,38 @@ 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')); + $this->assertEqualsWithDelta(0.277778, $speed->toUnit('m/s'), 0.000001); } - 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')); + $this->assertEqualsWithDelta(3.218688, $speed->toUnit('km/h'), 0.000001); } - public function testToKnot() + public function testToKnot(): void { $speed = new Velocity(2, 'm/s'); $this->assertEquals(3.8876923435786983, $speed->toUnit('knot')); } + + public function testToMach(): void + { + $speed = new Velocity(1000, 'm/s'); + $this->assertEqualsWithDelta(2.906977, $speed->toUnit('mach'), 0.000001); + } } diff --git a/tests/PhysicalQuantity/VolumeFlowTest.php b/tests/PhysicalQuantity/VolumeFlowTest.php new file mode 100644 index 0000000..ee9980a --- /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->assertEqualsWithDelta(1000, $area->toUnit('dm^3/s'), 0.000001); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEqualsWithDelta(100000, $area->toUnit('dm^3/s'), 0.000001); + } + + + public function testToCubicMillimeterSecond(): void + { + $area = new VolumeFlow(1, 'm^3/s'); + $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(): void + { + $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(): void + { + $area = new VolumeFlow(1, 'l/s'); + $this->assertEqualsWithDelta(1000, $area->toUnit('ml/s'), 0.000001); + $area = new VolumeFlow(100, 'l/s'); + $this->assertEqualsWithDelta(100000, $area->toUnit('ml/s'), 0.000001); + } + + public function testToHectoLitresSecond(): void + { + $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(): void + { + $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->assertEqualsWithDelta(35.314667, $area->toUnit('CFM'), 0.000001); + $area = new VolumeFlow(100, 'm^3/s'); + $this->assertEqualsWithDelta(211888.000328, $area->toUnit('CFM'), 0.000001); + } +} diff --git a/tests/PhysicalQuantity/VolumeTest.php b/tests/PhysicalQuantity/VolumeTest.php index 3b6493e..384af3e 100644 --- a/tests/PhysicalQuantity/VolumeTest.php +++ b/tests/PhysicalQuantity/VolumeTest.php @@ -3,6 +3,7 @@ namespace PhpUnitsOfMeasureTest\PhysicalQuantity; use PhpUnitsOfMeasure\PhysicalQuantity\Volume; +use PhpUnitsOfMeasure\PhysicalQuantityInterface; class VolumeTest extends AbstractPhysicalQuantityTestCase { @@ -84,8 +85,64 @@ class VolumeTest extends AbstractPhysicalQuantityTestCase 'cups', ]; - protected function instantiateTestQuantity() + protected function instantiateTestQuantity(): PhysicalQuantityInterface { return new Volume(1, 'm^3'); } + + 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->assertEqualsWithDelta(100000, $area->toUnit('dm^3'), 0.000001); + } + + public function testToCubicMillimeter(): void + { + $area = new Volume(1, 'm^3'); + $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(): void + { + $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 testToMilliLitres(): void + { + $area = new Volume(1, 'l'); + $this->assertEqualsWithDelta(1000, $area->toUnit('ml'), 0.000001); + $area = new Volume(100, 'l'); + $this->assertEqualsWithDelta(100000, $area->toUnit('ml'), 0.000001); + } + + public function testToHectoLitres(): void + { + $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(): void + { + $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(): void + { + $area = new Volume(1, 'm^3'); + $this->assertEqualsWithDelta(264.172051, $area->toUnit('gal'), 0.000001); + $area = new Volume(100, 'm^3'); + $this->assertEqualsWithDelta(26417.205124, $area->toUnit('gal'), 0.000001); + } } 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 + + + + + - - -