From 62c51fd910ce56e32e97fc09f7dbad6c2a564a6f Mon Sep 17 00:00:00 2001 From: Michael Moussa Date: Wed, 14 Sep 2016 08:59:42 -0400 Subject: [PATCH] Support generating default route w/o parameters Given a matched route with name `album.index` and path `/album[/page/{page:\d+}]`, invoking the `UrlHelper` with the route name `album.index` and no additional parameters while on page 5 would return `/album/page/5`, since it automatically merges existing params from the `RouteResult`. This behavior would make it impossible to use the `UrlHelper` to generate the base `/album` URL while on any page. Invoking the helper with the new third argument `$reuseResultParams` set to `false` will now cause the helper to only take the params provided in the 2nd `$params` argument into consideration, which will produce the base `/album` URL in the above example. BC Breaks: - None if `UrlHelper` is being consumed directly. - Method signature change if `UrlHelper` has been extended. --- src/UrlHelper.php | 5 +++-- test/UrlHelperTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/UrlHelper.php b/src/UrlHelper.php index 3985130..c4e9938 100644 --- a/src/UrlHelper.php +++ b/src/UrlHelper.php @@ -42,6 +42,7 @@ 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. @@ -49,7 +50,7 @@ public function __construct(RouterInterface $router) * 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) { @@ -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); } diff --git a/test/UrlHelperTest.php b/test/UrlHelperTest.php index e38c132..6a38d3b 100644 --- a/test/UrlHelperTest.php +++ b/test/UrlHelperTest.php @@ -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);