php - How to not display certain errors for Laravel 7 feature test

I have 2 methods in my feature test LoginTest.php that check for incorrect password or username.

When I run vendor/bin/phpunit I don't get any errors reported: OK (40 tests, 154 assertions)

However, I do get the following error displayed. This is expected since login is supposed to fail.

testing.ERROR: The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client. {"exception":"[object] (Laravel\Passport\Exceptions\OAuthServerException(code: 10): The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client. at /app/vendor/laravel/passport/src/Http/Controllers/HandlesOAuthErrors.php:26)

Here are the 2 methods that are causing the issue.

{
    $this->createAccount("test@test.com");

    $loginForm = array(
        "grant_type" => "password",
        "client_id" => "2",
        "client_secret" => "lGk9EYlEHdQfXKy0EdTJ5S4Y126y0lkz0ofXiUXe",
        "scope" => "*",
        "username" => "test@test12.com",
        "password" =>  $this->formData["password"]
    );
    $response = $this->json('POST', '/oauth/token', $loginForm);
    $response->assertStatus(400);
}

public function testLoginPasswordIncorrect()
{
    $this->createAccount("test@test.com");
    $loginForm = array(
        "grant_type" => "password",
        "client_id" => "2",
        "client_secret" => "lGk9EYlEHdQfXKy0EdTJ5S4Y126y0lkz0ofXiUXe",
        "scope" => "*",
        "username" => $this->formData["email"],
        "password" => "Wrong"
    );
    $response = $this->json('POST', '/oauth/token', $loginForm);
    $response->assertStatus(400);
}

Should I not be bothered about these displayed errors? Or is there a way to not display them?

Answer

Solution:

To avoid those messages, you can use the "expectException" and "withoutExceptionHandling" methods before making the HTTP request. However, if you choose to use them, you get an exception instead of an HTTP response (Because of this, you cannot use the "assertStatus" method).

    // ...
    
    $this->withoutExceptionHandling();
    
    $this->expectException(\Laravel\Passport\Exceptions\OAuthServerException::class);
    
    $response = $this->json('POST', '/oauth/token', $loginForm);

Source