Skip to content

Commit

Permalink
Improve tests and simplify the API for not showing currency symbol in…
Browse files Browse the repository at this point in the history
… short format

# Conflicts:
#	README.md
#	src/Infolists/Components/MoneyEntry.php
#	src/Tables/Columns/MoneyColumn.php
#	tests/MoneyColumnTest.php
  • Loading branch information
pelmered committed Dec 5, 2024
1 parent f9a653d commit f49e4fb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 3 additions & 12 deletions src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class MoneyEntry extends TextEntry
{
use HasMoneyAttributes;

protected bool $showCurrencySymbol = true;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -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;
}
}
15 changes: 3 additions & 12 deletions src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class MoneyColumn extends TextColumn
{
use HasMoneyAttributes;

protected bool $showCurrencySymbol = true;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/MoneyColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
21 changes: 17 additions & 4 deletions tests/MoneyEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand Down

0 comments on commit f49e4fb

Please sign in to comment.