Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Laravel Passport

I have a problem - I am using Laravel Passport for my api. I need to write tests. Whenever I use the WithoutMiddleware trait in my tests it disables the Implicit route model binding feature, which I use. Whenever I don't use that trait, I need to authenticate directly from my test. To do that, I need to create an API token via Passport.

Passport, however, needs to be installed before testing via artisan passport:install, because my tests are using DatabaseTransactions and DatabaseMigrations traits. When I do this, tests take enormous amount of time to run and I feel like it is not the right way to go about it. Isn't there a way to disable only the auth middleware? Or any other ideas to go about this?

like image 317
Pavel Koch Avatar asked Oct 29 '22 18:10

Pavel Koch


1 Answers

A little late to the party, but according to this issue this functionality is not currently supported with no plans to change that anytime soon.

However, all Laravel test classes inherit the withoutMiddleware method, which you can use to disable middleware on specific methods. Not sure if this is helpful to you, but just throwing it out there:

public function testBasicExample()
{
    $this->withoutMiddleware();

    $this->visit('/')
         ->see('Laravel 5');
}

You can also check to see if tests are running within the middleware itself by calling the runningUnitTests() method on the Application instance.

like image 59
samrap Avatar answered Nov 10 '22 03:11

samrap