Skip to content

Commit

Permalink
Code format + decimal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Jul 24, 2024
1 parent aac004a commit ad879eb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 65 deletions.
16 changes: 13 additions & 3 deletions src/Infolists/Components/MoneyEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions src/Tables/Columns/MoneyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
90 changes: 42 additions & 48 deletions tests/FormInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand All @@ -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']);
}
Expand All @@ -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();
}
Expand All @@ -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');
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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());
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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']);
}
}
36 changes: 24 additions & 12 deletions tests/MoneyEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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

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

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

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

0 comments on commit ad879eb

Please sign in to comment.