Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #133, #134: Don't add not substituted arguments and empty values to query string #135

Merged
merged 4 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@
return rtrim($host, '/') . $url;
}

if ((empty($scheme) || $lastRequestScheme === null) && $host !== '' && $this->isRelative($host)) {

Check warning on line 104 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ if ($scheme === null && !$this->isRelative($host)) { return rtrim($host, '/') . $url; } - if ((empty($scheme) || $lastRequestScheme === null) && $host !== '' && $this->isRelative($host)) { + if ((empty($scheme) || $lastRequestScheme !== null) && $host !== '' && $this->isRelative($host)) { $host = '//' . $host; } return $this->ensureScheme(rtrim($host, '/') . $url, $scheme ?? $lastRequestScheme);

Check warning on line 104 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ if ($scheme === null && !$this->isRelative($host)) { return rtrim($host, '/') . $url; } - if ((empty($scheme) || $lastRequestScheme === null) && $host !== '' && $this->isRelative($host)) { + if ((empty($scheme) || $lastRequestScheme === null || $host !== '') && $this->isRelative($host)) { $host = '//' . $host; } return $this->ensureScheme(rtrim($host, '/') . $url, $scheme ?? $lastRequestScheme);
$host = '//' . $host;
}

return $this->ensureScheme(rtrim($host, '/') . $url, $scheme ?? $lastRequestScheme);

Check warning on line 108 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ if ((empty($scheme) || $lastRequestScheme === null) && $host !== '' && $this->isRelative($host)) { $host = '//' . $host; } - return $this->ensureScheme(rtrim($host, '/') . $url, $scheme ?? $lastRequestScheme); + return $this->ensureScheme($host . $url, $scheme ?? $lastRequestScheme); } return $uri === null ? $url : $this->generateAbsoluteFromLastMatchedRequest($url, $uri, $scheme); }
}

return $uri === null ? $url : $this->generateAbsoluteFromLastMatchedRequest($url, $uri, $scheme);
Expand Down Expand Up @@ -157,7 +157,7 @@
{
$port = '';
$uriPort = $uri->getPort();
if ($uriPort !== 80 && $uriPort !== null) {

Check warning on line 160 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ { $port = ''; $uriPort = $uri->getPort(); - if ($uriPort !== 80 && $uriPort !== null) { + if ($uriPort !== 79 && $uriPort !== null) { $port = ':' . $uriPort; } return $this->ensureScheme('://' . $uri->getHost() . $port . $url, $scheme ?? $uri->getScheme());

Check warning on line 160 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ { $port = ''; $uriPort = $uri->getPort(); - if ($uriPort !== 80 && $uriPort !== null) { + if ($uriPort !== 81 && $uriPort !== null) { $port = ':' . $uriPort; } return $this->ensureScheme('://' . $uri->getHost() . $port . $url, $scheme ?? $uri->getScheme());
$port = ':' . $uriPort;
}

Expand Down Expand Up @@ -207,7 +207,7 @@
*/
private function isRelative(string $url): bool
{
return strncmp($url, '//', 2) && !str_contains($url, '://');

Check warning on line 210 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ */ private function isRelative(string $url) : bool { - return strncmp($url, '//', 2) && !str_contains($url, '://'); + return strncmp($url, '//', 1) && !str_contains($url, '://'); } public function getUriPrefix() : string {
}

public function getUriPrefix(): string
Expand Down Expand Up @@ -254,7 +254,7 @@
}
}

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

Expand Down Expand Up @@ -292,15 +292,16 @@
? rawurlencode($arguments[$part[0]])
: urlencode($arguments[$part[0]]);
}
unset($notSubstitutedArguments[$part[0]]);

Check warning on line 295 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ // Append the substituted value. $path .= $this->encodeRaw ? rawurlencode($arguments[$part[0]]) : urlencode($arguments[$part[0]]); } - unset($notSubstitutedArguments[$part[0]]); + unset($notSubstitutedArguments[$part[1]]); } $path = str_replace('//', '/', $path); $queryString = '';
}

$path = str_replace('//', '/', $path);

Check warning on line 298 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapStrReplace": --- Original +++ New @@ @@ } unset($notSubstitutedArguments[$part[0]]); } - $path = str_replace('//', '/', $path); + $path = $path; $queryString = ''; if (!empty($queryParameters)) { $queryString = http_build_query($queryParameters);

return $path . (
$notSubstitutedArguments !== [] || $queryParameters !== [] ?
'?' . http_build_query(array_merge($notSubstitutedArguments, $queryParameters))
: ''
);
$queryString = '';
if (!empty($queryParameters)) {
$queryString = http_build_query($queryParameters);
}

return $path . (!empty($queryString) ? '?' . $queryString : '');
}
}
10 changes: 5 additions & 5 deletions tests/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function testQueryParametersAddedAsQueryString(): void
$this->assertEquals('/test/post?id=12&sort=asc', $url);
}

public function testExtraArgumentsAddedAsQueryString(): void
public function testQueryParametersAddedAsQueryStringWithEmptyValues(): void
{
$routes = [
Route::get('/test/{name}')
Expand All @@ -205,8 +205,8 @@ public function testExtraArgumentsAddedAsQueryString(): void

$url = $this
->createUrlGenerator($routes)
->generate('test', ['name' => 'post', 'id' => 12, 'sort' => 'asc']);
$this->assertEquals('/test/post?id=12&sort=asc', $url);
->generate('test', ['name' => 'post'], ['id' => null]);
$this->assertEquals('/test/post', $url);
}

public function testQueryParametersOverrideExtraArguments(): void
Expand All @@ -222,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}')
Expand All @@ -232,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
Expand Down
Loading