Skip to content

Commit

Permalink
Take excluded_middleware into account for middleware assertions (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nestecha authored Mar 8, 2022
1 parent 7d2fa95 commit 2547c03
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Traits/AdditionalAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ public function assertActionUsesMiddleware($controller, $method, $middleware = n
PHPUnitAssert::assertNotNull($route, 'Unable to find route for controller action (' . $controller . '@' . $method . ')');
}

$excludedMiddleware = $route->action['excluded_middleware'] ?? [];
$usedMiddlewares = array_diff($route->gatherMiddleware(), $excludedMiddleware);

if (is_array($middleware)) {
PHPUnitAssert::assertSame([], array_diff($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . implode(', ', $middleware) . ')');
PHPUnitAssert::assertSame([], array_diff($middleware, $usedMiddlewares), 'Controller action does not use middleware (' . implode(', ', $middleware) . ')');
} else {
PHPUnitAssert::assertTrue(in_array($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . $middleware . ')');
PHPUnitAssert::assertTrue(in_array($middleware, $usedMiddlewares), 'Controller action does not use middleware (' . $middleware . ')');
}
}

Expand All @@ -78,7 +81,9 @@ public function assertRouteUsesMiddleware(string $routeName, array $middlewares,
$router = resolve(\Illuminate\Routing\Router::class);

$route = $router->getRoutes()->getByName($routeName);
$usedMiddlewares = $route->gatherMiddleware();

$excludedMiddleware = $route->action['excluded_middleware'] ?? [];
$usedMiddlewares = array_diff($route->gatherMiddleware(), $excludedMiddleware);

PHPUnitAssert::assertNotNull($route, "Unable to find route for name `$routeName`");

Expand Down

0 comments on commit 2547c03

Please sign in to comment.