PHP Unit Tests with Laravel Auth0 Integration

Hey everyone,

So I was looking at doing some units tests on my Laravel application but I am unable to find a solution on how to pass an authenticated user to the the test so we can test protected routes, no user data is stored locally other than there auth0 id. All user management is done via the management API so mitigate any chance of data becoming out of sync.

Example test below

public function the_dashboard_route_returns_a_successful_response(): void
    {
        // Unauthenticated user
        $response = $this->get(route('Dashboard'));

        $response
            ->assertStatus(302)
            ->assertRedirectToRoute('Login');

        // Authenticated User
        $user = ""; // <-- How can I generate an authenticated user

        $response = $this->actingAs($user)->get(route('Dashboard'))
            ->assertOk()
            ->assertViewIs('pages.dashboard');
    }

All the Auth0 blog posts seem to be down and I have had no response for the support, so hoping the community can help.

Thanks in advance!

To answer your question of “How can I generate an authenticated user”, $this->actingAs is what handles the “authentication”, if you’re wanting to create a User then I’d recommend utilising Laravel Factories

Also it’s worthwhile pointing out that this should be a Feature test, not a Unit test as you’re testing against a Feature and not a Unit of code :slightly_smiling_face:

More information here; https://laracasts.com/discuss/channels/laravel/what-is-the-difference-of-unit-test-and-feature-test