Mock Middleware for route testing in Laravel

I am trying to write some unit tests to ensure that my routes were not accidentally rewritten. I already found the answer to check if the correct controller is assigned to a specific route here .

However, I would also like to check if the mediators are correctly assigned to the route. I tried a similar approach with

$tmp = new CorsService;
$corsMiddleware = Mockery::mock('Barryvdh\Cors\HandleCors[handle]', array($tmp))
    ->shouldReceive('handle')->once()
    ->andReturnUsing(function($request, Closure $next) {
        return $next($request);
    });

\App::instance('Barryvdh\Cors\HandleCors', $corsMiddleware);

For some reason, the test does not pick it up. I assume this is because middleware instances are not saved using App::instance.

What am I doing wrong?

+4
source share
2

, , 2

  • ->shouldReceive Mockery::mock
  • \ Closure

:

$tmp = new CorsService;
$corsMiddleware = Mockery::mock('Barryvdh\Cors\HandleCors[handle]', array($tmp));
$corsMiddleware->shouldReceive('handle')->once()
    ->andReturnUsing(function($request, \Closure $next) {
        return $next($request);
    });

\App::instance('Barryvdh\Cors\HandleCors', $corsMiddleware);
+3

// Get Routes
    foreach (Route::getRoutes() as $route) {
        $middleware = $route->gatherMiddleware();
        $name = $route->getName();
        \Log::debug($name.'--');
        \Log::debug($middleware);
    }
0

All Articles