Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobFitzp committed Apr 15, 2023
1 parent bae0a33 commit 1458ca1
Show file tree
Hide file tree
Showing 18 changed files with 530 additions and 105 deletions.
54 changes: 33 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@

Simple PHP package to convert between different units of weight measurement.

Supports gram, kilogram, milligram, ounce, pound, stone and ton... and can easily be extended to add additional units.

```php
echo (new Pound(27.8))
->to(Kilogram::class)
->round()
->formatted()

// 12.6kg
```

## Installation

You can install the package via composer:
Expand All @@ -28,34 +39,35 @@ $kilograms->setAmount(5.75);
$ounce = $kilograms->to(Ounce::class);

// Get weight amount
$ounce->getAmount();
```
$ounce->amount();

Supported measurement units:
// Round the amount
// Defaults to a precision of 1 decimal place
$ounce->round();

```php
// Gram
\JacobFitzp\WeightConversions\Units\Gram::class;

// Kilogram
\JacobFitzp\WeightConversions\Units\Kilogram::class;

// Milligram
\JacobFitzp\WeightConversions\Units\Milligram::class;

// Ounce
\JacobFitzp\WeightConversions\Units\Ounce::class;
// Get a formatted human-readable version of the amount
// For example "2kgG", "12st 5"
$ounce->formatted();
```

// Pound
\JacobFitzp\WeightConversions\Units\Pound::class;
### Extending

// Stone
\JacobFitzp\WeightConversions\Units\Stone::class;
You can create your own units of measurement by extending the `AbstractUnit` class:

// Ton
\JacobFitzp\WeightConversions\Units\Ton::class;
```php
class Hobnobs extends AbstractUnit
{
public const RELATIVE_TO_KG = 50;

public function formatted(string $format = '%s hobnobs'): string
{
return sprintf($format, $this->amount());
}
}
```

Let's break down how this works... The main thing to be aware of is the `RELATIVE_TO_KG` constant, this represents the amount of the unit that is relative to 1kg - For exampe, above we are saying that 50 hobnobs = 1kg. This is used as the basis for all conversion calculations.

## Testing

```bash
Expand Down
41 changes: 36 additions & 5 deletions src/Units/AbstractUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,29 @@ abstract class AbstractUnit
*
* @var float|int
*/
protected float $amountInKilograms = 0;
public float|int $amountInKilograms = 0;
protected false|int $roundingPrecision = false;

public function __construct(protected float $amount = 0)
public function __construct(protected int|float $amount = 0)
{
$this->calculateAmountInKilograms();
}

/**
* Get weight amount for this unit
*
* @return float
* @return int|float
*/
public function getAmount(): float
public function amount(): int|float
{
// With rounding
if (is_int($this->roundingPrecision)) {
return round(
$this->amount,
$this->roundingPrecision
);
}

return $this->amount;
}

Expand Down Expand Up @@ -61,6 +70,28 @@ public function to(string $unitType): AbstractUnit
return new $unitType($unitType::RELATIVE_TO_KG * $this->amountInKilograms);
}

/**
* Round the amount
*
* @param false|int $precision
* @return $this
*/
public function round(false|int $precision = 1): AbstractUnit
{
$this->roundingPrecision = $precision;

return $this;
}

/**
* Get a formatted human-readable version of the amount
* For example "23KG", "12st 5"
*
* @param string $format
* @return string
*/
abstract public function formatted(string $format = ''): string;

/**
* Calculate the amount in kilograms.
* This is used as the basis for all conversions
Expand All @@ -69,6 +100,6 @@ public function to(string $unitType): AbstractUnit
*/
protected function calculateAmountInKilograms(): void
{
$this->amountInKilograms = self::RELATIVE_TO_KG * $this->amount;
$this->amountInKilograms = $this->amount() / $this::RELATIVE_TO_KG;
}
}
5 changes: 5 additions & 0 deletions src/Units/Gram.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Gram extends AbstractUnit
{
public const RELATIVE_TO_KG = 1000;

public function formatted(string $format = '%sg'): string
{
return sprintf($format, $this->amount());
}
}
5 changes: 5 additions & 0 deletions src/Units/Kilogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Kilogram extends AbstractUnit
{
public const RELATIVE_TO_KG = 1;

public function formatted(string $format = '%skg'): string
{
return sprintf($format, $this->amount());
}
}
5 changes: 5 additions & 0 deletions src/Units/Milligram.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Milligram extends AbstractUnit
{
public const RELATIVE_TO_KG = 1000000;

public function formatted(string $format = '%smg'): string
{
return sprintf($format, $this->amount());
}
}
5 changes: 5 additions & 0 deletions src/Units/Ounce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Ounce extends AbstractUnit
{
public const RELATIVE_TO_KG = 35.274;

public function formatted(string $format = '%soz'): string
{
return sprintf($format, $this->amount());
}
}
5 changes: 5 additions & 0 deletions src/Units/Pound.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Pound extends AbstractUnit
{
public const RELATIVE_TO_KG = 2.2046;

public function formatted(string $format = '%slb'): string
{
return sprintf($format, $this->amount());
}
}
18 changes: 18 additions & 0 deletions src/Units/Stone.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,22 @@
class Stone extends AbstractUnit
{
public const RELATIVE_TO_KG = 0.15747;

/**
* Get formatted version which can be displayed to user
* For example "12st 6"
*
* @param string $format
* @return string
*/
public function formatted(string $format = '%sst %s'): string
{
$stones = floor($this->amount());
$pounds = (new self($this->amount - $stones))
->to(Pound::class)
->round(0)
->amount();

return sprintf($format, $stones, $pounds);
}
}
5 changes: 5 additions & 0 deletions src/Units/Ton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
class Ton extends AbstractUnit
{
public const RELATIVE_TO_KG = 0.001;

public function formatted(string $format = '%sT'): string
{
return sprintf($format, $this->amount());
}
}
79 changes: 0 additions & 79 deletions tests/ConversionTest.php

This file was deleted.

70 changes: 70 additions & 0 deletions tests/Units/AbstractUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace JacobFitzp\WeightConversions\Tests\Units;

use JacobFitzp\WeightConversions\Units\AbstractUnit;
use JacobFitzp\WeightConversions\Units\Kilogram;
use PHPUnit\Framework\TestCase;

class AbstractUnitTest extends TestCase
{
public function test_it_can_get_amount(): void
{
$hobnobs = new Hobnob(25);

$this->assertEquals(25, $hobnobs->amount());
}

public function test_it_can_set_amount(): void
{
$hobnobs = new Hobnob(25);
$hobnobs->setAmount(50);

$this->assertEquals(50, $hobnobs->amount());
}

public function test_it_can_convert_to_kg(): void
{
$kg = (new Hobnob(75))
->to(Kilogram::class);

$this->assertInstanceOf(Kilogram::class, $kg);
$this->assertEquals(1.5, $kg->amount());
}

public function test_it_can_convert_from_kg(): void
{
$hobnobs = (new Kilogram(2))
->to(Hobnob::class);

$this->assertInstanceOf(Hobnob::class, $hobnobs);
$this->assertEquals(100, $hobnobs->amount());
}

public function test_it_can_format_amount(): void
{
$hobnobs = new Hobnob(25);

$this->assertEquals('25 hobnobs', $hobnobs->formatted());
}

public function test_it_can_round_amount(): void
{
$hobnobs = new Hobnob(25.1234);

$this->assertEquals(25.1, $hobnobs->round()->amount());
$this->assertEquals(25.12, $hobnobs->round(2)->amount());
$this->assertEquals(25.123, $hobnobs->round(3)->amount());
$this->assertEquals(25.1234, $hobnobs->round(4)->amount());
}
}

class Hobnob extends AbstractUnit
{
public const RELATIVE_TO_KG = 50;

public function formatted(string $format = '%s hobnobs'): string
{
return sprintf($format, $this->amount());
}
}
Loading

0 comments on commit 1458ca1

Please sign in to comment.