Skip to content

Commit

Permalink
Merge pull request #167 from elecena/fix/Router-rtrim
Browse files Browse the repository at this point in the history
Router - fix passing null to parameter #1 ($string) of type string is deprecated
  • Loading branch information
macbre authored Nov 3, 2022
2 parents 17fe848 + efdf94e commit b010c28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
5 changes: 3 additions & 2 deletions classes/Router.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(\NanoApp $app)
*
* Trim separator and extra chars given
*/
private function normalize($url, $extraChars = '')
private function normalize(string $url, string $extraChars = ''): string
{
$url = rtrim($url, self::SEPARATOR . $extraChars);
$url = ltrim($url, self::SEPARATOR);
Expand Down Expand Up @@ -267,7 +267,8 @@ public function getLastRoute()
public function getPathPrefix()
{
// parse homepage's URL
$pathPrefix = self::SEPARATOR . $this->normalize(parse_url($this->homeUrl, PHP_URL_PATH));
$homeUrlPath = parse_url($this->homeUrl, PHP_URL_PATH) ?? '';
$pathPrefix = self::SEPARATOR . $this->normalize($homeUrlPath);

if (strlen($pathPrefix) > 1) {
$pathPrefix .= self::SEPARATOR;
Expand Down
43 changes: 36 additions & 7 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,53 @@
* Set of unit tests for Router class
*/

use Nano\NanoBaseTest;
use Nano\Router;

class RouterTest extends \Nano\NanoBaseTest
class RouterTest extends NanoBaseTest
{
private function getRouter()
/**
* @param array $customConfigSettings custom Config settings to apply to NanoApp
* @return Router
*/
private function getRouter(array $customConfigSettings = []): Router
{
// use test application's directory
$dir = realpath(__DIR__ . '/app');
$this->app = Nano::app($dir);
$this->app = Nano::app(__DIR__ . '/app');

foreach ($customConfigSettings as $key => $value) {
$this->app->getConfig()->set($key, $value);
}

return new Router($this->app);
}

public function testGetPathPrefix()
/**
* @dataProvider getPathPrefixDataProvider
*/
public function testGetPathPrefix(string $homeUrl, string $expected)
{
$router = $this->getRouter();
$router = $this->getRouter(['home' => $homeUrl]);
$this->assertEquals($expected, $router->getPathPrefix());
}

$this->assertEquals('/site/', $router->getPathPrefix());
public function getPathPrefixDataProvider(): Generator
{
// parse_url( PHP_URL_PATH ) gives null in this case
yield 'Home page at root' => [
'homeUrl' => 'https://foo.bar',
'expected' => '/',
];

yield 'Home page at /' => [
'homeUrl' => 'https://foo.bar/',
'expected' => '/',
];

yield 'Home page at /site' => [
'homeUrl' => 'https://foo.bar/site',
'expected' => '/site/',
];
}

public function testSanitize()
Expand Down

0 comments on commit b010c28

Please sign in to comment.