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

AmirrezaN's avatar

Should I write tests for middleware existence on routes?

Should we write tests to see if a specific middleware is being executed on route (groups)?

I'm not talking about the middleware functionality since most of them are already tested by the framework but the existence of them in specific routes' middleware pipeline. Currently, I'm parsing output of php artisan route:list to get given middlewares on a route and check it to see if it contains auth:api or scope:something for example. But doing that kind of parsing, not founding related kind of tests on the web and etc make me think that I'm doing this wrong way.

Should I continue doing so? It gives me more confidence and I've actually used it to prevent a bug once.

0 likes
1 reply
Tray2's avatar

I would do that for auth routes something like

/** @test */
function a_guest_is_redirected_when_trying_to_access_books_create()
{
    $response = $this->get('/books/create');
    $response->assertLocation('/login');
}


1 like

Please or to participate in this conversation.