Skip to content

Commit

Permalink
Fix #134: Don't add empty query string
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Dec 23, 2023
1 parent 286474f commit e2e168d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private function missingArguments(array $parts, array $substitutions): array
}
}

// All required arguments are availgit logable.
// All required arguments are available.
return [];
}

Expand Down Expand Up @@ -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 : '');
}
}
15 changes: 14 additions & 1 deletion tests/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/UrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit e2e168d

Please sign in to comment.