Selectively Enabling Middleware in a Laravel Lumen Unit Test

Testing

Committed to the LaraBrain by: wjgilmore (@wjgilmore) at July 28, 2016 4:37 pm

It's typical to disable middleware when running tests, but what if you want to selectively enable it?

Disabling middleware when running your Lumen unit tests can be done by setting the middleware.disable flag to true inside your project's TestCase:

class TestCase extends Laravel\Lumen\Testing\TestCase
{

    ...

   public function setUp()
   {
       parent::setUp();
       $this->app->instance('middleware.disable', true);
   }

   ...

}

However, what if you wanted to selectively enable middleware in a unit test, perhaps to test the middleware itself? You can do so by adding this line to the top of your test:

    app()->instance('middleware.disable', false);