From 45b80ba48bd9b3f0b6418efaec0489a5ccdebb01 Mon Sep 17 00:00:00 2001 From: Nick Breland Date: Wed, 14 Feb 2024 10:58:16 +0000 Subject: [PATCH] test: formatAsDecimal --- tests/MoneyFormatterTest.php | 106 +++++++++++++++++++++++++++-------- 1 file changed, 82 insertions(+), 24 deletions(-) diff --git a/tests/MoneyFormatterTest.php b/tests/MoneyFormatterTest.php index 87b5488..5e01124 100644 --- a/tests/MoneyFormatterTest.php +++ b/tests/MoneyFormatterTest.php @@ -3,8 +3,10 @@ namespace Pelmered\FilamentMoneyField\Tests; use Money\Currency; use Pelmered\FilamentMoneyField\MoneyFormatter; -use PHPUnit\Framework; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +#[CoversClass(MoneyFormatter::class)] final class MoneyFormatterTest extends TestCase { public static function provideMoneyDataSEK(): array @@ -33,6 +35,32 @@ public static function provideMoneyDataSEK(): array ]; } + public static function provideDecimalMoneyDataSEK(): array + { + return [ + 'thousands' => [ + 1000000, + '10 000,00', + ], + 'decimals' => [ + 10045, + '100,45', + ], + 'millions' => [ + 123456789, + '1 234 567,89', + ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', + ], + ]; + } + public static function provideMoneyDataUSD(): array { return [ @@ -59,6 +87,32 @@ public static function provideMoneyDataUSD(): array ]; } + public static function provideDecimalMoneyDataUSD(): array + { + return [ + 'thousands' => [ + 1000000, + '10,000.00', + ], + 'decimals' => [ + 10045, + '100.45', + ], + 'millions' => [ + 123456789, + '1,234,567.89', + ], + 'empty_string' => [ + '', + '', + ], + 'null' => [ + null, + '', + ], + ]; + } + public static function provideDecimalDataSEK(): array { return [ @@ -110,13 +164,17 @@ public static function provideDecimalDataUSD(): array ], ]; } + + #[DataProvider('provideMoneyDataUSD')] + public function testMoneyFormatterUSD(mixed $input, string $expectedOutput) + { + self::assertSame( + static::replaceNonBreakingSpaces($expectedOutput), + MoneyFormatter::format($input, new Currency('USD'), 'en_US') + ); + } - - /** - * @covers MoneyFormatter::format - * @dataProvider provideMoneyDataSEK - */ - #[Framework\CoversClass(MoneyFormatter::class)] + #[DataProvider('provideMoneyDataSEK')] public function testMoneyFormatterSEK(mixed $input, string $expectedOutput) { self::assertSame( @@ -125,24 +183,28 @@ public function testMoneyFormatterSEK(mixed $input, string $expectedOutput) ); } - /** - * @covers MoneyFormatter::format - * @dataProvider provideMoneyDataUSD - */ - #[Framework\CoversClass(MoneyFormatter::class)] - public function testMoneyFormatterUSD(mixed $input, string $expectedOutput) + #[DataProvider('provideDecimalMoneyDataUSD')] + //#[CoversClass(MoneyFormatter::class)] + public function testMoneyDecimalFormatterUSD(mixed $input, string $expectedOutput) { self::assertSame( static::replaceNonBreakingSpaces($expectedOutput), - MoneyFormatter::format($input, new Currency('USD'), 'en_US') + MoneyFormatter::formatAsDecimal($input, new Currency('USD'), 'en_US') ); } - /** - * @covers MoneyFormatter::parseDecimal - * @dataProvider provideDecimalDataSEK - */ - #[Framework\CoversClass(MoneyFormatter::class)] + #[DataProvider('provideDecimalMoneyDataSEK')] + //#[CoversClass(MoneyFormatter::class)] + public function testMoneyDecimalFormatterSEK(mixed $input, string $expectedOutput) + { + self::assertSame( + static::replaceNonBreakingSpaces($expectedOutput), + MoneyFormatter::formatAsDecimal($input, new Currency('SEK'), 'sv_SE') + ); + } + + #[DataProvider('provideDecimalDataSEK')] + //#[CoversClass(MoneyFormatter::class)] public function testMoneyParserDecimalSEK(mixed $input, string $expectedOutput) { self::assertSame( @@ -151,11 +213,7 @@ public function testMoneyParserDecimalSEK(mixed $input, string $expectedOutput) ); } - /** - * @covers MoneyFormatter::parseDecimal - * @dataProvider provideDecimalDataUSD - */ - #[Framework\CoversClass(MoneyFormatter::class)] + #[DataProvider('provideDecimalDataUSD')] public function testMoneyParserDecimalUSD(mixed $input, string $expectedOutput) { self::assertSame(