laravel 使用虚拟数据库测试 passport oauth2 时测试失败

laravel testing fails when testing passport oauth2 with virtual database

我正在使用虚拟测试数据库进行测试。我的 API 正在与邮递员合作。但是在编写测​​试时会产生问题。当我执行测试时,它会显示一长串错误,其中包含以下消息-

"message": "Client error: POST http://localhost/oauth/token resulted in a 401 Unauthorized response:\n{\"error\":\"invalid_client\",\"message\":\"Client authentication failed\"}

这是我的路线-

Route::post('/v1/create', 'API\v1\UserController@register');

这是我的控制器

 public function register(Request $request) 
{ 

    $validator = Validator::make($request->all(), [ 
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:6', 'confirmed'],
    ]);
    if ($validator->fails()) { 
                return response()->json(['error'=>$validator->errors()], 401);            
            }
    $input = $request->all(); 
    $input['password'] = bcrypt($input['password']); 
    $user = User::create($input); 


    $http=new Client;

    $response=$http->post(url("/oauth/token"),[
        'form_params'=>[
            'grant_type'    =>'password',
            'client_id'     =>$request->client_id,
            'client_secret' =>$request->client_secret,
            'password'      =>$request->password,
            'username'      =>$request->email,
            'scope'         =>''             
            ]
        ]);
    // return response()->json(['success'=>$response], $this->successStatus); 
    return $response;

}

这是我的测试-

<?php

namespace Tests\Feature\API\v1;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use PharIo\Manifest\Email;

class UserTest extends TestCase
{
use WithFaker;
use DatabaseMigrations;
public $mockConsoleOutput = false;

/** @test */
public function a_user_can_create_user(){
$user= factory(User::class)->create(); //create user
$login=$this->actingAs($user,'api'); //user login with api

 //create password grant client
 $this->artisan('passport:client', ['--password' =>true, '--no-interaction' => true, '--redirect_uri'=>'http://localhost', '--name'=>'test client']);
 // fetch client for id and secret
 $client = \DB::table('oauth_clients')->where('password_client', 1)->first();

// dd($client->getData());


$email=$this->faker->email();
$password=$this->faker->password;
$newUser=$this->json('POST','/api/v1/create',[
    'grant_type'     =>'password',
    'client_id'     => $client->id,
    'client_secret' => $client->secret,
    'name' => $this->faker->name(),
    'email' => $email,
    'password' => $password,
    'password_confirmation' => $password,
    'remember_token' => str_random(10),
]);


// ->assertJsonStructure(['access_token', 'refresh_token']);



 dd($newUser);
 // $this->assertDatabaseHas('users',['email'=>$email]);
  // $newUser->assertJsonFragment(['token_type'=>'Bearer']);
  }


  }

请帮我看看我错过了什么

我已经通过代理请求解决了。发生这种情况是因为 - 当我用 guzzle 调用 "oauth/token" 端点时 - 该调用被视为真实调用并且测试在那里不起作用。所以这对我解决问题有很大帮助。

$tokenRequest = Request::create('oauth/token', 'POST',$request->toArray()); 
$response = Route::dispatch($tokenRequest);