How to Disable Selected Middleware in Laravel Tests

前端 未结 4 1359
心在旅途
心在旅途 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;
    }
    
    0 讨论(0)
  • 2020-12-30 04:25

    The best way I have found to do this isn't by using the WithoutMiddleware trait but by modifying the middleware you want to disable. For example, if you want to disable the VerifyCsrfToken middleware functionality in your tests you can do the following.

    Inside app/Http/Middleware/VerifyCsrfToken.php, add a handle method that checks the APP_ENV for testing.

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

    This will override the handle method inside of Illuminate\Foundation\Http\Middleware\VerifyCsrfToken, disabling the functionality entirely.

    0 讨论(0)
  • 2020-12-30 04:26

    The following worked for me:

    use WithoutMiddleware;
    
    public function setUp(): void
    {
        parent::setUp();
        $this->withoutMiddleware();
    }
    
    0 讨论(0)
  • 2020-12-30 04:31

    You can use trait in test:

    use Illuminate\Foundation\Testing\WithoutMiddleware;

    Laravel >= 5.7

    0 讨论(0)
提交回复
热议问题