diff --git a/src/UrlHelper.php b/src/UrlHelper.php index bf7c191..8c64f02 100644 --- a/src/UrlHelper.php +++ b/src/UrlHelper.php @@ -59,19 +59,17 @@ public function __invoke($route = null, array $params = []) } if ($route === null) { - return $this->generateUriFromResult($params, $result); - } - - if ($this->result) { - $params = $this->mergeParams($route, $result, $params); + $url = $this->generateUriFromResult($params, $result); + } else { + if ($result) { + $params = $this->mergeParams($route, $result, $params); + } + $url = $this->router->generateUri($route, $params); } $basePath = $this->getBasePath(); - if ($basePath === '/') { - return $this->router->generateUri($route, $params); - } - return $basePath . $this->router->generateUri($route, $params); + return ($basePath === '/') ? $url : $basePath . $url; } /** diff --git a/test/UrlHelperTest.php b/test/UrlHelperTest.php index b427b6b..e38c132 100644 --- a/test/UrlHelperTest.php +++ b/test/UrlHelperTest.php @@ -165,4 +165,24 @@ public function testBasePathIsPrependedToGeneratedPath() $helper->setBasePath('/prefix'); $this->assertEquals('/prefix/foo/baz', $helper('foo', ['bar' => 'baz'])); } + + public function testBasePathIsPrependedToGeneratedPathWhenUsingRouteResult() + { + $result = $this->prophesize(RouteResult::class); + $result->isFailure()->willReturn(false); + $result->getMatchedRouteName()->willReturn('foo'); + $result->getMatchedParams()->willReturn(['bar' => 'baz']); + + $this->router->generateUri('foo', ['bar' => 'baz'])->willReturn('/foo/baz'); + + $helper = $this->createHelper(); + $helper->setBasePath('/prefix'); + $helper->setRouteResult($result->reveal()); + + // test with explicit params + $this->assertEquals('/prefix/foo/baz', $helper(null, ['bar' => 'baz'])); + + // test with implicit route result params + $this->assertEquals('/prefix/foo/baz', $helper()); + } }