From 4240ed5d9a73752158de140cb5b21a99c97d1386 Mon Sep 17 00:00:00 2001 From: Peter Elmered <peter@elmered.com> Date: Tue, 23 Apr 2024 00:40:23 +0200 Subject: [PATCH] Add tests for parsing issue in isssue #20 --- src/MoneyFormatter.php | 2 +- tests/MoneyFormatterTest.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index f76d3c1..f83e646 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -39,7 +39,7 @@ public static function parseDecimal($moneyString, Currency $currency, string $lo $numberFormatter = self::getNumberFormatter($locale, NumberFormatter::DECIMAL); $moneyParser = new IntlLocalizedDecimalParser($numberFormatter, $currencies); - // Needed to fix some parsing issues with small numbers such as "2,00" with "," as thousands separator + // Needed to fix some parsing issues with small numbers such as "2,00" with "," left as thousands separator in the wrong place // See: https://github.com/pelmered/filament-money-field/issues/20 $formattingRules = self::getFormattingRules($locale); $moneyString = str_replace($formattingRules->groupingSeparator, '', $moneyString); diff --git a/tests/MoneyFormatterTest.php b/tests/MoneyFormatterTest.php index 5e01124..2dded83 100644 --- a/tests/MoneyFormatterTest.php +++ b/tests/MoneyFormatterTest.php @@ -164,7 +164,7 @@ public static function provideDecimalDataUSD(): array ], ]; } - + #[DataProvider('provideMoneyDataUSD')] public function testMoneyFormatterUSD(mixed $input, string $expectedOutput) { @@ -202,7 +202,7 @@ public function testMoneyDecimalFormatterSEK(mixed $input, string $expectedOutpu MoneyFormatter::formatAsDecimal($input, new Currency('SEK'), 'sv_SE') ); } - + #[DataProvider('provideDecimalDataSEK')] //#[CoversClass(MoneyFormatter::class)] public function testMoneyParserDecimalSEK(mixed $input, string $expectedOutput) @@ -221,4 +221,14 @@ public function testMoneyParserDecimalUSD(mixed $input, string $expectedOutput) MoneyFormatter::parseDecimal($input, new Currency('USD'), 'en_US') ); } + + public static function testMoneyParserDecimal(): void + { + // Tests for some parsing issues with small numbers such as "2,00" with "," left as thousands separator in the wrong place + // See: https://github.com/pelmered/filament-money-field/issues/20 + self::assertSame( + '20000', + MoneyFormatter::parseDecimal('2,00', new Currency('USD'), 'en_US') + ); + } }