Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unnaturaltm's avatar

Second actingAs($user) failing

Hi, I'm having a weird bug. My application is working fine locally, but in the API tests, only the first call with actingAs($user) works.

In a previously passing test, I just duplicate the assertion like so -

$response = $this->actingAs($user)
            ->get('route')
             ->seeStatusCode(200);

$response = $this->actingAs($user)
            ->get('route')
             ->seeStatusCode(200);

In the controller function that this test hits, I log the Auth-ed user -

codecept_debug(
        is_null(Auth::user()) ? 'null' : 'have user'
);

Now when I run the test it logs "have user" and then "null" and the test fails on the line of the second assertion (because I have a gate according to the user roles and all that - no user, no role).

(Extra info - This weird behaviour started happening after i upgraded from laravel/lumen 6. I don't have laravel/ui because it's a REST api. We already had our own OAuth stuff working so if it's because I need Passport then I don't get how it works the first assertion.)

I logged the user in TestCase.php where actingAs() is defined and it looks okay, but I don't know where to go from there. Where else can I log to figure out what's wrong? Would anyone know off hand what's the problem? (Pages with multiple authed API requests work fine so it's just the tests.)

0 likes
2 replies
bugsysha's avatar

What is the scenario of this test? I never use multiple requests in one tests cause I had issues with that approach long time ago. Single request per test.

unnaturaltm's avatar

The test is just one of many in the suite, and I put two requests just to make the point that all after the first that passes, all fail because somehow the Auth::user() is null. The first passing is even weirder than if all failed.

Please or to participate in this conversation.