Skip to content

Commit

Permalink
Support matching routes on method + path
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 24, 2023
1 parent 7f58c48 commit 57b55ec
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
31 changes: 4 additions & 27 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,12 @@
<server name="SCRIBE_TESTS" value="1" />
</php>
<testsuites>
<testsuite name="Full Test">
<file>tests/GenerateDocumentation/OutputTest.php</file>
<file>tests/GenerateDocumentation/BehavioursTest.php</file>
</testsuite>
<testsuite name="Strategies">
<testsuite name="Non-Unit Tests">
<directory>tests/GenerateDocumentation/OutputTest.php</directory>
<directory>tests/Strategies</directory>
</testsuite>
<testsuite name="RouteMatcher Tests">
<file>tests/Unit/RouteMatcherDingoTest.php</file>
<file>tests/Unit/RouteMatcherTest.php</file>
</testsuite>
<testsuite name="Unit Tests 1">
<file>tests/Unit/ExtractorTest.php</file>
<file>tests/Unit/ExtractorPluginSystemTest.php</file>
<file>tests/Unit/ConfigDifferTest.php</file>
<file>tests/Unit/PathConfigurationTest.php</file>
</testsuite>
<testsuite name="Unit Tests 2">
<file>tests/Unit/ExtractedEndpointDataTest.php</file>
<file>tests/Unit/AnnotationParserTest.php</file>
</testsuite>
<testsuite name="Unit Tests 3">
<file>tests/Unit/OpenAPISpecWriterTest.php</file>
<file>tests/Unit/PostmanCollectionWriterTest.php</file>
</testsuite>
<testsuite name="Unit Tests 4">
<file>tests/Unit/ValidationRuleParsingTest.php</file>
<file>tests/Unit/HtmlWriterTest.php</file>
<file>tests/Unit/WritingUtilsTest.php</file>
<testsuite name="Unit Tests">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 3 additions & 3 deletions src/Matching/RouteMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
31 changes: 31 additions & 0 deletions src/Tools/RoutePatternMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Knuckles\Scribe\Tools;

use Illuminate\Routing\Route;
use Illuminate\Support\Str;

class RoutePatternMatcher
{
public static function matches(Route $route, $patterns): bool
{
$routeName = $route->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;
}
}
47 changes: 47 additions & 0 deletions tests/Unit/RoutePatternMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Knuckles\Scribe\Tests\Unit;

use Illuminate\Routing\Route;
use Knuckles\Scribe\Tools\RoutePatternMatcher;
use PHPUnit\Framework\TestCase;

class RoutePatternMatcherTest extends TestCase
{
/** @test */
public function matches_by_route_name()
{
$route = new Route(["POST"], "/abc", ['as' => '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*"]));
}

}

0 comments on commit 57b55ec

Please sign in to comment.