Tricks

Testing plugins

Jun 17, 2022
Ramón Zayas
FAQ, Integration, Admin panel

When testing filament plugins you most certainly won't have a User Model so you will have to create one in order to login to filament and navigate and execute functions within your new plugin.

I will try to explain the process I followed to do this:

Create a User model for testing.

# file: tests/Models/User.php
 
class User extends Model implements FilamentUser
{
protected $guarded = [];
public $timestamps = false;
protected $table = 'users';
public function canAccessFilament(): bool
{
return true;
}
}

A TestCase file with the database creation and the proper Filament providers. You may include any extra migration or setup code that your plugin requires.

# file: tests/TestCase.php
 
class TestCase extends Orchestra
{
protected User $adminUser;
 
protected function setUp(): void
{
parent::setUp();
 
$this->setUpDatabase($this->app);
 
}
 
protected function getPackageProviders($app)
{
return [
// Filament Service providers...
BladeHeroiconsServiceProvider::class,
BladeIconsServiceProvider::class,
FilamentServiceProvider::class,
FormsServiceProvider::class,
LivewireServiceProvider::class,
SupportServiceProvider::class,
TablesServiceProvider::class,
 
// Your package service provider...
EmailMessageServiceProvider::class,
FilamentEmailLogServiceProvider::class,
];
}
 
protected function setUpDatabase($app)
{
$app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('name');
});
 
$this->adminUser = User::create(['email' => '[email protected]', 'name' => 'Admin']);
}
}

Now in your tests you can use $this->adminUser for logging to filament.

# file: tests/MyTest.php
 
it('redirects on non logged users', function () {
$this->get(Config::get('filament.path'))
->assertRedirect();
});
 
 
it('shows dashboard to logged users', function () {
actingAs($this->adminUser)
->get(Config::get('filament.path'))
->assertSuccessful();
});
 
 
it('can render EmailResourcePage', function () {
actingAs($this->adminUser)
->get(EmailResource::getUrl('index'))
->assertSuccessful();
});

Hope I made myself clear. If you would like to view a complete example please take a look at my filament-email-log plugin.

No comments yet…