From d617622a92e09af01887b12223cc3f49de759e22 Mon Sep 17 00:00:00 2001 From: Mathias Grimm Date: Fri, 10 Oct 2025 20:36:57 -0300 Subject: [PATCH 1/2] Adding tests ensuring action() returns first route Fixes #57312 --- .../Routing/CompiledRouteCollectionTest.php | 33 +++++++++++++++++ tests/Routing/RoutingUrlGeneratorTest.php | 36 +++++++++++++++++++ 2 files changed, 69 insertions(+) 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..3b21bff99f93 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 From fcc925c9e7b78188f933c70c3dc5b16c14d4928c Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 10 Oct 2025 23:37:15 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- tests/Routing/RoutingUrlGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index 3b21bff99f93..a13da155b7e9 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -2026,7 +2026,7 @@ public function testActionUrlGenerationChoosesFirstMatchingRoute() 'foo' => 'bar', ]); - $this->assertSame('http://www.foo.com/posts/attachable/category?resourceId=1&foo=bar', $result,); + $this->assertSame('http://www.foo.com/posts/attachable/category?resourceId=1&foo=bar', $result); $result = $url->action('MyController', [ 'resource' => 'posts',