Unit test with Laravel Passport scope

1k views Asked by At

I guys ! I have a problem when i'm trying to make some unit test with Laravel and Passport.

I have some route protected by the auth:api middleware, but some other protected by the Passport scope middleware.

For exemple, this is one of my unit test :

 public function testGetPublishedPost(){

   $user = factory(\App\User::class)->create();
   $this->actingAs($user, 'api');


   $article1 = factory(\App\Article::class)->create();
   $article2 = factory(\App\Article::class)->create();
   $article3 = factory(\App\Article::class)->create( ['published' => false);


   $json = $this->call('get', '/api/articles');
   $articles = json_decode($json->getContent());

   $this->assertEquals(200, $json->getStatusCode());
   $this->assertEquals(2, count($articles));
}

All is working fine with this one, but now, I would like to make some test including a scope.

How can i do that ?

Thanks !

1

There are 1 answers

0
JPark On BEST ANSWER

If you are not concerned about testing the auth middleware, you can just skip the passport auth altogether by defining a temporary route to your function:

Route::get('/_test/api/articles', 'App\Http\Controllers\YourApiController@yourArticleFunction');

//Gain access to Auth:user() in your api controller
$this->be($user);

//Call the temporary route
$response = $this->call('GET', '/_test/api/articles');