Summer Sale! All accounts are 50% off this week.

alexlegard's avatar

phpunit tests 500 status confusion with multiple guards

What my test is trying to do, is test if an admin can visit the "profile" route, and the intended behavior is they should be redirected to the "login" page so they can login as a user. Important to note is that an admin is a different guard than a user.

The test currently fails, with the message that "Response status code [500] is not a redirect status code." I'm not really sure how I should deal with this issue.

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\User;
use App\Admin;
use App\SuperAdmin;

class AccountPageTest extends TestCase
{
    use DatabaseTransactions;

	/** My other tests.... **/

    /** @test **/
    public function admins_cannot_access_page()
    {
        $this->refreshApplication();

        $admin = Admin::find(1);

        $response = $this->actingAs($admin, 'admin')
            ->get('profile')
            ->assertRedirect('login');
    }
}


0 likes
25 replies
tykus's avatar

Disable exception handling to get a full stack trace.

public function admins_cannot_access_page()
{
    $this->refreshApplication();
    $this->withoutExceptionHandling();
    // rest of your test
lemmon's avatar

I guess we were typing at the same time @tykus, sorry about that.

lemmon's avatar

@alexlegard

Try without exception handling so maybe it will help you find out what the internal server error indicated by the 500

/** @test **/
public function admins_cannot_access_page()
{
		$this->withoutExceptionHandling();

don't forget to import it.

alexlegard's avatar

@lemmon @tykus I'll give your suggestions a try, and I have Admin and User user types because I want only admins to be able to use the admin panel to manage the website.

lemmon's avatar

@alexlegard

Try assigning a role or roles to a user through a pivot table. Just a suggestion :)

alexlegard's avatar

How to import withoutExceptionHandling? Just trying to make sure everything's right.

lemmon's avatar

@alexlegard

Oops, you do not need to import it.

Just add the below inside the test

$this->withoutExceptionHandling();
alexlegard's avatar

It doesn't seem to make a difference unfortunately.

  1. Tests\Feature\AccountPageTest::admins_cannot_access_page Response status code [500] is not a redirect status code. Failed asserting that false is true.

/** @test **/
    public function admins_cannot_access_page()
    {
        $this->withoutExceptionHandling();

        $this->refreshApplication();

        $admin = Admin::find(1);

        $response = $this->actingAs($admin, 'admin')
            ->get('profile')
            ->assertRedirect('login');
    }

lemmon's avatar

Are you using a separate database for testing?

lemmon's avatar

What version of laravel are you using?

lemmon's avatar

Why are you using RefreshApplication?

alexlegard's avatar

I am.

I changed the order of the first two statements and get a different error. (It might just be something I should work out on my own)

  1. Tests\Feature\AccountPageTest::admins_cannot_access_page TypeError: Argument 1 passed to App\Http\Controllers\ProfilesController::getBillingAddressString() must be an instance of App\Profile, instance of App\AdminProfile given, called in C:\Users\Owner\Documents\WebDevelopment\ReachWebExperts_Github\ReachWebExpertsProject\app\Http\Controllers\ProfilesController.php on line 24

/** @test **/
    public function admins_cannot_access_page()
    {
        $this->refreshApplication();

        $this->withoutExceptionHandling();

        $admin = Admin::find(1);

        $response = $this->actingAs($admin, 'admin')
            ->get('profile')
            ->assertRedirect('login');
    }

lemmon's avatar

There you go, Why are you refreshing the app?

There is the problem

That is the error you want

ProfilesController.php on line 24

alexlegard's avatar

I can't exactly remember why I'm refreshing the app, as I wrote this code a year ago (but didn't finish fixing the code for whatever reason)

lemmon's avatar

Also DatabaseTransactions is 5.4, so wat version are you using?

lemmon's avatar

So when you call

$response = $this->actingAs($admin, 'admin')
            ->get('profile')

it is calling the method

App\Http\Controllers\ProfilesController::getBillingAddressString()

and that expects an instance of App\Profile but it is given and instance of App\AdminProfile and it is not happy for that.

lemmon's avatar

Now you got the info to debug. good luck :)

alexlegard's avatar

@lemmon Thanks so much for the assistance. Now I have one other small problem. Now I have a test failure, instead of an error. I have the assertion:

$response = $this->actingAs($admin, 'admin') ->get('profile') ->assertRedirect('login');

And error:

  1. Tests\Feature\AccountPageTest::admins_cannot_access_page Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'http://127.0.0.1:8000/login' +'http://127.0.0.1:8000'

If I change ->assertRedirect('login'); to ->assertRedirect(''); then the test passes. But I know that the admin is redirected to /login page because I tested it in my own browser. I'm not sure what's behind this weird behavior.

lemmon's avatar

Die dump in the controller it might not even be getting to the part that redirects

lemmon's avatar

The redirect may be coming from somewhere else and happening after the test fails you got a post some code

alexlegard's avatar

Oh it's probably my isUser middleware.


namespace App\Http\Middleware;

use Closure;
use Auth;

class IsUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //If user is a user...
        if( auth::guard('web')->check() ) {
            return $next($request);
        }
        
        return redirect('');
    }
}

lemmon's avatar

Maybe you need to poke around in laracasts and take a little instruction. Just a suggestion , Maybe bring your testing methods up to date as well :)

alexlegard's avatar

Ok, now the test seems to be bug free :) thanks again for the help.

Please or to participate in this conversation.