尝试测试 laravel/passport auth cicle 时出错

Error trying to test laravel/passport auth cicle

我在尝试测试我的应用程序的身份验证流程时遇到了 Laravel/Passport 的一些问题... 奇怪的是,它只发生在我测试环境时。期望 200 http 代码但收到 500。

我用的是Laravel 5.8版本,我也安装了Horizo​​n和Telescope。我的 PHP 版本是 7.3.4。

我的测试

public function testLogin()
    {
        $user = factory(\App\Models\User::class)->create();

        $response = $this->json('post', 'v1/login', [
            'email' => $user->email,
            'password' => 'secret',
        ]);

        $response->assertStatus(Response::HTTP_OK);
    }

我的授权控制器

public function login(AuthRequest $request)
    {
        try {
            $credentials = $request->only(['email', 'password']);
            throw_if(!auth()->attempt($credentials), UnauthorizedAccess::class, __('auth.auth_required'));

            $accessToken = $this->handleAccessToken($credentials);
            return response()->json($accessToken);
        } catch (UnauthorizedAccess $ex) {
            return response()->json([
                'success' => false,
                'message' => $ex->getMessage()
            ], Response::HTTP_UNAUTHORIZED);
        }
    }

private function handleAccessToken(array $credentials)
    {
        $user = User::where(['email' => $credentials['email']])->first();
        $http = new Client();

        $response = $http->post(url('oauth/token'), [
            'form_params' => [
                'grant_type'    => 'password',
                'client_id'     => $user->client->id,
                'client_secret' => $user->client->secret,
                'username'      => $credentials['email'],
                'password'      => $credentials['password'],
                'scope'         => '*',
            ],
        ]);

        return json_decode((string) $response->getBody(), true);
    }

这是产生的错误:

在终端上

1) Tests\Feature\AuthTest::testLogin
Expected status code 200 but received 500.
Failed asserting that false is true.

laravel.log

[2019-04-26 09:02:53] local.ERROR: Client authentication failed {"exception":"[object] (League\OAuth2\Server\Exception\OAuthServerException(code: 4): Client authentication failed at /home/***/workspace/***/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:138)
[stacktrace]

[2019-04-26 09:02:53] testing.ERROR: Client error: `POST http://***/oauth/token` resulted in a `401 Unauthorized` response:
{"error":"invalid_client","error_description":"Client authentication failed","message":"Client authentication failed"}
 {"userId":"52b19d0d-e0c5-4924-84ae-d07700c672df","exception":"[object] (GuzzleHttp\Exception\ClientException(code: 401): Client error: `POST http://***/oauth/token` resulted in a `401 Unauthorized` response:
{\"error\":\"invalid_client\",\"error_description\":\"Client authentication failed\",\"message\":\"Client authentication failed\"}
 at /home/***/workspace/***/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113)
[stacktrace]

好吧,这个错误似乎是 Guzzle 本身在测试 env 时出现的。发生这种情况是因为 Guzzle 设置为默认环境。

然后,我按照@lawrencedawson的回答解决了,大家可以看到here