Skip to content

Commit

Permalink
Throw exception if more than one value is passed to a unit upon initi…
Browse files Browse the repository at this point in the history
…alization
  • Loading branch information
griffithben committed Jul 28, 2023
1 parent a32a569 commit dede5fc
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ preference set, but can be overridden when instantiating a new unit.
'Temperature' => 'Fahrenheit',
'Volume' => 'Gallon',
'Pressure' => 'Psi',
'Weight' => 'Pound'
'Weight' => 'Pound',
'Color' => 'Srm',
]
```
Expand Down
16 changes: 16 additions & 0 deletions src/AbstractUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public function getValue(?int $round = null): float
throw new RuntimeException("Unit '$gatherMethod' does not exist.");
}

/**
* @param array $values
* @return bool
*/
public function hasOnlyOneValue(array $values): bool
{
$count = 0;
foreach ($values as $value) {
if (!is_null($value)) {
$count++;
}
}

return $count === 1;
}

/**
* @return string
* @throws \RuntimeException
Expand Down
4 changes: 4 additions & 0 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function __construct(
?float $lovibond = null,
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$srm, $ebc, $lovibond])) {
throw new \InvalidArgumentException('Only one Color type can be set at a time.');
}

parent::__construct($preferences);

if ($srm) {
Expand Down
16 changes: 16 additions & 0 deletions src/Gravity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function __construct(
float $brix = null,
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$plato, $specificGravity, $brix])) {
throw new \InvalidArgumentException('Only one Gravity type can be set at a time.');
}

parent::__construct($preferences);

if ($plato) {
Expand Down Expand Up @@ -97,18 +101,30 @@ public function setBrix(float $brix): self
return $this;
}

/**
* @param float $plato
* @return float
*/
public static function convertPlatoToSpecificGravity(float $plato): float
{
return 259 / (259 - $plato);
}

/**
* @param float $plato
* @return float
*/
public static function convertPlatoToBrix(float $plato): float
{
$specificGravity = self::convertPlatoToSpecificGravity($plato);

return self::convertSpecificGravityToBrix($specificGravity);
}

/**
* @param float $specificGravity
* @return float
*/
public static function convertSpecificGravityToPlato(float $specificGravity): float
{
return 259 - (259 / $specificGravity);
Expand Down
4 changes: 4 additions & 0 deletions src/Pressure.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Pressure extends AbstractUnit

public function __construct(?float $bar = null, ?float $psi = null, array $preferences = [])
{
if (!$this->hasOnlyOneValue([$bar, $psi])) {
throw new \InvalidArgumentException('Only one Pressure type can be set at a time.');
}

parent::__construct($preferences);

if ($bar) {
Expand Down
4 changes: 4 additions & 0 deletions src/Temperature.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Temperature extends AbstractUnit

public function __construct(float $fahrenheit = null, float $celsius = null, array $preferences = [])
{
if (!$this->hasOnlyOneValue([$fahrenheit, $celsius])) {
throw new \InvalidArgumentException('Only one Temperature type can be set at a time.');
}

parent::__construct($preferences);

if ($fahrenheit) {
Expand Down
4 changes: 4 additions & 0 deletions src/Volume.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function __construct(
float $hectoliter = null,
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$ounce, $gallon, $barrel, $milliliter, $liter, $hectoliter])) {
throw new \InvalidArgumentException('Only one Volume type can be set at a time.');
}

parent::__construct($preferences);

if ($ounce) {
Expand Down
4 changes: 4 additions & 0 deletions src/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public function __construct(
?float $kilogram = null,
array $preferences = []
) {
if (!$this->hasOnlyOneValue([$ounce, $pound, $gram, $kilogram])) {
throw new \InvalidArgumentException('Only one Weight type can be set at a time.');
}

parent::__construct($preferences);

if ($ounce) {
Expand Down
16 changes: 16 additions & 0 deletions tests/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,20 @@ public function testSetLovibondWillReturnSrmWithGetValueAndDefaultPreferences():

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Color type can be set at a time.');

new Color();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Color type can be set at a time.');

new Color(srm: self::TEST_SRM, ebc: self::TEST_EBC);
}
}
16 changes: 16 additions & 0 deletions tests/GravityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,20 @@ public function testSetBrixWillReturnBrixWithGetBrix(): void

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Gravity type can be set at a time.');

new Gravity();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Gravity type can be set at a time.');

new Gravity(plato: self::TEST_PLATO, specificGravity: self::TEST_SPECIFIC_GRAVITY);
}
}
16 changes: 16 additions & 0 deletions tests/PressureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ public function testSetBarWillReturnBarWithGetBar(): void

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Pressure type can be set at a time.');

new Pressure();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Pressure type can be set at a time.');

new Pressure(bar: self::TEST_BAR, psi: self::TEST_PSI);
}
}
16 changes: 16 additions & 0 deletions tests/TemperatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ public function testSetCelsiusWillReturnCelsiusWithGetCelsius(): void

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Temperature type can be set at a time.');

new Temperature();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Temperature type can be set at a time.');

new Temperature(celsius: self::TEST_CELSIUS, fahrenheit: self::TEST_FAHRENHEIT);
}
}
16 changes: 16 additions & 0 deletions tests/VolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,20 @@ public function testSetHectoliterWillReturnHectoliterWithGetHectoliter(): void

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Volume type can be set at a time.');

new Volume();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Volume type can be set at a time.');

new Volume(gallon: self::TEST_GALLON, liter: self::TEST_LITER);
}
}
16 changes: 16 additions & 0 deletions tests/WeightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,20 @@ public function testSetKilogramWillReturnKilogramWithGetKilogram(): void

$this->assertEquals($expected, $actual);
}

public function testWillThrowExceptionWithNoValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Weight type can be set at a time.');

new Weight();
}

public function testWillThrowExceptionWithTooManyValuesSet(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one Weight type can be set at a time.');

new Weight(pound: self::TEST_POUND, ounce: self::TEST_OUNCE);
}
}

0 comments on commit dede5fc

Please sign in to comment.