From ef4c6527f075ed8e31c3f94b059798bc7bb026dd Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Mon, 5 Dec 2016 22:34:30 +0100 Subject: [PATCH 1/5] Possible implementation for #62 and temporary workaround for #63 (necessary for #62 unit tests) --- source/AbstractPhysicalQuantity.php | 24 +++++++++ source/PhysicalQuantityInterface.php | 17 +++++++ tests/AbstractPhysicalQuantityTest.php | 69 ++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/source/AbstractPhysicalQuantity.php b/source/AbstractPhysicalQuantity.php index 0d61bbf..607ef28 100644 --- a/source/AbstractPhysicalQuantity.php +++ b/source/AbstractPhysicalQuantity.php @@ -211,6 +211,30 @@ public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity) return get_class($this) === get_class($testQuantity); } + /** + * @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::isUnitDefined + */ + public static function isUnitDefined($name) { + $units = static::getUnitDefinitions(); + foreach ($units as $unit) { + if ($name === $unit->getName() || $unit->isAliasOf($name)) { + return true; + } + } + return false; + } + + /** + * @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::listAllUnits + */ + public static function listAllUnits() { + $return = array(); + $units = static::getUnitDefinitions(); + foreach ($units as $unit) { + $return[$unit->getName()] = $unit->getAliases(); + } + return $return; + } /** * Get the unit definition array diff --git a/source/PhysicalQuantityInterface.php b/source/PhysicalQuantityInterface.php index 63a804b..2cdb671 100644 --- a/source/PhysicalQuantityInterface.php +++ b/source/PhysicalQuantityInterface.php @@ -71,4 +71,21 @@ public function subtract(PhysicalQuantityInterface $quantity); * @return boolean True if the quantities are the same, false if not. */ public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity); + + /** + * Verify if the given value respond to an already defined unit of meaure of the current + * phisical quantity. + * + * @param string $name the string to verify + * + * @return boolean True if $name has been defined into the current physical quantity, false if not. + */ + public static function isUnitDefined($name); + + /** + * Return a list of all the unit of measure defined in the current physical quantity + * + * @return array of all units as strings. + */ + public static function listAllUnits(); } diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index 19ce3f2..bed713f 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -5,6 +5,7 @@ use PHPUnit_Framework_TestCase; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; use PhpUnitsOfMeasure\UnitOfMeasureInterface; +use PhpUnitsOfMeasure\UnitOfMeasure; // can be removed after #63 is closed use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch; use PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias; use PhpUnitsOfMeasure\Exception\NonNumericValue; @@ -33,6 +34,28 @@ protected function getTestUnitOfMeasure($name, $aliases = []) return $newUnit; } + + /** + * This function is a workaround introduced for #63, waiting for a most suitable solution. + * When #63 is closed, this method can be removed, and any call to its should be + * replaced with calls to the original $this->getTestUnitOfMeasure(...) + */ + protected function getTestUnitOfMeasureSafe($name, $aliases = []) + { + $newUnit = new UnitOfMeasure( + $name, + function ($valueInNativeUnit) { + return $valueInNativeUnit / 1; + }, + function ($valueInThisUnit) { + return $valueInThisUnit * 1; + } + ); + foreach ($aliases as $alias) { + $newUnit->addAlias($alias); + } + return $newUnit; + } /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit @@ -217,6 +240,52 @@ public function testGetAllUnits() $this->assertEquals($array, $expected); } + /** + * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::isUnitDefined + */ + public function testIsUnitDefined() + { + /* The following code still doesn't work: see #63. + * It is possible to enable this line (instead of the line after) to verify if #63 has been closed + + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + */ + $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + Wonkicity::addUnit($newUnit); + + $someExistingUnits = array('u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'); + $unexistingUnits = array('kg', 'l', 'definitelynoconflict_'); + + foreach ($someExistingUnits as $someExistingUnit) { + $this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit)); + } + foreach ($unexistingUnits as $unexistingUnit) { + $this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit)); + } + } + + /** + * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::listAllUnits + */ + public function testListAllUnits() + { + /* The following code still doesn't work: see #63. + * It is possible to enable this line (instead of the line after) to verify if #63 has been closed + + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + */ + $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + Wonkicity::addUnit($newUnit); + + $allUnits = Wonkicity::listAllUnits(); + $expected = array( + 'u' => array('uvee', 'uvees'), + 'v' => array('vorp', 'vorps'), + 'noconflict' => array('definitelynoconflict_1', 'definitelynoconflict_2'), + ); + $this->assertEquals($allUnits, $expected); + } + /** * Attempting to register these units should throw a DuplicateUnitNameOrAlias. * 1) The name of the new unit to test From a8ddf712fbb78869daf0cb0b2289d40b4c84cc00 Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Mon, 5 Dec 2016 22:45:09 +0100 Subject: [PATCH 2/5] Improved documentation for #62 --- source/PhysicalQuantityInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/PhysicalQuantityInterface.php b/source/PhysicalQuantityInterface.php index 2cdb671..c1e8ef9 100644 --- a/source/PhysicalQuantityInterface.php +++ b/source/PhysicalQuantityInterface.php @@ -86,6 +86,8 @@ public static function isUnitDefined($name); * Return a list of all the unit of measure defined in the current physical quantity * * @return array of all units as strings. + * Keys of the array are the units of measure; for any key the value is + * another array containing all aliases. */ public static function listAllUnits(); } From 6a22a9c5df21a4d3c568d10c88d5e9c7225ff376 Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Mon, 5 Dec 2016 22:54:57 +0100 Subject: [PATCH 3/5] Fixed coding standard errors for #62 --- source/AbstractPhysicalQuantity.php | 32 ++++----- source/PhysicalQuantityInterface.php | 10 +-- tests/AbstractPhysicalQuantityTest.php | 90 +++++++++++++------------- 3 files changed, 67 insertions(+), 65 deletions(-) diff --git a/source/AbstractPhysicalQuantity.php b/source/AbstractPhysicalQuantity.php index 607ef28..568b7fa 100644 --- a/source/AbstractPhysicalQuantity.php +++ b/source/AbstractPhysicalQuantity.php @@ -214,26 +214,28 @@ public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity) /** * @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::isUnitDefined */ - public static function isUnitDefined($name) { - $units = static::getUnitDefinitions(); - foreach ($units as $unit) { - if ($name === $unit->getName() || $unit->isAliasOf($name)) { - return true; - } - } - return false; + public static function isUnitDefined($name) + { + $units = static::getUnitDefinitions(); + foreach ($units as $unit) { + if ($name === $unit->getName() || $unit->isAliasOf($name)) { + return true; + } + } + return false; } /** * @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::listAllUnits */ - public static function listAllUnits() { - $return = array(); - $units = static::getUnitDefinitions(); - foreach ($units as $unit) { - $return[$unit->getName()] = $unit->getAliases(); - } - return $return; + public static function listAllUnits() + { + $return = array(); + $units = static::getUnitDefinitions(); + foreach ($units as $unit) { + $return[$unit->getName()] = $unit->getAliases(); + } + return $return; } /** diff --git a/source/PhysicalQuantityInterface.php b/source/PhysicalQuantityInterface.php index c1e8ef9..8f9071e 100644 --- a/source/PhysicalQuantityInterface.php +++ b/source/PhysicalQuantityInterface.php @@ -75,19 +75,19 @@ public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity); /** * Verify if the given value respond to an already defined unit of meaure of the current * phisical quantity. - * + * * @param string $name the string to verify - * + * * @return boolean True if $name has been defined into the current physical quantity, false if not. */ public static function isUnitDefined($name); /** * Return a list of all the unit of measure defined in the current physical quantity - * + * * @return array of all units as strings. - * Keys of the array are the units of measure; for any key the value is - * another array containing all aliases. + * Keys of the array are the units of measure; for any key the value is + * another array containing all aliases. */ public static function listAllUnits(); } diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index bed713f..e6d066a 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -42,19 +42,19 @@ protected function getTestUnitOfMeasure($name, $aliases = []) */ protected function getTestUnitOfMeasureSafe($name, $aliases = []) { - $newUnit = new UnitOfMeasure( - $name, - function ($valueInNativeUnit) { - return $valueInNativeUnit / 1; - }, - function ($valueInThisUnit) { - return $valueInThisUnit * 1; - } - ); - foreach ($aliases as $alias) { - $newUnit->addAlias($alias); - } - return $newUnit; + $newUnit = new UnitOfMeasure( + $name, + function ($valueInNativeUnit) { + return $valueInNativeUnit / 1; + }, + function ($valueInThisUnit) { + return $valueInThisUnit * 1; + } + ); + foreach ($aliases as $alias) { + $newUnit->addAlias($alias); + } + return $newUnit; } /** @@ -245,23 +245,23 @@ public function testGetAllUnits() */ public function testIsUnitDefined() { - /* The following code still doesn't work: see #63. - * It is possible to enable this line (instead of the line after) to verify if #63 has been closed - - $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - */ - $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - Wonkicity::addUnit($newUnit); - - $someExistingUnits = array('u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'); - $unexistingUnits = array('kg', 'l', 'definitelynoconflict_'); - - foreach ($someExistingUnits as $someExistingUnit) { - $this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit)); - } - foreach ($unexistingUnits as $unexistingUnit) { - $this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit)); - } + /* The following code still doesn't work: see #63. + * It is possible to enable this line (instead of the line after) to verify if #63 has been closed + + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + */ + $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + Wonkicity::addUnit($newUnit); + + $someExistingUnits = array('u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'); + $unexistingUnits = array('kg', 'l', 'definitelynoconflict_'); + + foreach ($someExistingUnits as $someExistingUnit) { + $this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit)); + } + foreach ($unexistingUnits as $unexistingUnit) { + $this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit)); + } } /** @@ -269,21 +269,21 @@ public function testIsUnitDefined() */ public function testListAllUnits() { - /* The following code still doesn't work: see #63. - * It is possible to enable this line (instead of the line after) to verify if #63 has been closed - - $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - */ - $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - Wonkicity::addUnit($newUnit); - - $allUnits = Wonkicity::listAllUnits(); - $expected = array( - 'u' => array('uvee', 'uvees'), - 'v' => array('vorp', 'vorps'), - 'noconflict' => array('definitelynoconflict_1', 'definitelynoconflict_2'), - ); - $this->assertEquals($allUnits, $expected); + /* The following code still doesn't work: see #63. + * It is possible to enable this line (instead of the line after) to verify if #63 has been closed + + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + */ + $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + Wonkicity::addUnit($newUnit); + + $allUnits = Wonkicity::listAllUnits(); + $expected = array( + 'u' => array('uvee', 'uvees'), + 'v' => array('vorp', 'vorps'), + 'noconflict' => array('definitelynoconflict_1', 'definitelynoconflict_2'), + ); + $this->assertEquals($allUnits, $expected); } /** From d6d2f9ca93ba44b0d340da08396a44ea1231fd3e Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Mon, 6 Feb 2017 10:27:46 +0100 Subject: [PATCH 4/5] Modifications required in the pull request 64 --- source/AbstractPhysicalQuantity.php | 2 +- tests/AbstractPhysicalQuantityTest.php | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/AbstractPhysicalQuantity.php b/source/AbstractPhysicalQuantity.php index 568b7fa..1b4f3a2 100644 --- a/source/AbstractPhysicalQuantity.php +++ b/source/AbstractPhysicalQuantity.php @@ -230,7 +230,7 @@ public static function isUnitDefined($name) */ public static function listAllUnits() { - $return = array(); + $return = []; $units = static::getUnitDefinitions(); foreach ($units as $unit) { $return[$unit->getName()] = $unit->getAliases(); diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index e6d066a..b8095f3 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -253,14 +253,14 @@ public function testIsUnitDefined() $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); Wonkicity::addUnit($newUnit); - $someExistingUnits = array('u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'); - $unexistingUnits = array('kg', 'l', 'definitelynoconflict_'); + $someExistingUnits = ['u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2']; + $unexistingUnits = ['kg', 'l', 'definitelynoconflict_']; foreach ($someExistingUnits as $someExistingUnit) { - $this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit)); + $this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit), "$someExistingUnit is not defined"); } foreach ($unexistingUnits as $unexistingUnit) { - $this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit)); + $this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit), "$unexistingUnit is not defined"); } } @@ -278,11 +278,10 @@ public function testListAllUnits() Wonkicity::addUnit($newUnit); $allUnits = Wonkicity::listAllUnits(); - $expected = array( - 'u' => array('uvee', 'uvees'), - 'v' => array('vorp', 'vorps'), - 'noconflict' => array('definitelynoconflict_1', 'definitelynoconflict_2'), - ); + $expected = []; + $expected['u'] = ['uvee', 'uvees']; + $expected['v'] = ['vorp', 'vorps']; + $expected['noconflict'] = ['definitelynoconflict_1', 'definitelynoconflict_2']; $this->assertEquals($allUnits, $expected); } From 45b8b96dd91abac3b3a0386e2c78a10da52177e7 Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Mon, 6 Feb 2017 10:40:52 +0100 Subject: [PATCH 5/5] Removed all code related to the workaround for #63 --- tests/AbstractPhysicalQuantityTest.php | 43 +++++--------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index b8095f3..bbda2ed 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -5,7 +5,6 @@ use PHPUnit_Framework_TestCase; use PhpUnitsOfMeasure\AbstractPhysicalQuantity; use PhpUnitsOfMeasure\UnitOfMeasureInterface; -use PhpUnitsOfMeasure\UnitOfMeasure; // can be removed after #63 is closed use PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch; use PhpUnitsOfMeasure\Exception\DuplicateUnitNameOrAlias; use PhpUnitsOfMeasure\Exception\NonNumericValue; @@ -31,31 +30,15 @@ protected function getTestUnitOfMeasure($name, $aliases = []) ->willReturn($name); $newUnit->method('getAliases') ->willReturn($aliases); + $newUnit->method('isAliasOf') + ->will($this->returnCallback( + function ($value) use ($aliases) { + return in_array($value, $aliases); + } + )); return $newUnit; } - - /** - * This function is a workaround introduced for #63, waiting for a most suitable solution. - * When #63 is closed, this method can be removed, and any call to its should be - * replaced with calls to the original $this->getTestUnitOfMeasure(...) - */ - protected function getTestUnitOfMeasureSafe($name, $aliases = []) - { - $newUnit = new UnitOfMeasure( - $name, - function ($valueInNativeUnit) { - return $valueInNativeUnit / 1; - }, - function ($valueInThisUnit) { - return $valueInThisUnit * 1; - } - ); - foreach ($aliases as $alias) { - $newUnit->addAlias($alias); - } - return $newUnit; - } /** * @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::addUnit @@ -245,12 +228,7 @@ public function testGetAllUnits() */ public function testIsUnitDefined() { - /* The following code still doesn't work: see #63. - * It is possible to enable this line (instead of the line after) to verify if #63 has been closed - - $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - */ - $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); Wonkicity::addUnit($newUnit); $someExistingUnits = ['u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2']; @@ -269,12 +247,7 @@ public function testIsUnitDefined() */ public function testListAllUnits() { - /* The following code still doesn't work: see #63. - * It is possible to enable this line (instead of the line after) to verify if #63 has been closed - - $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); - */ - $newUnit = $this->getTestUnitOfMeasureSafe('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); + $newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']); Wonkicity::addUnit($newUnit); $allUnits = Wonkicity::listAllUnits();