Skip to content

Assert controller action uses middleware #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ assertActionUsesFormRequest(string $controller, string $method, string $form_req

Verifies the _action_ for a given controller performs validation using the given form request.

```php
assertActionUsesMiddleware(string $controller, string $method, string|array $middleware)
```

Verifies the _action_ for a given controller uses the given middleware or set of middleware.


## Matchers
```php
Expand Down
14 changes: 14 additions & 0 deletions src/Traits/HttpTestAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ public function assertActionUsesFormRequest(string $controller, string $method,

Assert::assertTrue($actual, 'Action "' . $method . '" does not have validation using the "' . $form_request . '" Form Request.');
}

public function assertActionUsesMiddleware($controller, $method, $middleware)
{
$router = resolve(\Illuminate\Routing\Router::class);
$route = $router->getRoutes()->getByAction($controller . '@' . $method);

Assert::assertNotNull($route, 'Unable to find route for controller action (' . $controller . '@' . $method . ')');

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