Skip to content

Instantly share code, notes, and snippets.

@kirkbushell
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkbushell/b7a4948d6f9506f42e3b to your computer and use it in GitHub Desktop.
Save kirkbushell/b7a4948d6f9506f42e3b to your computer and use it in GitHub Desktop.
Avoid Laravel 5 middleware in tests
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Pipeline\Pipeline;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
];
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth'
];
/**
* Send the given request through the middleware / router. This overloads the parent's
* method and checks to see if middleware is enabled or not for the request. It is
* possible for certain environments (such as testing) that middleware should not be
* loaded.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendRequestThroughRouter($request)
{
$this->app->instance('request', $request);
$this->bootstrap();
$pipeline = (new Pipeline($this->app))->send($request);
// Define a new environment variable that you can call here
// true = middleware enabled, false = don't run requests through middleware
if (env('ENABLE_MIDDLEWARE')) {
$pipeline->through($this->middleware);
}
return $pipeline->then($this->dispatchToRouter());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment