-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from pelmered/feature/validation-tests
Add and improve tests
- Loading branch information
Showing
19 changed files
with
1,322 additions
and
926 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace Pelmered\FilamentMoneyField\Exceptions; | ||
|
||
class UnsupportedCurrency extends \RuntimeException | ||
{ | ||
public function __construct(string $currencyCode) | ||
{ | ||
parent::__construct('Currency not supported: ' . $currencyCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace Pelmered\FilamentMoneyField\Forms\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Money\Exception\ParserException; | ||
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; | ||
use Pelmered\FilamentMoneyField\MoneyFormatter; | ||
|
||
class MaxValueRule implements ValidationRule | ||
{ | ||
public function __construct(private int $max, private MoneyInput $component) | ||
{} | ||
|
||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$currencyCode = $this->component->getCurrency(); | ||
$locale = $this->component->getLocale(); | ||
|
||
try { | ||
$minorValue = MoneyFormatter::parseDecimal( | ||
$value, | ||
$currencyCode, | ||
$locale | ||
); | ||
|
||
if ($minorValue >= $this->max) { | ||
$fail( | ||
strtr( | ||
'The {attribute} must be less than or equal to {value}.', | ||
[ | ||
'{attribute}' => ucwords($this->component->getLabel()), | ||
'{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale), | ||
] | ||
) | ||
); | ||
} | ||
} catch (ParserException $parserException) { | ||
$fail( | ||
strtr( | ||
'The {attribute} must be a valid numeric value.', | ||
[ | ||
'{attribute}' => ucwords($this->component->getLabel()), | ||
] | ||
) | ||
); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace Pelmered\FilamentMoneyField\Forms\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Money\Exception\ParserException; | ||
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; | ||
use Pelmered\FilamentMoneyField\MoneyFormatter; | ||
|
||
readonly class MinValueRule implements ValidationRule | ||
{ | ||
public function __construct(private int $min, private MoneyInput $component) | ||
{} | ||
|
||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$currencyCode = $this->component->getCurrency(); | ||
$locale = $this->component->getLocale(); | ||
|
||
try { | ||
$minorValue = MoneyFormatter::parseDecimal( | ||
$value, | ||
$currencyCode, | ||
$locale | ||
); | ||
|
||
if ($minorValue <= $this->min) { | ||
$fail( | ||
strtr( | ||
'The {attribute} must be at least {value}.', | ||
[ | ||
'{attribute}' => ucwords($this->component->getLabel()), | ||
'{value}' => MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale), | ||
] | ||
) | ||
); | ||
} | ||
} catch (ParserException $parserException) { | ||
$fail( | ||
strtr( | ||
'The {attribute} must be a valid numeric value.', | ||
[ | ||
'{attribute}' => ucwords($this->component->getLabel()), | ||
] | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace Pelmered\FilamentMoneyField\Tests\Components; | ||
|
||
|
||
use Filament\Forms\Concerns\InteractsWithForms; | ||
use Filament\Forms\Contracts\HasForms; | ||
use Livewire\Component; | ||
use Filament\Forms\Form; | ||
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; | ||
|
||
class FormTestComponent extends Component implements HasForms | ||
{ | ||
use InteractsWithForms; | ||
|
||
public $data; | ||
public $form; | ||
|
||
public static function make(): static | ||
{ | ||
return new static(); | ||
} | ||
|
||
|
||
public function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
MoneyInput::make('price') | ||
->minValue(100) | ||
->maxValue(1000) | ||
]); | ||
} | ||
|
||
public function mount(): void | ||
{ | ||
//$this->form->fill(); | ||
} | ||
|
||
public function data($data): static | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
|
||
public function getData() | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
namespace Pelmered\FilamentMoneyField\Tests\Components; | ||
|
||
|
||
use Filament\Forms\Concerns\InteractsWithForms; | ||
use Filament\Forms\Contracts\HasForms; | ||
use Filament\Infolists\Concerns\InteractsWithInfolists; | ||
use Filament\Infolists\Contracts\HasInfolists; | ||
use Filament\Infolists\Infolist; | ||
use Livewire\Component; | ||
use Filament\Forms\Form; | ||
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput; | ||
use Pelmered\FilamentMoneyField\Infolists\Components\MoneyEntry; | ||
|
||
class InfolistTestComponent extends Component implements HasForms, HasInfolists | ||
{ | ||
use InteractsWithForms; | ||
use InteractsWithInfolists; | ||
|
||
public static function make(): static | ||
{ | ||
return new static(); | ||
} | ||
|
||
public function infolist(Infolist $infolist): Infolist | ||
{ | ||
return $infolist | ||
->state([]) | ||
->schema([ | ||
MoneyEntry::make('amount') | ||
->currency('SEK') | ||
->locale('sv_SE') | ||
->label('Amount'), | ||
]); | ||
} | ||
|
||
/* | ||
public function render(): View | ||
{ | ||
return view('infolists.fixtures.actions'); | ||
} | ||
*/ | ||
} |
Oops, something went wrong.