Skip to content

Commit

Permalink
BAU-3545 correct return type for AmountMoney/Pence classes (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
orzeuek authored Mar 1, 2018
1 parent 0a2a49a commit 3cf9b55
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/AmountMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function fromHumanReadableString(string $amount): AmountMoney
$big = intval($parts[0]);
$small = count($parts) > 1 ? intval(substr($parts[1].'00', 0, 2)) : 0;

return new self($big * 100 + $small);
return new static($big * 100 + $small);
}

/**
Expand All @@ -49,7 +49,7 @@ public function __construct($value)
*/
public function minus(AmountMoney $amount): AmountMoney
{
return new self($this->value - $amount->getValue());
return new static($this->value - $amount->getValue());
}

/**
Expand All @@ -58,7 +58,7 @@ public function minus(AmountMoney $amount): AmountMoney
*/
public function plus(AmountMoney $amount): AmountMoney
{
return new self($this->value + $amount->getValue());
return new static($this->value + $amount->getValue());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/AmountPence.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AmountPence extends AmountMoney
*/
public function minus(AmountMoney $amount): AmountMoney
{
return new self($this->value - $amount->getValue());
return new static($this->value - $amount->getValue());
}

/**
Expand All @@ -26,7 +26,7 @@ public function minus(AmountMoney $amount): AmountMoney
*/
public function plus(AmountMoney $amount): AmountMoney
{
return new self($this->value + $amount->getValue());
return new static($this->value + $amount->getValue());
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/AmountMoneyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Assertis\Util;

use PHPUnit\Framework\TestCase;

/**
* @author Rafał Orłowski <[email protected]>
*/
class AmountMoneyTest extends TestCase
{

/**
* @return array
*/
public function provideFromHumanReadableString(): array
{
return [
['123.456', 12345],
['123.45', 12345],
['123.4', 12340],
['123', 12300],
['1,234.56', 123456],
];
}

/**
* @dataProvider provideFromHumanReadableString
* @param string $string
* @param int $pence
*/
public function testFromHumanReadableString(string $string, int $pence)
{
$price = AmountMoney::fromHumanReadableString($string);
self::assertInstanceOf(AmountMoney::class, $price);
self::assertSame($pence, $price->getValue());
}

}
4 changes: 3 additions & 1 deletion tests/AmountPenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function provideFromHumanReadableString(): array
*/
public function testFromHumanReadableString(string $string, int $pence)
{
self::assertSame($pence, AmountPence::fromHumanReadableString($string)->getValue());
$price = AmountPence::fromHumanReadableString($string);
self::assertInstanceOf(AmountPence::class, $price);
self::assertSame($pence, $price->getValue());
}
}

0 comments on commit 3cf9b55

Please sign in to comment.