Last active
August 29, 2015 14:16
-
-
Save kirkbushell/b7a4948d6f9506f42e3b to your computer and use it in GitHub Desktop.
Avoid Laravel 5 middleware in tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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