Skip to content

Commit

Permalink
Add component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed May 8, 2024
1 parent 3d52c98 commit 546b636
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 32 deletions.
10 changes: 9 additions & 1 deletion src/Forms/Rules/MaxValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
);

Check warning on line 23 in src/Forms/Rules/MaxValueRule.php

View check run for this annotation

Codecov / codecov/patch

src/Forms/Rules/MaxValueRule.php#L19-L23

Added lines #L19 - L23 were not covered by tests

if ($minorValue >= $this->max) {
$fail('The ' . $this->component->getLabel() . ' must be less than or equal to ' . MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale) . '.');
$fail(
strtr(
'The {attribute} must be less than or equal to {value}.',
[
'{attribute}' => ucwords($this->component->getLabel()),
'{value}' => MoneyFormatter::formatAsDecimal($this->max, $currencyCode, $locale),
]
)
);

Check warning on line 34 in src/Forms/Rules/MaxValueRule.php

View check run for this annotation

Codecov / codecov/patch

src/Forms/Rules/MaxValueRule.php#L25-L34

Added lines #L25 - L34 were not covered by tests
}
}
}
11 changes: 10 additions & 1 deletion src/Forms/Rules/MinValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
$locale
);

Check warning on line 23 in src/Forms/Rules/MinValueRule.php

View check run for this annotation

Codecov / codecov/patch

src/Forms/Rules/MinValueRule.php#L19-L23

Added lines #L19 - L23 were not covered by tests

//dump($currencyCode, $locale, $minorValue, $this->min);
if ($minorValue <= $this->min) {
$fail('The ' . $this->component->getLabel() . ' must be less than or equal to ' . MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale) . '.');
$fail(
strtr(
'The {attribute} must be at least {value}.',
[
'{attribute}' => ucwords($this->component->getLabel()),
'{value}' => MoneyFormatter::formatAsDecimal($this->min, $currencyCode, $locale),
]
)
);

Check warning on line 35 in src/Forms/Rules/MinValueRule.php

View check run for this annotation

Codecov / codecov/patch

src/Forms/Rules/MinValueRule.php#L26-L35

Added lines #L26 - L35 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Filament\Forms\Form;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;

class LivewireComponent extends Component implements HasForms
class FormTestComponent extends Component implements HasForms
{
use InteractsWithForms;

Expand Down
43 changes: 43 additions & 0 deletions tests/Components/InfolistTestComponent.php
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');
}
*/
}
24 changes: 24 additions & 0 deletions tests/Database/Factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Pelmered\FilamentMoneyField\Tests\Database\Factories;

use Pelmered\FilamentMoneyField\Tests\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
protected $model = Post::class;

public function definition(): array
{
return [
'author_id' => random_int(1, 10),
'content' => $this->faker->paragraph(),
'is_published' => $this->faker->boolean(),
'tags' => $this->faker->words(),
'title' => $this->faker->sentence(),
'rating' => $this->faker->numberBetween(1, 10),
'price' => $this->faker->numberBetween(100, 10000),
];
}
}

27 changes: 8 additions & 19 deletions tests/FormInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule;
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule2;
use Pelmered\FilamentMoneyField\Forms\Rules\MinValueRule;
use Pelmered\FilamentMoneyField\Tests\Components\LivewireComponent;
use Pelmered\FilamentMoneyField\Tests\Components\FormTestComponent;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use Livewire\Livewire;
Expand All @@ -18,38 +18,27 @@
#[CoversClass(MoneyInput::class)]
class FormInputTest extends TestCase
{

/*
public function testFormInputMoneyFormat(): void
{
$input = MoneyInput::make('price');
//dd($input);
//$this->assertEquals('$25.00', $input->formatState(2500));
$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price'),
])->fill(['price' => 20]);

$this->assertEquals('$25.00', $input->formatState(2500));
$this->assertEquals('20', $component->getState()['price']);
}
*/

public function validationTester(Field $field, $value): true|array
{
$rules = [];
//$fieldName = 'totalAmount';
/*
$field = (new MoneyInput($fieldName))
->required()->minValue(100)->maxValue(10000);
*/

//$field->getName()

try {
ComponentContainer::make(LivewireComponent::make())
ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
$field
])->fill([$field->getName() => $value])
->validate();
} catch (ValidationException $exception) {
dump($exception->validator->failed()[$field->getStatePath()]);
return $exception->validator->failed()[$field->getStatePath()];
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Pelmered\FilamentMoneyField\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Pelmered\FilamentMoneyField\Tests\Database\Factories\PostFactory;

class Post extends Model
{
use HasFactory;
use SoftDeletes;

protected $casts = [
'is_published' => 'boolean',
'tags' => 'array',
];

protected $guarded = [];

/*
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}
*/

protected static function newFactory()
{
return PostFactory::new();
}
}
35 changes: 25 additions & 10 deletions tests/MoneyEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@

namespace Pelmered\FilamentMoneyField\Tests;

use Filament\Infolists\ComponentContainer;
use JetBrains\PhpStorm\NoReturn;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;
use Pelmered\FilamentMoneyField\Infolists\Components\MoneyEntry;
use Pelmered\FilamentMoneyField\Tests\Components\FormTestComponent;
use Pelmered\FilamentMoneyField\Tests\Components\InfolistTestComponent;
use Pelmered\FilamentMoneyField\Tests\Models\Post;

class MoneyEntryTest extends TestCase
{
public function testInfoListMoneyFormat(): void
{
$this->markTestSkipped('Not working yet');
$entry = MoneyEntry::make('price');

$moneyEntry = (new MoneyEntry('name'))->currency('SEK')->locale('sv_SE');
$component = ComponentContainer::make(InfolistTestComponent::make())
->components([
$entry,
])->state([$entry->getName() => 1000000]);

self::callMethod($moneyEntry, 'setUp', []);
$entry = $component->getComponent('price');

$state = 1000000;
$this->assertEquals('$10,000.00', $entry->formatState($entry->getState()));
}
public function testInfoListMoneyFormatSEK(): void
{
$entry = MoneyEntry::make('price')->currency('SEK')->locale('sv_SE');

$value = $moneyEntry->evaluate(self::getProperty($moneyEntry, 'formatStateUsing'), [
'state' => $state,
]);
$component = ComponentContainer::make(InfolistTestComponent::make())
->components([
$entry,
])->state([$entry->getName() => 1000000]);

$this->assertEquals('10000,00 kr', $value);
$entry = $component->getComponent('price');

$this->assertEquals('10000,00 kr', $moneyEntry->formatState('1000000'));
$this->assertEquals(
static::replaceNonBreakingSpaces('10 000,00 kr'),
$entry->formatState($entry->getState())
);
}

}
29 changes: 29 additions & 0 deletions tests/ValidationRulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Pelmered\FilamentMoneyField\Tests;

use Filament\Forms\Components\Field;
use Mockery\MockInterface;
use Pelmered\FilamentMoneyField\Forms\Components\MoneyInput;
use Filament\Forms\ComponentContainer;
use Illuminate\Support\Str;
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule;
use Pelmered\FilamentMoneyField\Forms\Rules\MaxValueRule2;
use Pelmered\FilamentMoneyField\Forms\Rules\MinValueRule;
use Pelmered\FilamentMoneyField\Tests\Components\FormTestComponent;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\Exception;
use Livewire\Livewire;
use Illuminate\Validation\ValidationException;

#[CoversClass(MoneyInput::class)]
class ValidationRulesTest extends TestCase
{
public function testMinValueRule()
{
$rule = new MinValueRule(10000, new MoneyInput('totalAmount'));

$rule->validate('totalAmount', 16, function ($message) {
$this->assertEquals('The Total Amount must be at least 100.00.', $message);
});
}
}

0 comments on commit 546b636

Please sign in to comment.