You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 7功能测试中如何屏蔽指定错误信息?

Laravel Passport测试时的错误日志问题

我在功能测试文件LoginTest.php里编写了两个方法,分别校验用户名错误和密码错误的登录场景。执行vendor/bin/phpunit后测试结果显示全部通过:OK (40 tests, 154 assertions),但控制台会输出如下预期内的错误日志:

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)

引发问题的两个方法代码如下:

// 用户名错误的测试方法
public function testLoginUsernameIncorrect()
{
    $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);
}

想问下这些错误日志需要在意吗?有没有办法屏蔽掉?


要不要在意这些错误?

不用在意。这些日志是Laravel Passport在验证失败时正常抛出的异常信息,你的测试已经通过assertStatus(400)的校验,说明场景完全符合预期。日志只是框架正常记录的错误流程,不会影响测试结果的有效性。

屏蔽日志的方法

有几种简单可行的方案:

  1. 临时调整测试环境的日志级别
    在测试类的setUp方法中添加代码,临时将日志级别调高,只记录最严重的错误:
protected function setUp(): void
{
    parent::setUp();
    // 测试期间仅记录emergency级别的日志
    config(['logging.channels.stack.level' => 'emergency']);
}

测试结束后Laravel会自动重置配置,不会影响其他环境的日志记录。

  1. 在测试方法中声明预期异常
    针对Passport的OAuthServerException,可以在测试方法开头声明要捕获的异常,这样框架就不会输出对应的日志:
public function testLoginUsernameIncorrect()
{
    $this->createAccount("test@test.com");
    // 声明预期会抛出该异常
    $this->expectException(\Laravel\Passport\Exceptions\OAuthServerException::class);

    $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);
}

使用expectException后,测试会自动验证该异常被抛出,无需额外处理。

  1. 自定义错误处理器(不推荐)
    如果需要全局修改Passport的错误日志行为,可以自定义错误处理器,但这种方式需要修改框架扩展的逻辑,后续升级Passport时容易出现兼容性问题,仅建议在特殊场景下使用。

内容的提问来源于stack exchange,提问作者Maxime Barber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:50:24