From e2e168d082b85256157b7da7324f0a188acf035f Mon Sep 17 00:00:00 2001 From: Rustam Date: Sat, 23 Dec 2023 22:23:37 +0500 Subject: [PATCH 1/4] Fix #134: Don't add empty query string --- src/UrlGenerator.php | 13 +++++++------ tests/UrlGeneratorTest.php | 15 ++++++++++++++- tests/UrlMatcherTest.php | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php index bb3a8aa..ef5674c 100644 --- a/src/UrlGenerator.php +++ b/src/UrlGenerator.php @@ -254,7 +254,7 @@ private function missingArguments(array $parts, array $substitutions): array } } - // All required arguments are availgit logable. + // All required arguments are available. return []; } @@ -297,10 +297,11 @@ private function generatePath(array $arguments, array $queryParameters, array $p $path = str_replace('//', '/', $path); - return $path . ( - $notSubstitutedArguments !== [] || $queryParameters !== [] ? - '?' . http_build_query(array_merge($notSubstitutedArguments, $queryParameters)) - : '' - ); + $queryString = ''; + if (!empty($notSubstitutedArguments) || !empty($queryParameters)) { + $queryString = http_build_query(array_merge($notSubstitutedArguments, $queryParameters)); + } + + return $path . (!empty($queryString) ? '?' . $queryString : ''); } } diff --git a/tests/UrlGeneratorTest.php b/tests/UrlGeneratorTest.php index 4c31566..02c457e 100644 --- a/tests/UrlGeneratorTest.php +++ b/tests/UrlGeneratorTest.php @@ -196,6 +196,19 @@ public function testQueryParametersAddedAsQueryString(): void $this->assertEquals('/test/post?id=12&sort=asc', $url); } + public function testQueryParametersAddedAsQueryStringWithEmptyValues(): void + { + $routes = [ + Route::get('/test/{name}') + ->name('test'), + ]; + + $url = $this + ->createUrlGenerator($routes) + ->generate('test', ['name' => 'post'], ['id' => null]); + $this->assertEquals('/test/post', $url); + } + public function testExtraArgumentsAddedAsQueryString(): void { $routes = [ @@ -866,7 +879,7 @@ private function createRouteCollection(array $routes): RouteCollectionInterface { $rootGroup = Group::create()->routes(...$routes); $collector = new RouteCollector(); - $collector->addGroup($rootGroup); + $collector->addRoute($rootGroup); return new RouteCollection($collector); } } diff --git a/tests/UrlMatcherTest.php b/tests/UrlMatcherTest.php index ed67a97..c3367e7 100644 --- a/tests/UrlMatcherTest.php +++ b/tests/UrlMatcherTest.php @@ -491,7 +491,7 @@ private function createUrlMatcher(array $routes, CacheInterface $cache = null): { $rootGroup = Group::create()->routes(...$routes); $collector = new RouteCollector(); - $collector->addGroup($rootGroup); + $collector->addRoute($rootGroup); return new UrlMatcher(new RouteCollection($collector), $cache, ['cache_key' => 'route-cache']); } } From aba44b773c8705c99b46534ce71ad1f348a35ac6 Mon Sep 17 00:00:00 2001 From: Rustam Date: Sat, 23 Dec 2023 22:30:39 +0500 Subject: [PATCH 2/4] Fix #133: Don't add not substituted arguments as query string --- src/UrlGenerator.php | 4 ++-- tests/UrlGeneratorTest.php | 17 ++--------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php index ef5674c..2226405 100644 --- a/src/UrlGenerator.php +++ b/src/UrlGenerator.php @@ -298,8 +298,8 @@ private function generatePath(array $arguments, array $queryParameters, array $p $path = str_replace('//', '/', $path); $queryString = ''; - if (!empty($notSubstitutedArguments) || !empty($queryParameters)) { - $queryString = http_build_query(array_merge($notSubstitutedArguments, $queryParameters)); + if (!empty($queryParameters)) { + $queryString = http_build_query($queryParameters); } return $path . (!empty($queryString) ? '?' . $queryString : ''); diff --git a/tests/UrlGeneratorTest.php b/tests/UrlGeneratorTest.php index 02c457e..a442687 100644 --- a/tests/UrlGeneratorTest.php +++ b/tests/UrlGeneratorTest.php @@ -209,19 +209,6 @@ public function testQueryParametersAddedAsQueryStringWithEmptyValues(): void $this->assertEquals('/test/post', $url); } - public function testExtraArgumentsAddedAsQueryString(): void - { - $routes = [ - Route::get('/test/{name}') - ->name('test'), - ]; - - $url = $this - ->createUrlGenerator($routes) - ->generate('test', ['name' => 'post', 'id' => 12, 'sort' => 'asc']); - $this->assertEquals('/test/post?id=12&sort=asc', $url); - } - public function testQueryParametersOverrideExtraArguments(): void { $routes = [ @@ -235,7 +222,7 @@ public function testQueryParametersOverrideExtraArguments(): void $this->assertEquals('/test/post?id=12&sort=asc', $url); } - public function testQueryParametersMergedWithExtraArguments(): void + public function testNotSubstitutedArgumentsRemove(): void { $routes = [ Route::get('/test/{name}') @@ -245,7 +232,7 @@ public function testQueryParametersMergedWithExtraArguments(): void $url = $this ->createUrlGenerator($routes) ->generate('test', ['name' => 'post', 'id' => 11], ['sort' => 'asc']); - $this->assertEquals('/test/post?id=11&sort=asc', $url); + $this->assertEquals('/test/post?sort=asc', $url); } public function testDefaultNotUsedForOptionalArgument(): void From 65ef29b374890beca4c9adc7b7891df96c30b7ec Mon Sep 17 00:00:00 2001 From: Rustam Date: Sat, 23 Dec 2023 22:36:50 +0500 Subject: [PATCH 3/4] Fix tests --- tests/UrlGeneratorTest.php | 2 +- tests/UrlMatcherTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/UrlGeneratorTest.php b/tests/UrlGeneratorTest.php index a442687..ab3bb2a 100644 --- a/tests/UrlGeneratorTest.php +++ b/tests/UrlGeneratorTest.php @@ -866,7 +866,7 @@ private function createRouteCollection(array $routes): RouteCollectionInterface { $rootGroup = Group::create()->routes(...$routes); $collector = new RouteCollector(); - $collector->addRoute($rootGroup); + $collector->addGroup($rootGroup); return new RouteCollection($collector); } } diff --git a/tests/UrlMatcherTest.php b/tests/UrlMatcherTest.php index c3367e7..ed67a97 100644 --- a/tests/UrlMatcherTest.php +++ b/tests/UrlMatcherTest.php @@ -491,7 +491,7 @@ private function createUrlMatcher(array $routes, CacheInterface $cache = null): { $rootGroup = Group::create()->routes(...$routes); $collector = new RouteCollector(); - $collector->addRoute($rootGroup); + $collector->addGroup($rootGroup); return new UrlMatcher(new RouteCollection($collector), $cache, ['cache_key' => 'route-cache']); } } From b2d6730b0ff02ae4a4317add284da55ada0233d1 Mon Sep 17 00:00:00 2001 From: Rustam Date: Sat, 23 Dec 2023 22:44:17 +0500 Subject: [PATCH 4/4] Add changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60534c9..8be2a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 3.0.1 under development -- no changes in this release. +- Bug #133: Don't add not substituted arguments to a query string (@rustamwin) +- Bug #134: Don't add query string if it's empty (@rustamwin) ## 3.0.0 February 17, 2023