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

Commit

Permalink
Merge pull request zendframework/zendframework#2221 from weierophinne…
Browse files Browse the repository at this point in the history
…y/hotfix/url-plugin-sync

Synced url plugin and helper usage
  • Loading branch information
EvanDotPro committed Aug 22, 2012
2 parents 0a6bf6e + 40ab290 commit a995715
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ public function setRouteMatch(RouteMatch $routeMatch)
* @throws Exception\RuntimeException If no RouteMatch was provided
* @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name
*/
public function __invoke($name = null, array $params = array(), array $options = array(), $reuseMatchedParams = false)
public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false)
{
if (null === $this->router) {
throw new Exception\RuntimeException('No RouteStackInterface instance provided');
}

if (3 == func_num_args() && is_bool($options)) {
$reuseMatchedParams = $options;
$options = array();
}

if ($name === null) {
if ($this->routeMatch === null) {
throw new Exception\RuntimeException('No RouteMatch instance provided');
Expand Down
64 changes: 64 additions & 0 deletions test/Helper/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\View\Helper;

use Zend\View\Helper\Url as UrlHelper;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\SimpleRouteStack as Router;

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ protected function setUp()
'route' => '/:controller[/:action]',
)
));
$this->router = $router;

$this->url = new UrlHelper;
$this->url->setRouter($router);
Expand All @@ -68,4 +70,66 @@ public function testModuleRoute()
$url = $this->url->__invoke('default', array('controller' => 'ctrl', 'action' => 'act'));
$this->assertEquals('/ctrl/act', $url);
}

public function testPluginWithoutRouteMatchesInEventRaisesExceptionWhenNoRouteProvided()
{
$this->setExpectedException('Zend\View\Exception\RuntimeException', 'RouteMatch');
$url = $this->url->__invoke();
}

public function testPluginWithRouteMatchesReturningNoMatchedRouteNameRaisesExceptionWhenNoRouteProvided()
{
$this->url->setRouteMatch(new RouteMatch(array()));
$this->setExpectedException('Zend\View\Exception\RuntimeException', 'matched');
$url = $this->url->__invoke();
}

public function testPassingNoArgumentsWithValidRouteMatchGeneratesUrl()
{
$routeMatch = new RouteMatch(array());
$routeMatch->setMatchedRouteName('home');
$this->url->setRouteMatch($routeMatch);
$url = $this->url->__invoke();
$this->assertEquals('/', $url);
}

public function testCanReuseMatchedParameters()
{
$this->router->addRoute('replace', array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:controller/:action',
'defaults' => array(
'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController',
),
),
));
$routeMatch = new RouteMatch(array(
'controller' => 'foo',
));
$routeMatch->setMatchedRouteName('replace');
$this->url->setRouteMatch($routeMatch);
$url = $this->url->__invoke('replace', array('action' => 'bar'), array(), true);
$this->assertEquals('/foo/bar', $url);
}

public function testCanPassBooleanValueForThirdArgumentToAllowReusingRouteMatches()
{
$this->router->addRoute('replace', array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:controller/:action',
'defaults' => array(
'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController',
),
),
));
$routeMatch = new RouteMatch(array(
'controller' => 'foo',
));
$routeMatch->setMatchedRouteName('replace');
$this->url->setRouteMatch($routeMatch);
$url = $this->url->__invoke('replace', array('action' => 'bar'), true);
$this->assertEquals('/foo/bar', $url);
}
}

0 comments on commit a995715

Please sign in to comment.