huuuk
huuuk

Reputation: 4795

Disable Laravel 5 (not 5.1) middleware for testing

Is there a way to turn off some Laravel 5.0 middleware for functional tests?

Upvotes: 0

Views: 335

Answers (2)

huuuk
huuuk

Reputation: 4795

I've found simple solution, although probably it may be not "TRUE", but it works. I've modified method handle() in my app/Http/Middleware/VerifyCsrfToken.php to this

public function handle($request, Closure $next)
{
    if (env('APP_ENV') === 'testing') {
        return $next($request);
    }
    return parent::handle($request, $next);
}

Upvotes: 0

Amarnasan
Amarnasan

Reputation: 15579

Just edit app/Http/kernel.php file and comment any undesired middleware line in array $middleware.

You don't need to comment the ones in $routeMiddleware since these won't be automatically called, and they need to be specifically activated in the routes.php file.

Another way:

Copy Kernel.php as Kerneltest.php in the same folder.

Then rename the class in Kerneltest.php to Kerneltestand make it extends Kernel.

Then remove any middleware lines from Kerneltest

Then add the following to bootstrap\app.php :

$app->singleton(
    'Illuminate\Contracts\Http\Kerneltest',
    'App\Http\Kerneltest'
);

Then in public\index.php use

$kernel = $app->make('Illuminate\Contracts\Http\Kerneltest');

or

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');

depending on whether you're testing or not.

Upvotes: 2

Related Questions