diff --git a/src/Infolists/Components/MoneyEntry.php b/src/Infolists/Components/MoneyEntry.php index 928e400..73f8e12 100644 --- a/src/Infolists/Components/MoneyEntry.php +++ b/src/Infolists/Components/MoneyEntry.php @@ -18,14 +18,24 @@ protected function setUp(): void $this->numeric(); $this->formatStateUsing(function (MoneyEntry $component, $state): string { - return MoneyFormatter::format($state, $component->getCurrency(), $component->getLocale()); + return MoneyFormatter::format( + $state, + $component->getCurrency(), + $component->getLocale(), + decimals: $this->getDecimals() + ); }); } - public function short() + public function short(): static { $this->formatStateUsing(function (MoneyEntry $component, $state) { - return MoneyFormatter::formatShort($state, $component->getCurrency(), $component->getLocale()); + return MoneyFormatter::formatShort( + $state, + $component->getCurrency(), + $component->getLocale(), + decimals: $this->getDecimals() + ); }); return $this; diff --git a/src/Tables/Columns/MoneyColumn.php b/src/Tables/Columns/MoneyColumn.php index b2f1f99..500d18c 100644 --- a/src/Tables/Columns/MoneyColumn.php +++ b/src/Tables/Columns/MoneyColumn.php @@ -18,14 +18,24 @@ protected function setUp(): void $this->numeric(); $this->formatStateUsing(function (MoneyColumn $component, $state): string { - return MoneyFormatter::format($state, $component->getCurrency(), $component->getLocale()); + return MoneyFormatter::format( + $state, + $component->getCurrency(), + $component->getLocale(), + decimals: $this->getDecimals() + ); }); } public function short() { $this->formatStateUsing(function (MoneyColumn $component, $state) { - return MoneyFormatter::formatShort($state, $component->getCurrency(), $component->getLocale()); + return MoneyFormatter::formatShort( + $state, + $component->getCurrency(), + $component->getLocale(), + decimals: $this->getDecimals() + ); }); return $this; diff --git a/tests/FormInputTest.php b/tests/FormInputTest.php index c1f4750..6be5a55 100644 --- a/tests/FormInputTest.php +++ b/tests/FormInputTest.php @@ -18,9 +18,8 @@ public function testFormInputMoneyFormat(): void { $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 123456]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 123456]); $this->assertEquals('123456', $component->getState()['price']); } @@ -29,9 +28,8 @@ public function testNullState(): void { $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => null]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => null]); $this->assertNull($component->getState()['price']); } @@ -42,9 +40,8 @@ public function testNonNumericState(): void $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 'non_numeric']); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 'non_numeric']); $component->getState(); } @@ -55,9 +52,8 @@ public function testCurrencySymbolPlacementAfterInGlobalConfig() $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 20]); /** @var MoneyInput $field */ $field = $component->getComponent('data.price'); @@ -71,9 +67,8 @@ public function testCurrencySymbolPlacementBeforeInGlobalConfig() $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 20]); $field = $component->getComponent('data.price'); $this->assertEquals('$', $field->getPrefixLabel()); @@ -86,9 +81,8 @@ public function testCurrencySymbolPlacementHiddenInGlobalConfig() $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 20]); $field = $component->getComponent('data.price'); $this->assertNull($field->getPrefixLabel()); @@ -99,9 +93,8 @@ public function testCurrencySymbolPlacementAfterOnField() { $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price')->symbolPlacement('after'), - ])->fill(['price' => 20]); + ->components([ MoneyInput::make('price')->symbolPlacement('after')]) + ->fill(['price' => 20]); $field = $component->getComponent('data.price'); //dd($field); @@ -113,9 +106,8 @@ public function testCurrencySymbolPlacementBeforeOnField() { $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price')->symbolPlacement('before'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')->symbolPlacement('before')]) + ->fill(['price' => 20]); $field = $component->getComponent('data.price'); $this->assertEquals('$', $field->getPrefixLabel()); @@ -126,24 +118,32 @@ public function testCurrencySymbolPlacementHiddenOnField() { $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price')->symbolPlacement('hidden'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')->symbolPlacement('hidden')]) + ->fill(['price' => 20]); $field = $component->getComponent('data.price'); $this->assertNull($field->getPrefixLabel()); $this->assertNull($field->getSuffixLabel()); } + public function testCurrencySymbolPlacementInvalidOnField() + { + $this->expectException(\InvalidArgumentException::class); + + ComponentContainer::make(FormTestComponent::make()) + ->statePath('data') + ->components([MoneyInput::make('price')->symbolPlacement('invalid')]) + ->fill(['price' => 20]); + } + public function testInputMask() { config(['filament-money-field.use_input_mask' => true]); $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - MoneyInput::make('price'), - ])->fill(['price' => 20]); + ->components([MoneyInput::make('price')]) + ->fill(['price' => 20]); $this->assertStringContainsString('money($input', $component->getComponent('data.price')->getMask()->toHtml()); } @@ -153,9 +153,8 @@ public function validationTester(Field $field, $value, ?callable $assertsCallbac try { ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => $value]) + ->components([$field]) + ->fill([$field->getName() => $value]) ->validate(); } catch (ValidationException $exception) { if ($assertsCallback) { @@ -233,9 +232,8 @@ public function testAllowLabelToBeOverrided(): void $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => 45345]); + ->components([$field]) + ->fill([$field->getName() => 45345]); $field = $component->getComponent('data.price'); $this->assertEquals('Custom Label', $field->getLabel()); @@ -249,9 +247,8 @@ public function testResolveLabelClosures(): void $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => 45345]); + ->components([$field]) + ->fill([$field->getName() => 45345]); $field = $component->getComponent('data.price'); $this->assertEquals('Custom Label in Closure', $field->getLabel()); @@ -262,25 +259,22 @@ public function testSetDecimalsOnField(): void $field = (new MoneyInput('price'))->decimals(1); $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => 2345345]); + ->components([$field]) + ->fill([$field->getName() => 2345345]); $this->assertEquals('2345345', $component->getState()['price']); $field = (new MoneyInput('price'))->decimals(3); $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => 2345345]); + ->components([$field]) + ->fill([$field->getName() => 2345345]); $this->assertEquals('2345345', $component->getState()['price']); $field = (new MoneyInput('price'))->decimals(-2); $component = ComponentContainer::make(FormTestComponent::make()) ->statePath('data') - ->components([ - $field, - ])->fill([$field->getName() => 2345345]); + ->components([$field]) + ->fill([$field->getName() => 2345345]); $this->assertEquals('2345345', $component->getState()['price']); } } diff --git a/tests/MoneyEntryTest.php b/tests/MoneyEntryTest.php index fd430f6..822a2d7 100644 --- a/tests/MoneyEntryTest.php +++ b/tests/MoneyEntryTest.php @@ -13,9 +13,8 @@ public function testInfoListMoneyFormat(): void $entry = MoneyEntry::make('price'); $component = ComponentContainer::make(InfolistTestComponent::make()) - ->components([ - $entry, - ])->state([$entry->getName() => 100000000]); + ->components([$entry]) + ->state([$entry->getName() => 100000000]); $entry = $component->getComponent('price'); @@ -27,9 +26,8 @@ public function testInfoListMoneyFormatSek(): void $entry = MoneyEntry::make('price')->currency('SEK')->locale('sv_SE'); $component = ComponentContainer::make(InfolistTestComponent::make()) - ->components([ - $entry, - ])->state([$entry->getName() => 1000000]); + ->components([$entry]) + ->state([$entry->getName() => 1000000]); $entry = $component->getComponent('price'); @@ -44,9 +42,8 @@ public function testInfoListMoneyFormatShort(): void $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'); @@ -61,9 +58,8 @@ public function testInfoListMoneyFormatShortSek(): void $entry = MoneyEntry::make('price')->short()->currency('SEK')->locale('sv_SE'); $component = ComponentContainer::make(InfolistTestComponent::make()) - ->components([ - $entry, - ])->state([$entry->getName() => 123456]); + ->components([$entry]) + ->state([$entry->getName() => 123456]); $entry = $component->getComponent('price'); @@ -72,4 +68,20 @@ public function testInfoListMoneyFormatShortSek(): void $entry->formatState($entry->getState()) ); } + + public function testInfoListMoneyFormatSekNoDecimals(): void + { + $entry = MoneyEntry::make('price')->currency('SEK')->locale('sv_SE')->decimals(0); + + $component = ComponentContainer::make(InfolistTestComponent::make()) + ->components([$entry]) + ->state([$entry->getName() => 1000000]); + + $entry = $component->getComponent('price'); + + $this->assertEquals( + static::replaceNonBreakingSpaces('10 000 kr'), + $entry->formatState($entry->getState()) + ); + } }