Skip to content

Commit

Permalink
Possible implementation for PhpUnitsOfMeasure#62 and temporary workar…
Browse files Browse the repository at this point in the history
…ound for PhpUnitsOfMeasure#63 (necessary for PhpUnitsOfMeasure#62 unit tests)
  • Loading branch information
pierpaolocira committed Dec 5, 2016
1 parent 96d3695 commit ef4c652
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
24 changes: 24 additions & 0 deletions source/AbstractPhysicalQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions source/PhysicalQuantityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
69 changes: 69 additions & 0 deletions tests/AbstractPhysicalQuantityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef4c652

Please sign in to comment.