diff --git a/src/AmountMoney.php b/src/AmountMoney.php index ba63861..f9e44a2 100644 --- a/src/AmountMoney.php +++ b/src/AmountMoney.php @@ -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); } /** @@ -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()); } /** @@ -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()); } /** diff --git a/src/AmountPence.php b/src/AmountPence.php index 5e27d34..e4212ee 100644 --- a/src/AmountPence.php +++ b/src/AmountPence.php @@ -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()); } /** @@ -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()); } /** diff --git a/tests/AmountMoneyTest.php b/tests/AmountMoneyTest.php new file mode 100644 index 0000000..28f1374 --- /dev/null +++ b/tests/AmountMoneyTest.php @@ -0,0 +1,40 @@ + + */ +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()); + } + +} \ No newline at end of file diff --git a/tests/AmountPenceTest.php b/tests/AmountPenceTest.php index be7b030..2175ad0 100644 --- a/tests/AmountPenceTest.php +++ b/tests/AmountPenceTest.php @@ -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()); } }