How to Disable Selected Middleware in Laravel Tests

前端 未结 4 1372
心在旅途
心在旅途 2020-12-30 03:39

So it is common that errors from Authentication and CSRF arise when running phpunit.

So in the TestCase we use:

use WithoutMiddleware;

4条回答
  •  难免孤独
    2020-12-30 04:12

    Laravel >= 5.5

    As of Laravel 5.5, the withoutMiddleware() method allows you to specify the middleware to disable, instead of disabling them all. So, instead of modifying all of your middleware to add env checks, you can just do this in your test:

    $this->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);
    

    Laravel < 5.5

    If you're on Laravel < 5.5, you can implement the same functionality by adding the updated method to your base TestCase class to override the functionality from the framework TestCase.

    PHP >= 7

    If you're on PHP7+, add the following to your TestCase class, and you'll be able to use the same method call mentioned above. This functionality uses an anonymous class, which was introduced in PHP7.

    /**
     * Disable middleware for the test.
     *
     * @param  string|array|null  $middleware
     * @return $this
     */
    public function withoutMiddleware($middleware = null)
    {
        if (is_null($middleware)) {
            $this->app->instance('middleware.disable', true);
    
            return $this;
        }
    
        foreach ((array) $middleware as $abstract) {
            $this->app->instance($abstract, new class {
                public function handle($request, $next)
                {
                    return $next($request);
                }
            });
        }
    
        return $this;
    }
    

    PHP < 7

    If you're on PHP < 7, you'll have to create an actual class file, and inject that into the container instead of the anonymous class.

    Create this class somewhere:

    class FakeMiddleware
    {
        public function handle($request, $next)
        {
            return $next($request);
        }
    }
    

    Override the withoutMiddleware() method in your TestCase and use your FakeMiddleware class:

    /**
     * Disable middleware for the test.
     *
     * @param  string|array|null  $middleware
     * @return $this
     */
    public function withoutMiddleware($middleware = null)
    {
        if (is_null($middleware)) {
            $this->app->instance('middleware.disable', true);
    
            return $this;
        }
    
        foreach ((array) $middleware as $abstract) {
            $this->app->instance($abstract, new FakeMiddleware());
        }
    
        return $this;
    }
    

提交回复
热议问题