when I want to test policies using PHPUnit it does not work as expected it works based on gate method in novaServiceProvider
protected function gate()
{
Gate::define('viewNova', function () {
return true;
});
}
it only hits this method all policies are ignored, it seems that when using API this method override all policies,
however, when using web all policies are working as expected and it doesn't seem to hit this method at all
how to make policies work as expected in PHPUnit testing
public function test_view_any()
{
create(Branch::class, ['name' => 'admin',]);
$user = create(User::class, ['branch_id' => 1,]);
$this->signIn($user);
$role = create(Role::class, ['name' => 'admin']);
$user->roles()->attach($role->id);
$this->get('/resources/customers')
->assertStatus(200);
}
this is the test
public function viewAny(User $user)
{
return $user->hasPermission('customers-index-all')
|| $user->hasPermission('customers-index-branch')
|| $user->hasPermission('customers-index-self');
}
and this is the policy
now the test must be green if the user has the required role the is defined in CustomerPolicy viewAny method, but what is actually happening is even if I return true in viewAny Method it will give Expected status code 200 but received 403. as long as novaView gate returns false but if novaView gate returns true even if the policy method viewAny returns false it will still be green and that is the case for all other policies I mentioned the Customer policy as an example