mardi 30 juillet 2019

Problems on testing middleware in Laravel with Clousure $next

I have this middleware on my app that checks the user role for a route:

public function handle($request, Closure $next, ...$roles)
{
   if (in_array($request->user()->rol, $roles)) {
        return $next($request);
   } else {
        return redirect()->action('SecurityController@noAutorizado');
   }
}

And I'm triying to make a test for this middleware (phpUnit):

    public function testUsuarioLogadoPuedeAccederAPantallaUsuarios()
{

    $user = UsuariosTestFixtures::unAsignador();
    $this->actingAs($user);
    $request = Request::create('/usuarios', 'GET');

    $middleware = new CheckRole();
    $response = $middleware->handle($request,Closure $next,$user->getRole(), function () {});
    $this->assertEquals($response, true);

}

But i'm retreiving this error: Argument 2 passed to App\Http\Middleware\CheckRole::handle() must be an instance of Closure, null given

I don't know how I have to pass the "Closure $next" on the $middleware->handle

I've tryed this:

public function testUsuarioLogadoPuedeAccederAPantallaUsuarios(Closure $next){...}

But It returns an error: Too few arguments to function UsuarioControllerTest::testUsuarioLogadoPuedeAccederAPantallaUsuarios(), 0 passed in C:\www\APPS\catsa\vendor\phpunit\phpunit\src\Framework\TestCase.php

What's the solution?

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire