Skip to content

Commit

Permalink
Merge pull request #64 from pierpaolocira/php-units-of-measure-issue-62
Browse files Browse the repository at this point in the history
Possible implementation for #62 and temporary workaround for #63
  • Loading branch information
triplepoint authored Jul 26, 2017
2 parents 212a332 + a0fe8e0 commit 6107236
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
26 changes: 26 additions & 0 deletions source/AbstractPhysicalQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ 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 = [];
$units = static::getUnitDefinitions();
foreach ($units as $unit) {
$return[$unit->getName()] = $unit->getAliases();
}
return $return;
}

/**
* Get the unit definition array
Expand Down
19 changes: 19 additions & 0 deletions source/PhysicalQuantityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,23 @@ 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.
* Keys of the array are the units of measure; for any key the value is
* another array containing all aliases.
*/
public static function listAllUnits();
}
35 changes: 35 additions & 0 deletions tests/AbstractPhysicalQuantityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,41 @@ public function testGetAllUnits()
$this->assertEquals($array, $expected);
}

/**
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::isUnitDefined
*/
public function testIsUnitDefined()
{
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
Wonkicity::addUnit($newUnit);

$someExistingUnits = ['u', 'uvees', 'v', 'vorp', 'noconflict', 'definitelynoconflict_1', 'definitelynoconflict_2'];
$unexistingUnits = ['kg', 'l', 'definitelynoconflict_'];

foreach ($someExistingUnits as $someExistingUnit) {
$this->assertTrue(Wonkicity::isUnitDefined($someExistingUnit), "$someExistingUnit is not defined");
}
foreach ($unexistingUnits as $unexistingUnit) {
$this->assertFalse(Wonkicity::isUnitDefined($unexistingUnit), "$unexistingUnit is not defined");
}
}

/**
* @covers \PhpUnitsOfMeasure\AbstractPhysicalQuantity::listAllUnits
*/
public function testListAllUnits()
{
$newUnit = $this->getTestUnitOfMeasure('noconflict', ['definitelynoconflict_1', 'definitelynoconflict_2']);
Wonkicity::addUnit($newUnit);

$allUnits = Wonkicity::listAllUnits();
$expected = [];
$expected['u'] = ['uvee', 'uvees'];
$expected['v'] = ['vorp', 'vorps'];
$expected['noconflict'] = ['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
Expand Down

0 comments on commit 6107236

Please sign in to comment.