-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support matching routes on method + path
- Loading branch information
Showing
4 changed files
with
85 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*"])); | ||
} | ||
|
||
} |