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

Support generating default route w/o parameters #23

Merged
merged 1 commit into from
Oct 3, 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
5 changes: 3 additions & 2 deletions src/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public function __construct(RouterInterface $router)
*
* @param string $routeName
* @param array $params
* @param bool $reuseResultParams
* @return string
* @throws Exception\RuntimeException if no route provided, and no result match
* present.
* @throws Exception\RuntimeException if no route provided, and result match is a
* routing failure.
* @throws RouterException if router cannot generate URI for given route.
*/
public function __invoke($routeName = null, array $params = [])
public function __invoke($routeName = null, array $params = [], $reuseResultParams = true)
{
$result = $this->getRouteResult();
if ($routeName === null && $result === null) {
Expand All @@ -67,7 +68,7 @@ public function __invoke($routeName = null, array $params = [])
return $basePath . $this->generateUriFromResult($params, $result);
}

if ($result) {
if ($result && $reuseResultParams) {
$params = $this->mergeParams($routeName, $result, $params);
}

Expand Down
15 changes: 15 additions & 0 deletions test/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public function testProvidedParametersOverrideAnyPresentInARouteResultWhenGenera
$this->assertEquals('URL', $helper('resource', ['id' => 2]));
}

public function testWillNotReuseRouteResultParamsIfReuseResultParamsFlagIsFalseWhenGeneratingUri()
{
$result = $this->prophesize(RouteResult::class);
$result->isFailure()->willReturn(false);
$result->getMatchedRouteName()->willReturn('resource');
$result->getMatchedParams()->willReturn(['id' => 1]);

$this->router->generateUri('resource', [])->willReturn('URL');

$helper = $this->createHelper();
$helper->setRouteResult($result->reveal());

$this->assertEquals('URL', $helper('resource', [], false));
}

public function testCanInjectRouteResult()
{
$result = $this->prophesize(RouteResult::class);
Expand Down