Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

prepend basePath when autogenerating from route result #15

Merged
merged 2 commits into from
Aug 20, 2016
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
16 changes: 7 additions & 9 deletions src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}