In the next couple of lessons, we'll add automated tests to ensure that our app works as expected. This first test will be to check if only the admin user can access some parts of the application.
To write tests, for the data we will use Factories.
For this test, first, we will add a factory callback to the UserFactory
called admin
. This way when we will create a user which needs to be an admin we will be able to chain the admin()
method to the factory call.
database/factories/UserFactory.php:
class UserFactory extends Factory{ // ... public function admin(): static { return $this->afterCreating(fn (User $user) => $user->update(['is_admin' => true])); }}
Now we can create a feature test...