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

Commit 752eb5e

Browse files
committed
Merge branch 'hotfix/route-result-param-merging'
Close #6
2 parents 571ed61 + d08b810 commit 752eb5e

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- [4](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
9+
- [#4](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
1010
Allow rendering view models via render
1111

1212
### Deprecated
@@ -19,7 +19,8 @@ All notable changes to this project will be documented in this file, in reverse
1919

2020
### Fixed
2121

22-
- Nothing.
22+
- [#6](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/6)
23+
Merge route result params with those provided
2324

2425
## 0.2.0 - 2015-10-20
2526

src/UrlHelper.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function __invoke($route = null, $params = [])
5555
return $this->generateUriFromResult($params);
5656
}
5757

58+
if ($this->result) {
59+
$params = $this->mergeParams($route, $params);
60+
}
61+
5862
return $this->router->generateUri($route, $params);
5963
}
6064

@@ -84,4 +88,39 @@ private function generateUriFromResult(array $params)
8488
$params = array_merge($this->result->getMatchedParams(), $params);
8589
return $this->router->generateUri($name, $params);
8690
}
91+
92+
/**
93+
* Merge route result params and provided parameters.
94+
*
95+
* If $params is not an array, returns them verbatim.
96+
*
97+
* If the route result represents a routing failure, returns the params
98+
* verbatim.
99+
*
100+
* If the route result does not represent the same route name requested,
101+
* returns the params verbatim.
102+
*
103+
* Otherwise, merges the route result params with those provided at
104+
* invocation, with the latter having precedence.
105+
*
106+
* @param string $route Route name.
107+
* @param array $params Parameters provided at invocation.
108+
* @return array
109+
*/
110+
private function mergeParams($route, $params)
111+
{
112+
if (! is_array($params)) {
113+
return $params;
114+
}
115+
116+
if ($this->result->isFailure()) {
117+
return $params;
118+
}
119+
120+
if ($this->result->getMatchedRouteName() !== $route) {
121+
return $params;
122+
}
123+
124+
return array_merge($this->result->getMatchedParams(), $params);
125+
}
87126
}

test/UrlHelperTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,49 @@ public function testWhenRouteProvidedTheHelperDelegatesToTheRouterToGenerateUrl(
8989
$helper = $this->createHelper();
9090
$this->assertEquals('URL', $helper('foo', ['bar' => 'baz']));
9191
}
92+
93+
public function testIfRouteResultRouteNameDoesNotMatchRequestedNameItWillNotMergeParamsToGenerateUri()
94+
{
95+
$result = $this->prophesize(RouteResult::class);
96+
$result->isFailure()->willReturn(false);
97+
$result->getMatchedRouteName()->willReturn('not-resource');
98+
$result->getMatchedParams()->shouldNotBeCalled();
99+
100+
$this->router->generateUri('resource', [])->willReturn('URL');
101+
102+
$helper = $this->createHelper();
103+
$helper->setRouteResult($result->reveal());
104+
105+
$this->assertEquals('URL', $helper('resource'));
106+
}
107+
108+
public function testMergesRouteResultParamsWithProvidedParametersToGenerateUri()
109+
{
110+
$result = $this->prophesize(RouteResult::class);
111+
$result->isFailure()->willReturn(false);
112+
$result->getMatchedRouteName()->willReturn('resource');
113+
$result->getMatchedParams()->willReturn(['id' => 1]);
114+
115+
$this->router->generateUri('resource', ['id' => 1, 'version' => 2])->willReturn('URL');
116+
117+
$helper = $this->createHelper();
118+
$helper->setRouteResult($result->reveal());
119+
120+
$this->assertEquals('URL', $helper('resource', ['version' => 2]));
121+
}
122+
123+
public function testProvidedParametersOverrideAnyPresentInARouteResultWhenGeneratingUri()
124+
{
125+
$result = $this->prophesize(RouteResult::class);
126+
$result->isFailure()->willReturn(false);
127+
$result->getMatchedRouteName()->willReturn('resource');
128+
$result->getMatchedParams()->willReturn(['id' => 1]);
129+
130+
$this->router->generateUri('resource', ['id' => 2])->willReturn('URL');
131+
132+
$helper = $this->createHelper();
133+
$helper->setRouteResult($result->reveal());
134+
135+
$this->assertEquals('URL', $helper('resource', ['id' => 2]));
136+
}
92137
}

0 commit comments

Comments
 (0)