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

Commit ce4c9a1

Browse files
committed
Created alternate url() helper implementation
1 parent 043401a commit ce4c9a1

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* @see http://github.com/zendframework/zend-expressive for the canonical source repository
4+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Expressive\Template\ZendView;
9+
10+
use Zend\Expressive\Exception;
11+
use Zend\Expressive\Router\RouterInterface;
12+
use Zend\Expressive\Router\RouteResult;
13+
use Zend\View\Helper\AbstractHelper;
14+
15+
class UrlHelper extends AbstractHelper
16+
{
17+
/**
18+
* @var RouteResult
19+
*/
20+
private $result;
21+
22+
/**
23+
* @var RouterInterface
24+
*/
25+
private $router;
26+
27+
/**
28+
* @param RouterInterface $router
29+
*/
30+
public function __construct(RouterInterface $router)
31+
{
32+
$this->router = $router;
33+
}
34+
35+
/**
36+
* @param string $route
37+
* @param array $params
38+
* @return string
39+
* @throws Exception\RenderingException if no route provided, and no result
40+
* match present.
41+
* @throws Exception\RenderingException if no route provided, and result
42+
* match is a routing failure.
43+
* @throws Exception\RuntimeException if router cannot generate URI for
44+
* given route.
45+
*/
46+
public function __invoke($route = null, $params = [])
47+
{
48+
if ($route === null && $this->result === null) {
49+
throw new Exception\RenderingException(
50+
'Attempting to use matched result when none was injected; aborting'
51+
);
52+
}
53+
54+
if ($route === null) {
55+
return $this->generateUriFromResult($params);
56+
}
57+
58+
return $this->router->generateUri($route, $params);
59+
}
60+
61+
/**
62+
* @param RouteResult $result
63+
*/
64+
public function setRouteResult(RouteResult $result)
65+
{
66+
$this->result = $result;
67+
}
68+
69+
/**
70+
* @param array $params
71+
* @return string
72+
* @throws Exception\RenderingException if current result is a routing
73+
* failure.
74+
*/
75+
private function generateUriFromResult(array $params)
76+
{
77+
if ($this->result->isFailure()) {
78+
throw new Exception\RenderingException(
79+
'Attempting to use matched result when routing failed; aborting'
80+
);
81+
}
82+
83+
$name = $this->result->getMatchedRouteName();
84+
$params = array_merge($this->result->getMatchedParams(), $params);
85+
return $this->router->generateUri($name, $params);
86+
}
87+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ZendTest\Expressive\Template\ZendView;
11+
12+
use ArrayObject;
13+
use PHPUnit_Framework_TestCase as TestCase;
14+
use Zend\Expressive\Exception;
15+
use Zend\Expressive\Router\RouterInterface;
16+
use Zend\Expressive\Router\RouteResult;
17+
use Zend\Expressive\Template\ZendView\UrlHelper;
18+
19+
class UrlHelperTest extends TestCase
20+
{
21+
public function setUp()
22+
{
23+
$this->router = $this->prophesize(RouterInterface::class);
24+
}
25+
26+
public function createHelper()
27+
{
28+
return new UrlHelper($this->router->reveal());
29+
}
30+
31+
public function testRaisesExceptionOnInvocationIfNoRouteProvidedAndNoResultPresent()
32+
{
33+
$helper = $this->createHelper();
34+
$this->setExpectedException(Exception\RenderingException::class, 'use matched result');
35+
$helper();
36+
}
37+
38+
public function testRaisesExceptionOnInvocationIfNoRouteProvidedAndResultIndicatesFailure()
39+
{
40+
$result = $this->prophesize(RouteResult::class);
41+
$result->isFailure()->willReturn(true);
42+
$helper = $this->createHelper();
43+
$helper->setRouteResult($result->reveal());
44+
$this->setExpectedException(Exception\RenderingException::class, 'routing failed');
45+
$helper();
46+
}
47+
48+
public function testRaisesExceptionOnInvocationIfRouterCannotGenerateUriForRouteProvided()
49+
{
50+
$this->router->generateUri('foo', [])->willThrow(Exception\RuntimeException::class);
51+
$helper = $this->createHelper();
52+
$this->setExpectedException(Exception\RuntimeException::class);
53+
$helper('foo');
54+
}
55+
56+
public function testWhenNoRouteProvidedTheHelperUsesComposedResultToGenerateUrl()
57+
{
58+
$result = $this->prophesize(RouteResult::class);
59+
$result->isFailure()->willReturn(false);
60+
$result->getMatchedRouteName()->willReturn('foo');
61+
$result->getMatchedParams()->willReturn(['bar' => 'baz']);
62+
63+
$this->router->generateUri('foo', ['bar' => 'baz'])->willReturn('URL');
64+
65+
$helper = $this->createHelper();
66+
$helper->setRouteResult($result->reveal());
67+
68+
$this->assertEquals('URL', $helper());
69+
}
70+
71+
public function testWhenNoRouteProvidedTheHelperMergesPassedParametersWithResultParametersToGenerateUrl()
72+
{
73+
$result = $this->prophesize(RouteResult::class);
74+
$result->isFailure()->willReturn(false);
75+
$result->getMatchedRouteName()->willReturn('foo');
76+
$result->getMatchedParams()->willReturn(['bar' => 'baz']);
77+
78+
$this->router->generateUri('foo', ['bar' => 'baz', 'baz' => 'bat'])->willReturn('URL');
79+
80+
$helper = $this->createHelper();
81+
$helper->setRouteResult($result->reveal());
82+
83+
$this->assertEquals('URL', $helper(null, ['baz' => 'bat']));
84+
}
85+
86+
public function testWhenRouteProvidedTheHelperDelegatesToTheRouterToGenerateUrl()
87+
{
88+
$this->router->generateUri('foo', ['bar' => 'baz'])->willReturn('URL');
89+
$helper = $this->createHelper();
90+
$this->assertEquals('URL', $helper('foo', ['bar' => 'baz']));
91+
}
92+
}

0 commit comments

Comments
 (0)