pilat's avatar
Level 41

Cannot test HTTP responses when using abort() inside a service provider

Hi, an't figure out one thing:

I have the following code in my Service Provider (it's deferred, by the way):

if ($someCond) {
  abort(404, 'blah-blah-blah');
}

… and here's the test:

/** @test */
public function test_regression_404_error_did_not_consider_auth_status()
{
    $this
        ->actingAs(User::find(1))
        ->get('section/somethingimaginary')
        ->seeStatusCode(404)
        ->see('something that only logged user should see on the error page');
}

When I try to run this test, I see the following error in PHPUnit's output:

1) MiddlewaresTest::test_regression_404_error_did_not_consider_auth_status
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: blah-blah-blah

How can I make this test work?

Notes:

  • the service provider is deferred;

  • when trying this same URL in the browser, I can actually see the error page I expect and the browser knows it has 404 response code;

  • there is something conditional (dependent on auth()->check()) on the error page, and it doesn't work, in fact. The error screen is alway as there were no logged in user. When I did abort(403) from a controller, this issue was not here.

0 likes
0 replies

Please or to participate in this conversation.