Skip to content

Instantly share code, notes, and snippets.

@innocenzi
Created September 20, 2022 15:35
Show Gist options
  • Save innocenzi/20d8153a2490fcd34f4fa80e9cb95c62 to your computer and use it in GitHub Desktop.
Save innocenzi/20d8153a2490fcd34f4fa80e9cb95c62 to your computer and use it in GitHub Desktop.
No closure route assertion
<?php
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
test('basic feature test', function () {
$routes = collect(app(Router::class)->getRoutes()->getRoutes())
->filter(fn (Route $route) => ! isVendorRoute($route));
foreach ($routes as $route) {
this()->assertNotNull(data_get($route->action, 'controller'), $route->uri);
}
});
function isVendorRoute(Route $route)
{
if ($route->action['uses'] instanceof Closure) {
$path = (new ReflectionFunction($route->action['uses']))
->getFileName();
} elseif (is_string($route->action['uses']) &&
str_contains($route->action['uses'], 'SerializableClosure')) {
return false;
} elseif (is_string($route->action['uses'])) {
if (in_array($route->getControllerClass(), [
'\Illuminate\Routing\RedirectController',
'\Illuminate\Routing\ViewController',
], true)) {
return false;
}
$path = (new ReflectionClass($route->getControllerClass()))
->getFileName();
} else {
return false;
}
return str_starts_with($path, base_path('vendor'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment