From 875a193517f39b5bd8ba2642bea6c8fc21cc0f5f Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 31 Jan 2024 23:49:03 +0100 Subject: [PATCH] Improve number formatting and parsing + tests --- src/MoneyFormatter.php | 11 +++++-- tests/MoneyFormatterTest.php | 56 ++++++++++++++++++++++++++++-------- tests/TestCase.php | 3 ++ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index e8008a5..7baa818 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -13,11 +13,12 @@ class MoneyFormatter { public static function format($value, $currency, $locale, $monetarySeparator = null): string { - $currencies = new ISOCurrencies(); + if (is_null($value) || $value === '') { + return ''; + } $numberFormatter = self::getNumberFormatter($locale, NumberFormatter::CURRENCY); - - $moneyFormatter = new IntlMoneyFormatter($numberFormatter, $currencies); + $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies()); $money = new Money($value, $currency); return $moneyFormatter->format($money); @@ -25,6 +26,10 @@ public static function format($value, $currency, $locale, $monetarySeparator = n public static function parseDecimal($moneyString, $currency, $locale): string { + if (is_null($moneyString) || $moneyString === '') { + return ''; + } + $currencies = new ISOCurrencies(); $numberFormatter = self::getNumberFormatter($locale, NumberFormatter::DECIMAL); $moneyParser = new IntlLocalizedDecimalParser($numberFormatter, $currencies); diff --git a/tests/MoneyFormatterTest.php b/tests/MoneyFormatterTest.php index 9010a11..87b5488 100644 --- a/tests/MoneyFormatterTest.php +++ b/tests/MoneyFormatterTest.php @@ -11,16 +11,24 @@ public static function provideMoneyDataSEK(): array { return [ 'thousands' => [ - '10 000,00 kr', 1000000, + '10 000,00 kr', ], 'decimals' => [ - '100,45 kr', 10045, + '100,45 kr', ], 'millions' => [ - '1 234 567,89 kr', 123456789, + '1 234 567,89 kr', + ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', ], ]; } @@ -29,16 +37,24 @@ public static function provideMoneyDataUSD(): array { return [ 'thousands' => [ - '$10,000.00', 1000000, + '$10,000.00', ], 'decimals' => [ - '$100.45', 10045, + '$100.45', ], 'millions' => [ - '$1,234,567.89', 123456789, + '$1,234,567.89', + ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', ], ]; } @@ -58,6 +74,14 @@ public static function provideDecimalDataSEK(): array '1 234 567,89', '123456789', ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', + ], ]; } @@ -76,6 +100,14 @@ public static function provideDecimalDataUSD(): array '1,234,567.89', '123456789', ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', + ], ]; } @@ -85,7 +117,7 @@ public static function provideDecimalDataUSD(): array * @dataProvider provideMoneyDataSEK */ #[Framework\CoversClass(MoneyFormatter::class)] - public function testMoneyFormatterSEK( string $expectedOutput, mixed $input) + public function testMoneyFormatterSEK(mixed $input, string $expectedOutput) { self::assertSame( static::replaceNonBreakingSpaces($expectedOutput), @@ -98,7 +130,7 @@ public function testMoneyFormatterSEK( string $expectedOutput, mixed $input) * @dataProvider provideMoneyDataUSD */ #[Framework\CoversClass(MoneyFormatter::class)] - public function testMoneyFormatterUSD( string $expectedOutput, mixed $input) + public function testMoneyFormatterUSD(mixed $input, string $expectedOutput) { self::assertSame( static::replaceNonBreakingSpaces($expectedOutput), @@ -107,11 +139,11 @@ public function testMoneyFormatterUSD( string $expectedOutput, mixed $input) } /** - * @covers MoneyFormatter::parse + * @covers MoneyFormatter::parseDecimal * @dataProvider provideDecimalDataSEK */ #[Framework\CoversClass(MoneyFormatter::class)] - public function testMoneyParserDecimalSEK( string $input, mixed $expectedOutput) + public function testMoneyParserDecimalSEK(mixed $input, string $expectedOutput) { self::assertSame( $expectedOutput, @@ -120,11 +152,11 @@ public function testMoneyParserDecimalSEK( string $input, mixed $expectedOutput) } /** - * @covers MoneyFormatter::parse + * @covers MoneyFormatter::parseDecimal * @dataProvider provideDecimalDataUSD */ #[Framework\CoversClass(MoneyFormatter::class)] - public function testMoneyParserDecimalUSD( string $input, mixed $expectedOutput) + public function testMoneyParserDecimalUSD(mixed $input, string $expectedOutput) { self::assertSame( $expectedOutput, diff --git a/tests/TestCase.php b/tests/TestCase.php index c5ffa68..a9a129c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -18,6 +18,9 @@ public static function getProperty($object, $property) return $reflection->getValue($object); } + /** + * Replaces all non-breaking spaces in the given string with the Unicode character for non-breaking space. + */ public static function replaceNonBreakingSpaces(string $string): string { return preg_replace('/\s/', "\xc2\xa0", $string);