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
14 changes: 14 additions & 0 deletions src/RegistryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ protected static function initEnergy(Registry $registry): void
new UnitPart(1, Dimension::TIME, -2)
)
);

static::registerSiUnit(
$registry,
'watt-hour',
['Wh'],
new Unit(
// watt
new UnitPart(1, Dimension::MASS, 1),
new UnitPart(1, Dimension::LENGTH, 2),
new UnitPart(1, Dimension::TIME, -3),
// hour
new UnitPart(3600, Dimension::TIME, 1)
)
);
}

protected static function initLength(Registry $registry): void
Expand Down
33 changes: 19 additions & 14 deletions tests/EnergyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,46 @@

use PHPUnit\Framework\TestCase;
use Vesper\UnitConversion\Converter;
use Vesper\UnitConversion\Dimension;
use Vesper\UnitConversion\Registry;
use Vesper\UnitConversion\RegistryBuilder;
use Vesper\UnitConversion\Unit;
use Vesper\UnitConversion\UnitPart;

final class EnergyTest extends TestCase
{
protected Unit $cal;
protected Unit $joule;
protected Unit $watthour;
protected Converter $converter;

protected function setUp(): void
{
parent::setUp();

$this->cal = new Unit(
new UnitPart(4.184, Dimension::MASS, 1),
new UnitPart(1, Dimension::LENGTH, 2),
new UnitPart(1, Dimension::TIME, -2)
);
$this->joule = new Unit(
new UnitPart(1, Dimension::MASS, 1),
new UnitPart(1, Dimension::LENGTH, 2),
new UnitPart(1, Dimension::TIME, -2),
);
$registry = new Registry();

RegistryBuilder::build($registry);

$this->cal = $registry->get('calorie');

$this->joule = $registry->get('joule');

$this->watthour = $registry->get('watt-hour');

$this->converter = new Converter();
}

public function test_cal_to_joule(): void
{
$this->assertEqualsWithDelta(4.184, $this->converter->convert($this->cal, $this->joule, 1), 0.0000001);
$this->assertEqualsWithDelta(4.1868, $this->converter->convert($this->cal, $this->joule, 1), 0.0000001);
}

public function test_joule_to_cal(): void
{
$this->assertEqualsWithDelta(1 / 4.184, $this->converter->convert($this->joule, $this->cal, 1), 0.0000001);
$this->assertEqualsWithDelta(1, $this->converter->convert($this->joule, $this->cal, 4.1868), 0.0000001);
}

public function test_joule_to_watt_hour(): void
{
$this->assertEqualsWithDelta(1, $this->converter->convert($this->joule, $this->watthour, 3600), 0.0000001);
}
}