php - Laravel Passport API authentication fails testing

647

I'm using laravel with passport for api authentication.

When I test my method which contains code to login the user, I get a stacktrace error:

Class throttle does not exist {"exception":"[object] (ReflectionException(code: -1): Class throttle does not exist at C:\xampp\caramel\vendor\laravel\framework\src\Illuminate\Container\Container.php:752)

Login User Function

  private function loginUser($user, $password)
{
    $client = DB::table('oauth_clients')->where('id', 2)->first();
    //form parameters to get access token
    $params = [
        'client_id' =>$client->id,
        'client_secret' =>  $client->secret,
        'grant_type' => 'password',
        'username' => $user->email,
        'password' => $password,
        'scope' => '*'
    ];

    $request = Request::create('oauth/token', 'POST',$params);
    $response = json_decode(Route::dispatch($request)->getContent());
    $data = array();
    if($response->access_token != null)
    {
        $data['response'] = config('app.success');
    }
    else
    {
        $data['response'] = config('app.failed');
    }
    $data['user'] = $user;
    $data['token'] = $response;

    return $data;
}

Test Function

public function testLoginUser()
{
    $user = User::find(1);

    $class = App::make('App\Http\Controllers\v1\Auth\api\PassportLogin');

    $methodName = 'loginUser';
    $method = $this->privateMethod($class,$methodName);;
    $data = $method->invokeArgs($class,array($user,'caramel'));
    $this->assertNotNull($data);

}

Stacktrace

testing.ERROR: Class throttle does not exist {"exception":"[object] (ReflectionException(code: -1): Class throttle does not exist at C:\xampp\caramel\vendor\laravel\framework\src\Illuminate\Container\Container.php:752)

250

Answer

Solution:

might it can help you , adding this line to array.

protected $routeMiddleware = [
....
....
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
....

];

People are also looking for solutions to the problem: equivalent PHP sha1 in C#

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.