Permissions table referred to in AuthServiceProvider fails to migrate during PHPUnit test
When I run vendor/bin/phpunit against a simple test I get:
There was 1 error:
1) Tests\Feature\CommissionsTest::when_adding_a_subscription_also_assign_commission
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: permissions (SQL: select * from "permissions")
I'm using an in-memory SQLite database.
In AuthServiceProvider.php when I comment all code related to the permissions framework the tests run. The code is:
public function boot()
{
// $this->registerPolicies();
//
// // Dynamically register permissions with Laravel's Gate.
// foreach ($this->getPermissions() as $permission) {
// Gate::define($permission->name, function ($user) use ($permission) {
// return $user->hasPermission($permission);
// });
// }
}
I took a chance and added 'use WithoutMiddleware` but this didn't chance anything.
Where am I going wrong? It seems although I have use DatabaseMigrations the databases are not migrated by the time it hits the AuthServiceProvider.
To avoid this issue, you can verify if the table called "permissions" exists before it tries to hit the Permission model on the function getPermissions()
public function boot()
{
$this->registerPolicies();
if (Schema::hasTable('permissions')) {
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function ($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}
}
protected function getPermissions()
{
return Permission::with('roles')->get();
}