Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions res/units-tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,14 @@
'parent' => 'power',
'factor' => 1000,
),
'rps' =>
array (
'parent' => '',
'factor' => 1,
),
'krps' =>
array (
'parent' => 'rps',
'factor' => 1000,
),
);
3 changes: 3 additions & 0 deletions res/units-tree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ power:
W: 1
kw: 1000
kW: 1000

rps:
krps: 1000
11 changes: 11 additions & 0 deletions tests/unit/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected function setUp(): void

$this->usd = Unit::usd();
$this->eur = Unit::eur();

$this->rps = Unit::rps();
$this->krps = Unit::krps();
}

public function testSame()
Expand Down Expand Up @@ -106,6 +109,14 @@ public function testConvertThroughRoot()
$this->assertConvert(1000, $this->mb, $this->gb);
}

public function testConvertFractional()
{
// assertConvert() only compares integer factors; a request rate price is
// often fractional once converted to its billing unit (1500 rps = 1.5 krps).
$this->assertSame(1.5, $this->rps->convert($this->krps, 1500));
$this->assertSame(1500.0, $this->krps->convert($this->rps, 1.5));
}

public function testUnknownUnit()
{
$unknown = Unit::create('unknown');
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/UnitsTreeConsistencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* PHP Units of Measure Library
*
* @link https://github.com/hiqdev/php-units
* @package php-units
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\php\units\tests;

use Symfony\Component\Yaml\Yaml;

/**
* There is no generator for units-tree.php: it is hand-maintained alongside
* units-tree.yml. This test ensures the two never drift apart.
*/
class UnitsTreeConsistencyTest extends \PHPUnit\Framework\TestCase
{
private const YML_PATH = __DIR__ . '/../../res/units-tree.yml';
private const PHP_PATH = __DIR__ . '/../../res/units-tree.php';

public function testPhpTreeMatchesYamlTree(): void
{
$yaml = Yaml::parseFile(self::YML_PATH);
$php = require self::PHP_PATH;

$expected = [];
foreach ($yaml as $root => $children) {
// Every yaml root is also a directly usable unit, with itself as parent.
$expected[$root] = ['parent' => '', 'factor' => 1];
foreach ($children as $name => $factor) {
$expected[$name] = ['parent' => $root, 'factor' => $factor];
}
}

ksort($expected);
$actual = $php;
ksort($actual);

$this->assertSame($expected, $actual, 'units-tree.yml and units-tree.php are out of sync; there is no generator, so both must be edited by hand.');
}
}
Loading