Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pixelairport's avatar

How to test filament correctly

I want to test a module, I build for laravel. Im not sure how to do it correctly. The module should be also used in other projects, thats why Im a bit confused. The tests are in the test folder in my module. My main project has 2 filament panels (admin and account). I need to switch in every test to admin for this module. But what is when next project has another name for the panel and want to run the tests? This is what I mean Filament::setCurrentPanel(Filament::getPanel('admin'));.

Second question is, should I use the livewire plugin for form testing in the module? Or is that more for tests in my main application. I mean at this time I want to test, if my validation is correct in Request file. I think about, what is the correct way? Something like this:

Filament::setCurrentPanel(Filament::getPanel('admin'));

livewire(CreateCmsAuthor::class)
        ->fillForm([
            'name' => null,
        ])->call('create')
        ->assertHasFormErrors(['name' => 'required']);

or something like this

Filament::setCurrentPanel(Filament::getPanel('admin'));

// ... Do model factory, whithout create it and only get the values

// ... send it to the route where it is created

I hope somebody can understand my problem. It is not easy to describe and Im at the beginning of testing.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To test your Laravel module effectively, especially when it involves Filament panels, you can follow these guidelines:

  1. Panel Configuration: Since your module is intended to be used across different projects with potentially different panel names, you should make your tests flexible. You can achieve this by using environment variables or configuration files to define the panel name. This way, you can easily switch the panel name without modifying the test code directly.

  2. Testing with Livewire: Using Livewire for form testing is a good approach, especially if your module heavily relies on Livewire components. It allows you to simulate user interactions and validate form submissions effectively.

  3. Validation Testing: For testing validation, you can use both Livewire and traditional HTTP request testing. Livewire is more suited for testing the component's behavior, while HTTP request testing is useful for testing the request validation logic directly.

Here's a structured approach to your testing:

Using Environment Variables for Panel Names

First, define a configuration or environment variable for the panel name:

// config/filament.php
return [
    'default_panel' => env('FILAMENT_PANEL', 'admin'),
];

Then, use this configuration in your tests:

use Filament\Facades\Filament;

public function setUp(): void
{
    parent::setUp();
    Filament::setCurrentPanel(Filament::getPanel(config('filament.default_panel')));
}

Livewire Testing Example

For testing Livewire components, you can use the following approach:

use Livewire\Livewire;
use App\Http\Livewire\CreateCmsAuthor;

public function testCreateCmsAuthorValidation()
{
    Livewire::test(CreateCmsAuthor::class)
        ->set('name', null)
        ->call('create')
        ->assertHasErrors(['name' => 'required']);
}

HTTP Request Testing Example

For testing request validation directly, you can use Laravel's HTTP testing capabilities:

public function testCreateCmsAuthorRequestValidation()
{
    $response = $this->post(route('cms-authors.store'), [
        'name' => null,
    ]);

    $response->assertSessionHasErrors(['name' => 'required']);
}

Conclusion

By using configuration for panel names and leveraging both Livewire and HTTP request testing, you can create flexible and comprehensive tests for your module. This approach ensures that your module can be easily adapted to different projects with varying configurations.

Please or to participate in this conversation.