Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add user edit form #20

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Portable\FilaCms\Filament\Resources;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Tables;
use Filament\Tables\Table;
Expand All @@ -24,7 +25,19 @@ public static function form(Form $form): Form
{
return $form
->schema([
//
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('email')
->email()
->prefixIcon('heroicon-m-envelope')
->required(),
Forms\Components\TextInput::make('password')
->password()
->revealable(),
Forms\Components\Select::make('roles')
->relationship('roles', 'name')
->multiple()
->preload(),
]);
}

Expand Down
86 changes: 84 additions & 2 deletions tests/Filament/UserResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace Portable\FilaCms\Tests\Filament;

use Portable\FilaCms\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Livewire\Livewire;
use Portable\FilaCms\Filament\Resources\UserResource as TargetResource;
use Portable\FilaCms\Filament\Resources\UserResource;
use Portable\FilaCms\Tests\TestCase;
use Portable\FilaCms\Tests\User as TargetModel;
use Spatie\Permission\Models\Role;
use Livewire\Livewire;

class UserResourceTest extends TestCase
{
use RefreshDatabase;
use WithFaker;

protected function setUp(): void
{
Expand Down Expand Up @@ -46,4 +50,82 @@ public function test_can_list_users(): void

Livewire::test(UserResource\Pages\ListUsers::class)->assertCanSeeTableRecords($users);
}

public function test_can_create_record(): void
{
Livewire::test(TargetResource\Pages\CreateUser::class)
->fillForm($this->generateModel(true))
->call('create')
->assertHasNoFormErrors();

Livewire::test(TargetResource\Pages\CreateUser::class)
->fillForm([])
->call('create')
->assertHasFormErrors([
'name' => 'required',
'email' => 'required',
]);
}

public function test_can_render_edit_page(): void
{
$data = $this->generateModel();

$this->get(TargetResource::getUrl('edit', ['record' => $data]))->assertSuccessful();
}

public function test_can_retrieve_edit_data(): void
{
$data = $this->generateModel();

Livewire::test(
TargetResource\Pages\EditUser::class,
['record' => $data->getRouteKey()]
)
->assertFormSet([
'name' => $data->name,
]);
}

public function test_can_save_form(): void
{
$data = $this->generateModel();

$new = TargetModel::make($this->generateModel(true));

Livewire::test(TargetResource\Pages\EditUser::class, [
'record' => $data->getRoutekey(),
])
->fillForm([
'name' => $new->name,
'email' => $new->email,
'password' => $new->password,
'roles' => $new->roles,
])
->call('save')
->assertHasNoFormErrors();
$updatedTime = now();

$data->refresh();
$this->assertEquals($data->name, $new->name);
$this->assertEquals($data->updated_at->format('Y-m-d H:i'), $updatedTime->format('Y-m-d H:i'));
}

public function generateModel($raw = false): TargetModel|array
{
$adminRole = Role::where('name', 'Admin')->first();

$data = [
'name' => $this->faker->name,
'email' => $this->faker->email,
'password' => $this->faker->password,
'roles' => [$adminRole->id],
];

if ($raw) {
return $data;
}

return TargetModel::create($data);
}
}
Loading