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

Konstruktionsplan's avatar

Testing-Error. 🤞🏻

Hello friends!

Somehow he does not want. What do wrong? 🤔

Laravel Doc Example:

class ExampleTest extends TestCase
{
    public function test_an_action_that_requires_authentication()
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)
                         ->withSession(['banned' => false])
                         ->get('/');
    }
}

My Code:

        $user = User::factory()->create();

        $response = $this->actingAs($user)->get('register');
        $response->assertStatus(200);

Error

  Expected status code 200 but received 302.
  Failed asserting that 200 is identical to 302.

and PHPStorm say, when i hover above actingAs($user):

Expected parameter of type '\Illuminate\Contracts\Auth\Authenticatable', '\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model' provided 
0 likes
16 replies
MehdiAghighi's avatar

Your user model should implement \Illuminate\Contracts\Auth\Authenticatable interface. you can do this and all laravel default user authentication stuff to your user model by extending Illuminate\Foundation\Auth\User class in your user model.

2 likes
Konstruktionsplan's avatar

Now I didn't quite understand that. Authenticatable is already in the user model. And my other models should now no longer inherit from "Model", but from Illuminate\Foundation\Auth\User?

Tray2's avatar

302 means redirected. I take it you are using breeeze, jetstream or laravel ui? If so a logged in user should be redirected when hitting the register route.

You are moste likely redirected to /home.

2 likes
MehdiAghighi's avatar

@tray2 i don't think so. i think he is being redirected to login page because the user he is giving to actingAs method is not implementing Authenticable contract as i said above.

i think this is the problem

2 likes
tykus's avatar

Not the case I expect. More likely an authenticated user should not be able to see the Register page, and is automatically redirected. So the test expectation actually is incorrect.

The IDE complaining about types is a false flag.

1 like
marbobo's avatar

The IDE complaining about types is a false flag.

I agree with this. I just tried it on my fresh laravel installation. I got the test running correctly but my IDE phpstorm is complaining. do we have a fix to this?

marbobo's avatar

I don't know if this going to help. But I got rid of the PHPstorm orange highlight by adding type.. here how I did this.

P.S. i already implemented \Illuminate\Contracts\Auth\Authenticatable interface on my User model

/** @var \Illuminate\Contracts\Auth\Authenticatable $user */
$request = $this->actingAs($user, 'api')->getJson('/api/v1/campaigns');
8 likes
wilmix's avatar

I don't know why... but it works...thanks

Konstruktionsplan's avatar

The idea in the test is to call the user factory and create a user, take it and then get to the protected page. So it would be e.g. ->get('clients') or something like that.

Register I tried because I wanted to see if a new user could look at pages that are public.

But both don't work at the moment.

tykus's avatar

The idea in the test is to call the user factory and create a user, take it and then get to the protected page.

Yes. Authenticated (and authorized if relevant) should get a 200 response; while unauthenticated (and unauthorized) would be 401 and 403 respectively. If you have exception handling enabled, then the framework will handle those exceptions, and redirect (302) the request appropriately.

if a new user could look at pages that are public

Typically, endpoints such as register are protected with the guest middleware, so an authenticated user cannot visit them.

1 like
Konstruktionsplan's avatar

Useful knowledge. Thank you. :)

Nevertheless, does anyone have a hint for the solution?

Cobs's avatar

I could be unrelated but... why an already authenticated user would like to register ? this is an action for unauthenticated user only ?

  • Maybe check the destination route is correct
  • Also where are you redirected to ? what is the 'location' header in the response ?
  • If you have xdebug with step debugging, maybe check if Exceptions are thrown during the request. it would help to understand if it's an authentication problem or not.
  • Does your route use the default auth guard ?
jxjj's avatar

If it helps anyone else, I fixed this error in VSCode by adding a $model to my UserFactory:

class UserFactory extends Factory {

+    protected $model = \App\User::class;

...
}

Then reran Laravel IDE Helper for good measure:

$ sail artisan ide-helper:generate
$ sail artisan ide-helper:models
1 like

Please or to participate in this conversation.