From 22b90271c958a131d3ed2afa57ebae9bbb50311f Mon Sep 17 00:00:00 2001 From: ibra1994 Date: Wed, 4 Sep 2019 22:25:38 +0500 Subject: [PATCH] added new unit: miles per imperial gallon(mpig) in measure fuel economy units divided from volume added tests for miles per imperial gallon --- .../ToLitrePer100Kilometres.php | 4 +- .../ToMilesPerGallonImperial.php | 47 +++++++ ...esPerGallon.php => ToMilesPerGallonUS.php} | 6 +- .../ToKilometrePerLitre.php | 4 +- .../ToMilesPerGallonImperial.php | 47 +++++++ ...esPerGallon.php => ToMilesPerGallonUS.php} | 6 +- .../ToKilometrePerLitre.php | 47 +++++++ .../ToLitrePer100Kilometres.php | 47 +++++++ .../ToMilesPerGallonUS.php | 47 +++++++ .../ToKilometrePerLitre.php | 6 +- .../ToLitrePer100Kilometres.php | 11 +- .../ToMilesPerGallonImperial.php | 47 +++++++ src/UnitConverter/Measure.php | 10 +- .../Unit/FuelEconomy/FuelEconomyUnit.php | 2 +- .../Unit/FuelEconomy/KilometrePerLitre.php | 6 +- .../FuelEconomy/LitrePer100Kilometres.php | 6 +- .../FuelEconomy/MilesPerGallonImperial.php | 44 +++++++ ...ilesPerGallon.php => MilesPerGallonUS.php} | 10 +- .../FuelEconomy/KilometrePerLitre.spec.php | 22 +++- .../LitrePer100Kilometres.spec.php | 22 +++- .../MilesPerGallonImperial.spec.php | 115 ++++++++++++++++++ ...lon.spec.php => MilesPerGallonUS.spec.php} | 24 +++- .../Calculator/BinaryCalculator.spec.php | 2 +- .../Registry/UnitRegistry.spec.php | 1 + 24 files changed, 546 insertions(+), 37 deletions(-) create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonImperial.php rename src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/{ToMilesPerGallon.php => ToMilesPerGallonUS.php} (87%) create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonImperial.php rename src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/{ToMilesPerGallon.php => ToMilesPerGallonUS.php} (86%) create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToKilometrePerLitre.php create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToLitrePer100Kilometres.php create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToMilesPerGallonUS.php rename src/UnitConverter/Calculator/Formula/FuelEconomy/{MilesPerGallon => MilesPerGallonUS}/ToKilometrePerLitre.php (90%) rename src/UnitConverter/Calculator/Formula/FuelEconomy/{MilesPerGallon => MilesPerGallonUS}/ToLitrePer100Kilometres.php (81%) create mode 100644 src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToMilesPerGallonImperial.php create mode 100644 src/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.php rename src/UnitConverter/Unit/FuelEconomy/{MilesPerGallon.php => MilesPerGallonUS.php} (66%) create mode 100644 tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.spec.php rename tests/integration/UnitConverter/Unit/FuelEconomy/{MilesPerGallon.spec.php => MilesPerGallonUS.spec.php} (80%) diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToLitrePer100Kilometres.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToLitrePer100Kilometres.php index 9b2c2b7a..07c2dbb2 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToLitrePer100Kilometres.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToLitrePer100Kilometres.php @@ -24,6 +24,8 @@ */ class ToLitrePer100Kilometres extends AbstractFormula { + const MAGIC_NUMBER = 100; + const FORMULA_STRING = 'L/100km = 100 / km/l'; const FORMULA_TEMPLATE = '%s L/100km = 100 / %skm/l'; @@ -34,7 +36,7 @@ class ToLitrePer100Kilometres extends AbstractFormula public function describe($value, $fromUnits, $toUnits, int $precision = null) { // XXX: this formula assumes all calculators can accept strings, as it's the safest type. - $addResult = $this->calculator->div(100, $value); + $addResult = $this->calculator->div(self::MAGIC_NUMBER, $value); $result = $this->calculator->round($addResult, $precision); $this->plugVariables($result, $value); diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonImperial.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonImperial.php new file mode 100644 index 00000000..004a78c4 --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonImperial.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\KilometrePerLitre; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Kilometre Per Litre values to Miles Per Gallon. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToMilesPerGallonImperial extends AbstractFormula +{ + + const MAGIC_NUMBER = 2.82481; + + const FORMULA_STRING = 'mpg(Imperial) = 2.82481 * km/l'; + + const FORMULA_TEMPLATE = '%s mpg(Imperial) = 2.82481 * %skm/l'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->mul(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallon.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonUS.php similarity index 87% rename from src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallon.php rename to src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonUS.php index e7f9dd3d..b8265bbd 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallon.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/KilometrePerLitre/ToMilesPerGallonUS.php @@ -22,14 +22,14 @@ * @version 1.0.0 * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> */ -class ToMilesPerGallon extends AbstractFormula +class ToMilesPerGallonUS extends AbstractFormula { const MAGIC_NUMBER = 2.35215; - const FORMULA_STRING = 'mpg = 2.35215 * km/l'; + const FORMULA_STRING = 'mpg(US) = 2.35215 * km/l'; - const FORMULA_TEMPLATE = '%s mpg = 2.35215 * %skm/l'; + const FORMULA_TEMPLATE = '%s mpg(US) = 2.35215 * %skm/l'; /** * {@inheritDoc} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToKilometrePerLitre.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToKilometrePerLitre.php index e698b35f..45e66378 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToKilometrePerLitre.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToKilometrePerLitre.php @@ -24,6 +24,8 @@ */ class ToKilometrePerLitre extends AbstractFormula { + const MAGIC_NUMBER = 100; + const FORMULA_STRING = 'km/l = 100 / L/100km'; const FORMULA_TEMPLATE = '%s km/l = 100 / %sL/100km'; @@ -34,7 +36,7 @@ class ToKilometrePerLitre extends AbstractFormula public function describe($value, $fromUnits, $toUnits, int $precision = null) { // XXX: this formula assumes all calculators can accept strings, as it's the safest type. - $addResult = $this->calculator->div(100, $value); + $addResult = $this->calculator->div(self::MAGIC_NUMBER, $value); $result = $this->calculator->round($addResult, $precision); $this->plugVariables($result, $value); diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonImperial.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonImperial.php new file mode 100644 index 00000000..cf415643 --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonImperial.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\LitrePer100Kilometres; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Miles Per Gallon Per 100 Kilometres values to Miles Per Gallon. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToMilesPerGallonImperial extends AbstractFormula +{ + + const MAGIC_NUMBER = 282.481; + + const FORMULA_STRING = 'mpg(Imperial) = 282.481 / L/100km'; + + const FORMULA_TEMPLATE = '%s mpg(Imperial) = 282.481 / %sL/100km'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->div(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallon.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonUS.php similarity index 86% rename from src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallon.php rename to src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonUS.php index c9d2ead2..9d4627eb 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallon.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/LitrePer100Kilometres/ToMilesPerGallonUS.php @@ -22,14 +22,14 @@ * @version 1.0.0 * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> */ -class ToMilesPerGallon extends AbstractFormula +class ToMilesPerGallonUS extends AbstractFormula { const MAGIC_NUMBER = 235.215; - const FORMULA_STRING = 'mpg = 235.215 / L/100km'; + const FORMULA_STRING = 'mpg(US) = 235.215 / L/100km'; - const FORMULA_TEMPLATE = '%s mpg = 235.215 / %sL/100km'; + const FORMULA_TEMPLATE = '%s mpg(US) = 235.215 / %sL/100km'; /** * {@inheritDoc} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToKilometrePerLitre.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToKilometrePerLitre.php new file mode 100644 index 00000000..c749c10a --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToKilometrePerLitre.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Miles Per Gallon values to Kilometre Per Litre. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToKilometrePerLitre extends AbstractFormula +{ + + const MAGIC_NUMBER = 0.354006; + + const FORMULA_STRING = 'km/l = 0.354006 * mpg(imp)'; + + const FORMULA_TEMPLATE = '%s km/l = 0.354006 * %smpg(imp)'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->mul(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToLitrePer100Kilometres.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToLitrePer100Kilometres.php new file mode 100644 index 00000000..aa558ad7 --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToLitrePer100Kilometres.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Miles Per Gallon values to Litre Per 100 Kilometres. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToLitrePer100Kilometres extends AbstractFormula +{ + + const MAGIC_NUMBER = 282.481; + + const FORMULA_STRING = 'L/100km = 282.481 / mpg(imp)'; + + const FORMULA_TEMPLATE = '%s L/100km = 282.481 / %smpg(imp)'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->div(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToMilesPerGallonUS.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToMilesPerGallonUS.php new file mode 100644 index 00000000..ed4ddbbf --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonImperial/ToMilesPerGallonUS.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Kilometre Per Litre values to Miles Per Gallon. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToMilesPerGallonUS extends AbstractFormula +{ + + const MAGIC_NUMBER = 0.832674; + + const FORMULA_STRING = 'mpg(Imperial) = 0.832674 * mpg(US)'; + + const FORMULA_TEMPLATE = '%s mpg(Imperial) = 0.832674 * %smpg(US)'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->mul(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToKilometrePerLitre.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToKilometrePerLitre.php similarity index 90% rename from src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToKilometrePerLitre.php rename to src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToKilometrePerLitre.php index 726b96a9..189c2ef8 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToKilometrePerLitre.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToKilometrePerLitre.php @@ -12,7 +12,7 @@ * file that was distributed with this source code. */ -namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallon; +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS; use UnitConverter\Calculator\Formula\AbstractFormula; @@ -27,9 +27,9 @@ class ToKilometrePerLitre extends AbstractFormula const MAGIC_NUMBER = 0.425144; - const FORMULA_STRING = 'km/l = 0.425144 * mpg'; + const FORMULA_STRING = 'km/l = 0.425144 * mpg(US)'; - const FORMULA_TEMPLATE = '%s km/l = 0.425144 * %smpg'; + const FORMULA_TEMPLATE = '%s km/l = 0.425144 * %smpg(US)'; /** * {@inheritDoc} diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToLitrePer100Kilometres.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToLitrePer100Kilometres.php similarity index 81% rename from src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToLitrePer100Kilometres.php rename to src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToLitrePer100Kilometres.php index c441125c..9cedda24 100644 --- a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallon/ToLitrePer100Kilometres.php +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToLitrePer100Kilometres.php @@ -12,7 +12,7 @@ * file that was distributed with this source code. */ -namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallon; +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS; use UnitConverter\Calculator\Formula\AbstractFormula; @@ -24,9 +24,12 @@ */ class ToLitrePer100Kilometres extends AbstractFormula { - const FORMULA_STRING = 'L/100km = 235.215 / mpg'; - const FORMULA_TEMPLATE = '%s L/100km = 235.215 / %smpg'; + const MAGIC_NUMBER = 235.215; + + const FORMULA_STRING = 'L/100km = 235.215 / mpg(US)'; + + const FORMULA_TEMPLATE = '%s L/100km = 235.215 / %smpg(US)'; /** * {@inheritDoc} @@ -34,7 +37,7 @@ class ToLitrePer100Kilometres extends AbstractFormula public function describe($value, $fromUnits, $toUnits, int $precision = null) { // XXX: this formula assumes all calculators can accept strings, as it's the safest type. - $addResult = $this->calculator->div(235.215, $value); + $addResult = $this->calculator->div(self::MAGIC_NUMBER, $value); $result = $this->calculator->round($addResult, $precision); $this->plugVariables($result, $value); diff --git a/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToMilesPerGallonImperial.php b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToMilesPerGallonImperial.php new file mode 100644 index 00000000..319b49be --- /dev/null +++ b/src/UnitConverter/Calculator/Formula/FuelEconomy/MilesPerGallonUS/ToMilesPerGallonImperial.php @@ -0,0 +1,47 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS; + +use UnitConverter\Calculator\Formula\AbstractFormula; + +/** + * Formula to convert Kilometre Per Litre values to Miles Per Gallon. + * + * @version 1.0.0 + * @author Maksim Martianov <7222812+maksimru@users.noreply.github.com> + */ +class ToMilesPerGallonImperial extends AbstractFormula +{ + + const MAGIC_NUMBER = 1.20095; + + const FORMULA_STRING = 'mpg(US) = 1.20095 * mpg(Imperial)'; + + const FORMULA_TEMPLATE = '%s mpg(US) = 1.20095 * %smpg(Imperial)'; + + /** + * {@inheritDoc} + */ + public function describe($value, $fromUnits, $toUnits, int $precision = null) + { + // XXX: this formula assumes all calculators can accept strings, as it's the safest type. + $addResult = $this->calculator->mul(self::MAGIC_NUMBER, $value); + $result = $this->calculator->round($addResult, $precision); + + $this->plugVariables($result, $value); + + return $result; + } +} diff --git a/src/UnitConverter/Measure.php b/src/UnitConverter/Measure.php index cfbfbd70..add872f4 100644 --- a/src/UnitConverter/Measure.php +++ b/src/UnitConverter/Measure.php @@ -40,7 +40,8 @@ use UnitConverter\Unit\Frequency\Terahertz; use UnitConverter\Unit\FuelEconomy\KilometrePerLitre; use UnitConverter\Unit\FuelEconomy\LitrePer100Kilometres; -use UnitConverter\Unit\FuelEconomy\MilesPerGallon; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonImperial; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonUS; use UnitConverter\Unit\Length\AstronomicalUnit; use UnitConverter\Unit\Length\Centimetre; use UnitConverter\Unit\Length\Decimetre; @@ -132,6 +133,8 @@ class Measure const VOLUME = "volume"; + const FUEL_ECONOMY = "fuel_economy"; + /** * An array containing a list of default measurement types that are * supported, and the unit classes they govern. @@ -173,9 +176,12 @@ class Measure Litre::class, Millilitre::class, Pint::class, + ], + self::FUEL_ECONOMY => [ KilometrePerLitre::class, LitrePer100Kilometres::class, - MilesPerGallon::class, + MilesPerGallonUS::class, + MilesPerGallonImperial::class, ], self::MASS => [ Gram::class, diff --git a/src/UnitConverter/Unit/FuelEconomy/FuelEconomyUnit.php b/src/UnitConverter/Unit/FuelEconomy/FuelEconomyUnit.php index 83173d2e..3000da28 100644 --- a/src/UnitConverter/Unit/FuelEconomy/FuelEconomyUnit.php +++ b/src/UnitConverter/Unit/FuelEconomy/FuelEconomyUnit.php @@ -29,5 +29,5 @@ abstract class FuelEconomyUnit extends AbstractUnit { protected $base = KilometrePerLitre::class; - protected $unitOf = Measure::VOLUME; + protected $unitOf = Measure::FUEL_ECONOMY; } diff --git a/src/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.php b/src/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.php index 70106ba7..5e2280ae 100644 --- a/src/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.php +++ b/src/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.php @@ -15,7 +15,8 @@ namespace UnitConverter\Unit\FuelEconomy; use UnitConverter\Calculator\Formula\FuelEconomy\KilometrePerLitre\ToLitrePer100Kilometres; -use UnitConverter\Calculator\Formula\FuelEconomy\KilometrePerLitre\ToMilesPerGallon; +use UnitConverter\Calculator\Formula\FuelEconomy\KilometrePerLitre\ToMilesPerGallonImperial; +use UnitConverter\Calculator\Formula\FuelEconomy\KilometrePerLitre\ToMilesPerGallonUS; use UnitConverter\Calculator\Formula\NullFormula; /** @@ -35,7 +36,8 @@ protected function configure(): void ->addFormulae([ 'L/100km' => ToLitrePer100Kilometres::class, - 'mpg' => ToMilesPerGallon::class, + 'mpg' => ToMilesPerGallonUS::class, + 'mpig' => ToMilesPerGallonImperial::class, 'km/l' => NullFormula::class, ]); } diff --git a/src/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.php b/src/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.php index 07776573..cd6bd42a 100644 --- a/src/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.php +++ b/src/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.php @@ -15,7 +15,8 @@ namespace UnitConverter\Unit\FuelEconomy; use UnitConverter\Calculator\Formula\FuelEconomy\LitrePer100Kilometres\ToKilometrePerLitre; -use UnitConverter\Calculator\Formula\FuelEconomy\LitrePer100Kilometres\ToMilesPerGallon; +use UnitConverter\Calculator\Formula\FuelEconomy\LitrePer100Kilometres\ToMilesPerGallonImperial; +use UnitConverter\Calculator\Formula\FuelEconomy\LitrePer100Kilometres\ToMilesPerGallonUS; use UnitConverter\Calculator\Formula\NullFormula; /** @@ -33,7 +34,8 @@ protected function configure(): void ->setSymbol("L/100km") ->addFormulae([ 'km/l' => ToKilometrePerLitre::class, - 'mpg' => ToMilesPerGallon::class, + 'mpg' => ToMilesPerGallonUS::class, + 'mpig' => ToMilesPerGallonImperial::class, 'L/100km' => NullFormula::class, ]); } diff --git a/src/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.php b/src/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.php new file mode 100644 index 00000000..fc003dcf --- /dev/null +++ b/src/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.php @@ -0,0 +1,44 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Unit\FuelEconomy; + +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial\ToKilometrePerLitre; +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial\ToLitrePer100Kilometres; +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonImperial\ToMilesPerGallonUS; +use UnitConverter\Calculator\Formula\NullFormula; + +/** + * MilesPerGallon (US) unit data class. + * + * @version 1.0.0 + * @since 0.9.0 + */ +class MilesPerGallonImperial extends FuelEconomyUnit +{ + protected function configure(): void + { + $this + ->setName("miles per Imperial gallon") + + ->setSymbol("mpig") + + ->addFormulae([ + 'L/100km' => ToLitrePer100Kilometres::class, + 'km/l' => ToKilometrePerLitre::class, + 'mpg' => ToMilesPerGallonUS::class, + 'mpig' => NullFormula::class, + ]); + } +} diff --git a/src/UnitConverter/Unit/FuelEconomy/MilesPerGallon.php b/src/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.php similarity index 66% rename from src/UnitConverter/Unit/FuelEconomy/MilesPerGallon.php rename to src/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.php index bff1ecaf..5006ece1 100644 --- a/src/UnitConverter/Unit/FuelEconomy/MilesPerGallon.php +++ b/src/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.php @@ -14,8 +14,9 @@ namespace UnitConverter\Unit\FuelEconomy; -use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallon\ToKilometrePerLitre; -use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallon\ToLitrePer100Kilometres; +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS\ToKilometrePerLitre; +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS\ToLitrePer100Kilometres; +use UnitConverter\Calculator\Formula\FuelEconomy\MilesPerGallonUS\ToMilesPerGallonImperial; use UnitConverter\Calculator\Formula\NullFormula; /** @@ -24,18 +25,19 @@ * @version 1.0.0 * @since 0.9.0 */ -class MilesPerGallon extends FuelEconomyUnit +class MilesPerGallonUS extends FuelEconomyUnit { protected function configure(): void { $this - ->setName("miles per gallon") + ->setName("miles per US gallon") ->setSymbol("mpg") ->addFormulae([ 'L/100km' => ToLitrePer100Kilometres::class, 'km/l' => ToKilometrePerLitre::class, + 'mpig' => ToMilesPerGallonImperial::class, 'mpg' => NullFormula::class, ]); } diff --git a/tests/integration/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.spec.php b/tests/integration/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.spec.php index 67c7962a..0756cea4 100644 --- a/tests/integration/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.spec.php +++ b/tests/integration/UnitConverter/Unit/FuelEconomy/KilometrePerLitre.spec.php @@ -19,7 +19,8 @@ use UnitConverter\Registry\UnitRegistry; use UnitConverter\Unit\FuelEconomy\KilometrePerLitre; use UnitConverter\Unit\FuelEconomy\LitrePer100Kilometres; -use UnitConverter\Unit\FuelEconomy\MilesPerGallon; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonImperial; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonUS; use UnitConverter\UnitConverter; /** @@ -42,7 +43,8 @@ protected function setUp() $this->converter = new UnitConverter( new UnitRegistry([ new KilometrePerLitre(), - new MilesPerGallon(), + new MilesPerGallonUS(), + new MilesPerGallonImperial(), new LitrePer100Kilometres(), ]), new SimpleCalculator() @@ -71,7 +73,7 @@ public function assert1KilometrePerLitreIs1KilometrePerLitre() /** * @test */ - public function assert1KilometersPerLitreIs2MilesPerGallon() + public function assert1KilometersPerLitreIs2MilesPerUSGallon() { $expected = 2.35; $actual = $this->converter @@ -82,6 +84,20 @@ public function assert1KilometersPerLitreIs2MilesPerGallon() $this->assertEquals($expected, $actual); } + /** + * @test + */ + public function assert1KilometersPerLitreIs2MilesPerImperialGallon() + { + $expected = 2.82; + $actual = $this->converter + ->convert(1) + ->from("km/l") + ->to("mpig"); + + $this->assertEquals($expected, $actual); + } + /** * @test */ diff --git a/tests/integration/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.spec.php b/tests/integration/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.spec.php index 86d9e666..fbd13cf5 100644 --- a/tests/integration/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.spec.php +++ b/tests/integration/UnitConverter/Unit/FuelEconomy/LitrePer100Kilometres.spec.php @@ -19,7 +19,8 @@ use UnitConverter\Registry\UnitRegistry; use UnitConverter\Unit\FuelEconomy\KilometrePerLitre; use UnitConverter\Unit\FuelEconomy\LitrePer100Kilometres; -use UnitConverter\Unit\FuelEconomy\MilesPerGallon; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonImperial; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonUS; use UnitConverter\UnitConverter; /** @@ -43,8 +44,9 @@ protected function setUp() $this->converter = new UnitConverter( new UnitRegistry([ new KilometrePerLitre(), + new MilesPerGallonUS(), + new MilesPerGallonImperial(), new LitrePer100Kilometres(), - new MilesPerGallon(), ]), new SimpleCalculator() ); @@ -100,7 +102,7 @@ public function assert5LitrePer100KilometresIs20KilometrePerLitre() /** * @test */ - public function assert5LitrePer100KilometresIs47MilesPerGallon() + public function assert5LitrePer100KilometresIs47MilesPerUSGallon() { $expected = 47.04; $actual = $this->converter @@ -111,4 +113,18 @@ public function assert5LitrePer100KilometresIs47MilesPerGallon() $this->assertEquals($expected, $actual); } + /** + * @test + */ + public function assert5LitrePer100KilometresIs56MilesPerImperialGallon() + { + $expected = 56.5; + $actual = $this->converter + ->convert(5) + ->from("L/100km") + ->to("mpig"); + + $this->assertEquals($expected, $actual); + } + } diff --git a/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.spec.php b/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.spec.php new file mode 100644 index 00000000..4a431517 --- /dev/null +++ b/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonImperial.spec.php @@ -0,0 +1,115 @@ + + * @license MIT + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace UnitConverter\Tests\Integration\Unit\FuelEconomy; + +use PHPUnit\Framework\TestCase; +use UnitConverter\Calculator\SimpleCalculator; +use UnitConverter\Registry\UnitRegistry; +use UnitConverter\Unit\FuelEconomy\KilometrePerLitre; +use UnitConverter\Unit\FuelEconomy\LitrePer100Kilometres; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonImperial; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonUS; +use UnitConverter\UnitConverter; + +/** + * + * @covers UnitConverter\Unit\FuelEconomy\MilesPerGallon + * @uses UnitConverter\Unit\FuelEconomy\KilometrePerLitre + * @uses UnitConverter\Unit\AbstractUnit + * @uses UnitConverter\UnitConverter + * @uses UnitConverter\Calculator\SimpleCalculator + * @uses UnitConverter\Calculator\AbstractCalculator + * @uses UnitConverter\Calculator\Formula\AbstractFormula + * @uses UnitConverter\Calculator\Formula\UnitConversionFormula + * @uses UnitConverter\Registry\UnitRegistry + * @uses UnitConverter\Support\ArrayDotNotation + * @uses UnitConverter\Support\Collection + */ +class MilesPerGallonImperialSpec extends TestCase +{ + protected function setUp() + { + $this->converter = new UnitConverter( + new UnitRegistry([ + new KilometrePerLitre(), + new MilesPerGallonUS(), + new MilesPerGallonImperial(), + new LitrePer100Kilometres(), + ]), + new SimpleCalculator() + ); + } + + protected function tearDown() + { + unset($this->converter); + } + + /** + * @test + */ + public function assert1MilesPerImperialGallonIs1MilesPerImperialGallon() + { + $expected = 1; + $actual = $this->converter + ->convert(1) + ->from("mpig") + ->to("mpig"); + + $this->assertEquals($expected, $actual); + } + + /** + * @test + */ + public function assert1MilesPerImperialGallonIs1MilesPerGallon() + { + $expected = 0.83; + $actual = $this->converter + ->convert(1) + ->from("mpig") + ->to("mpg"); + + $this->assertEquals($expected, $actual); + } + + /** + * @test + */ + public function assert1MilesPerImperialGallonIs1KilometrePerLitre() + { + $expected = 0.35; + $actual = $this->converter + ->convert(1) + ->from("mpig") + ->to("km/l"); + + $this->assertEquals($expected, $actual); + } + + /** + * @test + */ + public function assert10MilesPerImperialGallonIs28LitrePer100Kilometres() + { + $expected = 28.25; + $actual = $this->converter + ->convert(10) + ->from("mpig") + ->to("L/100km"); + + $this->assertEquals($expected, $actual); + } +} diff --git a/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallon.spec.php b/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.spec.php similarity index 80% rename from tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallon.spec.php rename to tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.spec.php index 6f083ac4..af5eb1de 100644 --- a/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallon.spec.php +++ b/tests/integration/UnitConverter/Unit/FuelEconomy/MilesPerGallonUS.spec.php @@ -19,7 +19,8 @@ use UnitConverter\Registry\UnitRegistry; use UnitConverter\Unit\FuelEconomy\KilometrePerLitre; use UnitConverter\Unit\FuelEconomy\LitrePer100Kilometres; -use UnitConverter\Unit\FuelEconomy\MilesPerGallon; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonImperial; +use UnitConverter\Unit\FuelEconomy\MilesPerGallonUS; use UnitConverter\UnitConverter; /** @@ -36,15 +37,16 @@ * @uses UnitConverter\Support\ArrayDotNotation * @uses UnitConverter\Support\Collection */ -class MilesPerGallonSpec extends TestCase +class MilesPerGallonUSSpec extends TestCase { protected function setUp() { $this->converter = new UnitConverter( new UnitRegistry([ new KilometrePerLitre(), - new MilesPerGallon(), - new LitrePer100Kilometres() + new MilesPerGallonUS(), + new MilesPerGallonImperial(), + new LitrePer100Kilometres(), ]), new SimpleCalculator() ); @@ -69,6 +71,20 @@ public function assert1MilesPerGallonIs1MilesPerGallon() $this->assertEquals($expected, $actual); } + /** + * @test + */ + public function assert1MilesPerGallonIs1MilesPerImperialGallon() + { + $expected = 1.2; + $actual = $this->converter + ->convert(1) + ->from("mpg") + ->to("mpig"); + + $this->assertEquals($expected, $actual); + } + /** * @test */ diff --git a/tests/unit/UnitConverter/Calculator/BinaryCalculator.spec.php b/tests/unit/UnitConverter/Calculator/BinaryCalculator.spec.php index 1309baf5..f72e2a59 100644 --- a/tests/unit/UnitConverter/Calculator/BinaryCalculator.spec.php +++ b/tests/unit/UnitConverter/Calculator/BinaryCalculator.spec.php @@ -68,7 +68,7 @@ public function assertDivideMethodProperlyDividesTwoNumbers() */ public function assertModulusMethodProperlyReturnsTheRemainderOfDivision() { - $expected = "1"; + $expected = "1.0000"; $actual = $this->calculator->mod("5", "2"); $this->assertEquals($expected, $actual); diff --git a/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php b/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php index 375b0c91..1408723f 100644 --- a/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php +++ b/tests/unit/UnitConverter/Registry/UnitRegistry.spec.php @@ -74,6 +74,7 @@ public function assertListMeasurementsMethodReturnsArray() "length", "area", "volume", + "fuel_economy", "mass", "speed", "plane_angle",