Comments & Discussion
PP
One of the clean code tips is to not defining flags as an argument for your functions. instead you should divide the login into multiple functions. By this way, you also won't violate SRP principle.
I suggest using factory states, like below. UserFactory:
class UserFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'is_admin' => false
];
}
public function admin()
{
return $this->state(function (array $attributes) {
return [
'is_admin' => true,
];
});
}
}
then using it like this:
User::factory()->admin()->create();
Hello Povilas, In the company im currently working luckymedia, in TestCase.php we create a separate public function as follows:
, then when we want to be logged as an admin we test as follows:
Is this the propper way to test, or can you suggest a better approach?
Thanks in advance
Yes, that's a perfectly fine approach, to avoid repeating the same thing in each test.