Skip to content

Commit

Permalink
refactoring of NormalizeUrlTrailingSlashSubscriber (#2153)
Browse files Browse the repository at this point in the history
  • Loading branch information
s3tezsky committed Dec 17, 2020
2 parents 60a8647 + 28474cc commit 0c661fb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Expand Up @@ -44,12 +44,7 @@ public function onKernelException(ExceptionEvent $event)
{
if ($event->getThrowable() instanceof NotFoundHttpException) {
$pathInfo = $event->getRequest()->getPathInfo();

if (substr($pathInfo, -1) === '/') {
$pathInfo = rtrim($pathInfo, '/');
} else {
$pathInfo .= '/';
}
$pathInfo = TransformString::addOrRemoveTrailingSlashFromString($pathInfo);

$this->redirectToExistingPath($pathInfo, $event);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/framework/src/Component/String/TransformString.php
Expand Up @@ -57,12 +57,9 @@ public static function stringToFriendlyUrlSlug($string)
public static function addOrRemoveTrailingSlashFromString(string $string): string
{
if (substr($string, -1) === '/') {
$string = rtrim($string, '/');
} else {
$string .= '/';
return rtrim($string, '/');
}

return $string;
return $string . '/';
}

/**
Expand Down
Expand Up @@ -150,4 +150,47 @@ public function testStringToCamelCase($actual, $expected)
{
$this->assertSame($expected, TransformString::stringToCamelCase($actual));
}

/**
* @return array
*/
public function stringTrailingSlashesProvider(): array
{
return [
[
'foo',
'foo/',
],
[
'foo/bar',
'foo/bar/',
],
[
'foo/',
'foo',
],
[
'foo/bar/',
'foo/bar',
],
[
'',
'/',
],
[
'/',
'',
],
];
}

/**
* @dataProvider stringTrailingSlashesProvider
* @param string $string
* @param string $expected
*/
public function testAddOrRemoveTrailingSlashFromString(string $string, string $expected): void
{
static::assertSame($expected, TransformString::addOrRemoveTrailingSlashFromString($string));
}
}

0 comments on commit 0c661fb

Please sign in to comment.