RoutePatternMatcherTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Scribe\Tests\BaseUnitTest;
  5. use Knuckles\Scribe\Tools\RoutePatternMatcher;
  6. class RoutePatternMatcherTest extends BaseUnitTest
  7. {
  8. /** @test */
  9. public function matches_by_route_name()
  10. {
  11. $route = new Route(["POST"], "/abc", ['as' => 'users.show']);
  12. $this->assertTrue(RoutePatternMatcher::matches($route, ['users.show']));
  13. $this->assertTrue(RoutePatternMatcher::matches($route, ['users.*']));
  14. $this->assertFalse(RoutePatternMatcher::matches($route, ['users.index']));
  15. }
  16. /** @test */
  17. public function matches_by_route_method_and_path()
  18. {
  19. $route = new Route(["POST"], "/abc", ['as' => 'users.show']);
  20. $this->assertTrue(RoutePatternMatcher::matches($route, ["POST /abc"]));
  21. $this->assertTrue(RoutePatternMatcher::matches($route, ["POST abc"]));
  22. $this->assertTrue(RoutePatternMatcher::matches($route, ["POST ab*"]));
  23. $this->assertTrue(RoutePatternMatcher::matches($route, ["POST /ab*"]));
  24. $this->assertTrue(RoutePatternMatcher::matches($route, ["POST *"]));
  25. $this->assertTrue(RoutePatternMatcher::matches($route, ["* abc"]));
  26. $this->assertTrue(RoutePatternMatcher::matches($route, ["* /abc"]));
  27. $this->assertTrue(RoutePatternMatcher::matches($route, ["* *"]));
  28. $this->assertFalse(RoutePatternMatcher::matches($route, ["GET /abc"]));
  29. $this->assertFalse(RoutePatternMatcher::matches($route, ["GET abc"]));
  30. }
  31. /** @test */
  32. public function matches_by_route_path()
  33. {
  34. $route = new Route(["POST"], "/abc", ['as' => 'users.show']);
  35. $this->assertTrue(RoutePatternMatcher::matches($route, ["/abc"]));
  36. $this->assertTrue(RoutePatternMatcher::matches($route, ["abc"]));
  37. $this->assertTrue(RoutePatternMatcher::matches($route, ["ab*"]));
  38. $this->assertTrue(RoutePatternMatcher::matches($route, ["/ab*"]));
  39. $this->assertTrue(RoutePatternMatcher::matches($route, ["*"]));
  40. $this->assertFalse(RoutePatternMatcher::matches($route, ["/d*"]));
  41. $this->assertFalse(RoutePatternMatcher::matches($route, ["d*"]));
  42. }
  43. /** @test */
  44. public function matches_route_with_multiple_methods()
  45. {
  46. $route = new Route(["GET", "HEAD"], "/abc", ['as' => 'users.show']);
  47. $this->assertTrue(RoutePatternMatcher::matches($route, ["HEAD /abc"]));
  48. $this->assertTrue(RoutePatternMatcher::matches($route, ["GET abc"]));
  49. $this->assertFalse(RoutePatternMatcher::matches($route, ["POST abc"]));
  50. }
  51. }