0
I have a legacy application in Laravel where users can see or not certain system module according to their permissions. These permissions are saved to the database in N:N form and managed in Userpolicy.
Follow an example:
public function viewReports(User $user)
{
foreach($user->module as $m){
if($m->id === 3)
return true;
}
}
In order for me to refactor the code, I need to write tests to make sure that the changes don’t break the code. However, I don’t know how to proceed. I momentarily wrote the following test just to make sure it works, but I know it’s not good practice (since I’m using data recorded in the database):
public function testCanViewReports()
{
$user = User::findOrFail(1);
$this->assertTrue($user->can('viewReports', User::class));
}
What is the best way to create a stunt double for this test without relying on database?