From f49e4fbb52d1d8e1d53305564edfbd8e175d247a Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Thu, 5 Dec 2024 19:08:00 +0100 Subject: [PATCH] Improve tests and simplify the API for not showing currency symbol in short format # Conflicts: # README.md # src/Infolists/Components/MoneyEntry.php # src/Tables/Columns/MoneyColumn.php # tests/MoneyColumnTest.php --- README.md | 3 +-- src/Infolists/Components/MoneyEntry.php | 15 +++------------ src/Tables/Columns/MoneyColumn.php | 15 +++------------ tests/MoneyColumnTest.php | 2 +- tests/MoneyEntryTest.php | 21 +++++++++++++++++---- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b5e890c..c8baaea 100644 --- a/README.md +++ b/README.md @@ -140,8 +140,7 @@ MoneyColumn::make('price') ->short(), // Short format, e.g. $1.23M instead of $1,234,567.89 MoneyColumn::make('price') - ->short() - ->hideCurrencySymbol(), // Short format without currency symbol, e.g. 1.23M instead of $1.23M + ->short(showCurrencySymbol: false), // Short format without currency symbol, e.g. 1.23M instead of $1.23M MoneyColumn::make('price') ->decimals(4) diff --git a/src/Infolists/Components/MoneyEntry.php b/src/Infolists/Components/MoneyEntry.php index d28e90d..a298a99 100644 --- a/src/Infolists/Components/MoneyEntry.php +++ b/src/Infolists/Components/MoneyEntry.php @@ -10,8 +10,6 @@ class MoneyEntry extends TextEntry { use HasMoneyAttributes; - protected bool $showCurrencySymbol = true; - protected function setUp(): void { parent::setUp(); @@ -29,25 +27,18 @@ protected function setUp(): void }); } - public function short(): static + public function short($showCurrencySymbol = true): static { - $this->formatStateUsing(function (MoneyEntry $component, null|int|string $state) { + $this->formatStateUsing(function (MoneyEntry $component, null|int|string $state) use ($showCurrencySymbol) { return MoneyFormatter::formatShort( $state, $component->getCurrency(), $component->getLocale(), decimals: $this->getDecimals(), - showCurrencySymbol: $component->showCurrencySymbol, + showCurrencySymbol: $showCurrencySymbol, ); }); return $this; } - - public function hideCurrencySymbol(bool $hideCurrencySymbol = true): static - { - $this->showCurrencySymbol = ! $hideCurrencySymbol; - - return $this; - } } diff --git a/src/Tables/Columns/MoneyColumn.php b/src/Tables/Columns/MoneyColumn.php index b3555e4..1e05560 100644 --- a/src/Tables/Columns/MoneyColumn.php +++ b/src/Tables/Columns/MoneyColumn.php @@ -10,8 +10,6 @@ class MoneyColumn extends TextColumn { use HasMoneyAttributes; - protected bool $showCurrencySymbol = true; - protected function setUp(): void { parent::setUp(); @@ -29,25 +27,18 @@ protected function setUp(): void }); } - public function short(): static + public function short($showCurrencySymbol = true): static { - $this->formatStateUsing(function (MoneyColumn $component, null|int|string $state) { + $this->formatStateUsing(function (MoneyColumn $component, null|int|string $state) use ($showCurrencySymbol) { return MoneyFormatter::formatShort( $state, $component->getCurrency(), $component->getLocale(), decimals: $this->getDecimals(), - showCurrencySymbol: $component->showCurrencySymbol, + showCurrencySymbol: $showCurrencySymbol, ); }); return $this; } - - public function hideCurrencySymbol(bool $hideCurrencySymbol = true): static - { - $this->showCurrencySymbol = ! $hideCurrencySymbol; - - return $this; - } } diff --git a/tests/MoneyColumnTest.php b/tests/MoneyColumnTest.php index f5b3a3b..8d0bd0d 100644 --- a/tests/MoneyColumnTest.php +++ b/tests/MoneyColumnTest.php @@ -41,7 +41,7 @@ }); it('formats money column state to short format with sek and hide currency symbol', function () { - $column = MoneyColumn::make('price')->currency('SEK')->locale('sv_SE')->short()->hideCurrencySymbol(); + $column = MoneyColumn::make('price')->currency('SEK')->locale('sv_SE')->short(showCurrencySymbol: false); expect($column->formatState(651))->toEqual(replaceNonBreakingSpaces('6,51')); expect($column->formatState(235235))->toEqual(replaceNonBreakingSpaces('2,35K')); expect($column->formatState(23523562))->toEqual(replaceNonBreakingSpaces('235,24K')); diff --git a/tests/MoneyEntryTest.php b/tests/MoneyEntryTest.php index a482d30..3e7064d 100644 --- a/tests/MoneyEntryTest.php +++ b/tests/MoneyEntryTest.php @@ -33,14 +33,26 @@ $entry = MoneyEntry::make('price')->short(); $component = ComponentContainer::make(InfolistTestComponent::make()) - ->components([$entry]) - ->state([$entry->getName() => 123456789]); + ->components([$entry]) + ->state([$entry->getName() => 123456789]); $entry = $component->getComponent('price'); expect($entry->formatState($entry->getState()))->toEqual(replaceNonBreakingSpaces('$1.23M')); }); +it('formats infolist money in short format with decimals in USD', function () { + $entry = MoneyEntry::make('price')->short()->decimals(4); + + $component = ComponentContainer::make(InfolistTestComponent::make()) + ->components([$entry]) + ->state([$entry->getName() => 123456789]); + + $entry = $component->getComponent('price'); + + expect($entry->formatState($entry->getState()))->toEqual(replaceNonBreakingSpaces('$1.2346M')); +}); + it('formats infolist money in short format in sek', function () { $entry = MoneyEntry::make('price')->short()->currency('SEK')->locale('sv_SE'); @@ -53,12 +65,13 @@ expect($entry->formatState($entry->getState()))->toEqual(replaceNonBreakingSpaces('1,23K kr')); }); + it('formats infolist money in sek with no decimals', function () { $entry = MoneyEntry::make('price')->currency('SEK')->locale('sv_SE')->decimals(0); $component = ComponentContainer::make(InfolistTestComponent::make()) - ->components([$entry]) - ->state([$entry->getName() => 1000000]); + ->components([$entry]) + ->state([$entry->getName() => 1000000]); $entry = $component->getComponent('price');