diff --git a/phpunit.xml b/phpunit.xml index 462cb722..4b5d5439 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -26,35 +26,12 @@ - - tests/GenerateDocumentation/OutputTest.php - tests/GenerateDocumentation/BehavioursTest.php - - + + tests/GenerateDocumentation/OutputTest.php tests/Strategies - - tests/Unit/RouteMatcherDingoTest.php - tests/Unit/RouteMatcherTest.php - - - tests/Unit/ExtractorTest.php - tests/Unit/ExtractorPluginSystemTest.php - tests/Unit/ConfigDifferTest.php - tests/Unit/PathConfigurationTest.php - - - tests/Unit/ExtractedEndpointDataTest.php - tests/Unit/AnnotationParserTest.php - - - tests/Unit/OpenAPISpecWriterTest.php - tests/Unit/PostmanCollectionWriterTest.php - - - tests/Unit/ValidationRuleParsingTest.php - tests/Unit/HtmlWriterTest.php - tests/Unit/WritingUtilsTest.php + + tests/Unit diff --git a/src/Matching/RouteMatcher.php b/src/Matching/RouteMatcher.php index 24aadb3e..d6d0f6a6 100644 --- a/src/Matching/RouteMatcher.php +++ b/src/Matching/RouteMatcher.php @@ -6,6 +6,7 @@ use Illuminate\Routing\Route; use Illuminate\Support\Facades\Route as RouteFacade; use Illuminate\Support\Str; +use Knuckles\Scribe\Tools\RoutePatternMatcher; class RouteMatcher implements RouteMatcherInterface { @@ -61,7 +62,7 @@ private function getAllRoutes(bool $usingDingoRouter) private function shouldIncludeRoute(Route $route, array $routeRule, array $mustIncludes, bool $usingDingoRouter): bool { - if (Str::is($mustIncludes, $route->getName()) || Str::is($mustIncludes, $route->uri())) { + if (RoutePatternMatcher::matches($route, $mustIncludes)) { return true; } @@ -90,7 +91,6 @@ private function shouldExcludeRoute(Route $route, array $routeRule): bool $excludes[] = 'telescope/*'; } - return Str::is($excludes, $route->getName()) - || Str::is($excludes, $route->uri()); + return RoutePatternMatcher::matches($route, $excludes); } } diff --git a/src/Tools/RoutePatternMatcher.php b/src/Tools/RoutePatternMatcher.php new file mode 100644 index 00000000..9d8c8e0c --- /dev/null +++ b/src/Tools/RoutePatternMatcher.php @@ -0,0 +1,31 @@ +getName(); + $routePathWithoutInitialSlash = $route->uri(); + $routePathWithInitialSlash = "/$routePathWithoutInitialSlash"; + $routeMethods = $route->methods(); + if (Str::is($patterns, $routeName) + || Str::is($patterns, $routePathWithoutInitialSlash) + || Str::is($patterns, $routePathWithInitialSlash)) { + return true; + } + + foreach ($routeMethods as $httpMethod) { + if (Str::is($patterns, "$httpMethod $routePathWithoutInitialSlash") + || Str::is($patterns, "$httpMethod $routePathWithInitialSlash")) { + return true; + } + } + + return false; + } +} diff --git a/tests/Unit/RoutePatternMatcherTest.php b/tests/Unit/RoutePatternMatcherTest.php new file mode 100644 index 00000000..db9000be --- /dev/null +++ b/tests/Unit/RoutePatternMatcherTest.php @@ -0,0 +1,47 @@ + 'users.show']); + $this->assertTrue(RoutePatternMatcher::matches($route, ['users.show'])); + $this->assertTrue(RoutePatternMatcher::matches($route, ['users.*'])); + $this->assertFalse(RoutePatternMatcher::matches($route, ['users.index'])); + } + + /** @test */ + public function matches_by_route_method_and_path() + { + $route = new Route(["POST"], "/abc", ['as' => 'users.show']); + $this->assertTrue(RoutePatternMatcher::matches($route, ["POST /abc"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["POST abc"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["POST ab*"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["POST /ab*"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["POST *"])); + + $this->assertFalse(RoutePatternMatcher::matches($route, ["GET /abc"])); + $this->assertFalse(RoutePatternMatcher::matches($route, ["GET abc"])); + } + + /** @test */ + public function matches_by_route_path() + { + $route = new Route(["POST"], "/abc", ['as' => 'users.show']); + $this->assertTrue(RoutePatternMatcher::matches($route, ["/abc"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["abc"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["ab*"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["/ab*"])); + $this->assertTrue(RoutePatternMatcher::matches($route, ["*"])); + + $this->assertFalse(RoutePatternMatcher::matches($route, ["/d*"])); + } + +}