Testing a Form in FilamentPHP doesn't work

182 Views Asked by At

I have an Order Form which I would like to test. So I have an OrderResource and the url to the form is: admin/orders/create I believe that this corresponds to: 'create' => Pages\CreateOrder::route('/create'),

So I wrote a test for this like so:

livewire(CreateOrder::class)
    ->fillForm([
        'brand_id' => $brand->id,
    ])->assertFormSet(['supplier_id' => $suppliers->pluck('id')->toArray()]);


But when I run this I get:

Attempt to read property "form" on null.

      at vendor/filament/forms/src/Testing/TestsForms.php:125
        121▕     public function assertFormExists(): Closure
        122▕     {
        123▕         return function (string $name = 'form'): static {
        124▕             /** @var ComponentContainer $form */
      ➜ 125▕             $form = $this->instance()->{$name};
        126▕
        127▕             $livewireClass = $this->instance()::class;
        128▕
        129▕             Assert::assertInstanceOf(

MyCreateOrder class doesn't have a form property. I'm wondering where the $form property is set that renders the form when go to the aforementioned url

I have built this test based on the instructions over here. And I am running on filament 3.2.7 in PHP8.2 and Laravel 10.10

How can I solve this?

1

There are 1 best solutions below

0
On

This error typically indicates that the Livewire component CreateOrder can't find the form. A possibility if because the test environment cannot have an authenticated user or the user haven't permission.

Ensure User Authentication:

Before attempting to access the form in your test, create and authenticate a user:

    $user = User::factory()->create();
    $this->actingAs($user);

This step ensures your test environment use an authenticated session.

Manage Permissions with Roles (If Necessary):

If your application uses a permissions package, like Spatie's Laravel-permission, ensure the test user has the necessary roles or permissions to access the form:

    $user = User::factory()->create();

    // Assuming 'super_admin' role has the required permissions
    $user->assignRole('super_admin');
    $this->actingAs($user);