diff --git a/tests/Integration/Routing/CompiledRouteCollectionTest.php b/tests/Integration/Routing/CompiledRouteCollectionTest.php index 35d12753b5ca..993e23d7518b 100644 --- a/tests/Integration/Routing/CompiledRouteCollectionTest.php +++ b/tests/Integration/Routing/CompiledRouteCollectionTest.php @@ -571,6 +571,39 @@ public function testRouteWithSamePathAndSameMethodButDiffDomainNameWithOptionsMe ], $this->collection()->getRoutesByMethod()); } + public function testActionUrlGenerationChoosesFirstMatchingRouteWithCompiledRoutes() + { + $this->routeCollection->add($this->newRoute('GET', '{resource}/attachable/{field}', [ + 'controller' => 'MyController', + ])); + + $this->routeCollection->add($this->newRoute('GET', '{resource}/{resourceId}/attachable/{field}', [ + 'controller' => 'MyController', + ])); + + $routes = $this->collection(); + + $url = app('url'); + $url->setRoutes($routes); + + $result = $url->action('MyController', [ + 'resource' => 'posts', + 'resourceId' => 1, + 'field' => 'category', + 'foo' => 'bar', + ]); + + $this->assertStringEndsWith('/posts/attachable/category?resourceId=1&foo=bar', $result); + + $result = $url->action('MyController', [ + 'resource' => 'posts', + 'field' => 'category', + 'foo' => 'bar', + ]); + + $this->assertStringEndsWith('/posts/attachable/category?foo=bar', $result); + } + /** * Create a new Route object. * diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 54eab01a1cc8..a13da155b7e9 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -2000,6 +2000,42 @@ public function testUrlGenerationWithOptionalParameters(): void $url->route('tenantPostUserOptionalMethod', ['concreteTenant', 'concretePost', 'concreteUser', 'concreteMethod']), ); } + + public function testActionUrlGenerationChoosesFirstMatchingRoute() + { + $routes = new RouteCollection; + $request = Request::create('http://www.foo.com/'); + $url = new UrlGenerator($routes, $request); + + $route1 = new Route(['GET'], '{resource}/attachable/{field}', [ + 'controller' => 'MyController', + ]); + $routes->add($route1); + + $route2 = new Route(['GET'], '{resource}/{resourceId}/attachable/{field}', [ + 'controller' => 'MyController', + ]); + $routes->add($route2); + + $routes->refreshActionLookups(); + + $result = $url->action('MyController', [ + 'resource' => 'posts', + 'resourceId' => 1, + 'field' => 'category', + 'foo' => 'bar', + ]); + + $this->assertSame('http://www.foo.com/posts/attachable/category?resourceId=1&foo=bar', $result); + + $result = $url->action('MyController', [ + 'resource' => 'posts', + 'field' => 'category', + 'foo' => 'bar', + ]); + + $this->assertSame('http://www.foo.com/posts/attachable/category?foo=bar', $result); + } } class RoutableInterfaceStub implements UrlRoutable