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