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

jarcas's avatar
Level 1

Laravel 9->12, some tests now redirect to /

Ever since I updated from Laravel 9 to 12 (One version at a time), some of my tests are failing. I have no idea why, but for some reason they're returning 302 a redirect to /. I've tried disabling middleware that may be causing this, but that doesn't help. Anyone have any idea how I can figure out what's going on here? The admin.downloads.store works properly - it stores the data in the db properly, where it fails is the admin.downloads_groups.edit route asserting status 200 - it returns 302 and the redirect is to / for some reason instead of /login.

    public function test_can_view_edit_individual_download($values) {
        Storage::fake('local');
        
        $response = $this->postJson(route('admin.downloads.store'), $values);

        $downloadGroup = DownloadsGroup::first();

        $response = $this->get(route('admin.downloads_groups.edit', $downloadGroup->id));
        $response->assertStatus(200);
    }

Setup:

    public function setUp(): void {
        parent::setUp();
        $this->setupAdmin();
    }

setupAdmin:

    private function setupAdmin() {
        $user = User::factory()->create($this->userAdmin);
        $user->level = "admin";
        $user->save();
        $this->actingAs($user);

        return $user;
    }
0 likes
4 replies
martinbean's avatar

@jarcas You shouldn’t be making multiple requests in a single test case like that. It’s not supported and can lead to unexpected side effects because the framework isn’t re-booted between requests like it would in a production environment.

Use factories to create records instead:

public function test_can_view_edit_download_group_form(): void
{
    $downloadGroup = DownloadGroup::factory()->create();

    $response = $this->get(route('admin.download_groups.edit', $downloadGroup));

    $response->assertOk();
}

Test cases are called test cases because they should test a single case instead of having multiple concerns like creating a record and then retrieving it.

jarcas's avatar
Level 1

@martinbean Thanks, fixed that but I'm still getting a 302 for some reason. While I was updating the code to use a factory, I forgot to assign a column and got an error from the view in the controller's method I'm trying to access, but once I got the factory working properly I'm getting the 302 again.

Any ideas as to why that'd be?

martinbean's avatar

@jarcas Don’t know. Temporarily dump the response to see where it’s trying to redirect to:

  public function test_can_view_edit_download_group_form(): void
  {
      $downloadGroup = DownloadGroup::factory()->create();

      $response = $this->get(route('admin.download_groups.edit', $downloadGroup));

+     $response->dump();
      $response->assertOk();
}
jarcas's avatar
Level 1

I've tried dumping and sadly it doesn't help much, however I have made some progress:

        $response = $this->withoutMiddleware(null)->withMiddleware([
            \App\Http\Middleware\Authenticate::class,
            \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\Http\Middleware\HandleCors::class,
        ])->get(route('admin.downloads_groups.edit', $downloadsGroup));

With this the route kinda works - it returns the view, however $downloadsGroup is null. It's set correctly in the web.php routes, and the controller. Is there some middleware that handles passing parameters to routes that I'm missing?

Please or to participate in this conversation.