Skip to content

Commit

Permalink
Merge pull request zendframework#2234 from weierophinney/hotfix/mvc-r…
Browse files Browse the repository at this point in the history
…edirect-toroute-sync

Synced redirect() plugin with url() plugin
  • Loading branch information
EvanDotPro committed Aug 24, 2012
2 parents ad2399f + 893a6c6 commit a508880
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 26 deletions.
40 changes: 14 additions & 26 deletions library/Zend/Mvc/Controller/Plugin/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ class Redirect extends AbstractPlugin
* @throws Exception\DomainException if composed controller does not implement InjectApplicationEventInterface, or
* router cannot be found in controller event
*/
public function toRoute($route, array $params = array(), array $options = array())
public function toRoute($route = null, array $params = array(), $options = array(), $reuseMatchedParams = false)
{
$response = $this->getResponse();
$router = $this->getRouter();
$controller = $this->getController();
if (!$controller || !method_exists($controller, 'plugin')) {
throw new Exception\DomainException('Redirect plugin requires a controller that defines the plugin() method');
}

$response = $this->getResponse();
$urlPlugin = $controller->plugin('url');

if (is_scalar($options)) {
$url = $urlPlugin->fromRoute($route, $params, $options);
} else {
$url = $urlPlugin->fromRoute($route, $params, $options, $reuseMatchedParams);
}

$options['name'] = $route;
$url = $router->assemble($params, $options);
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
Expand All @@ -64,27 +73,6 @@ public function toUrl($url)
return $response;
}

/**
* Get the router
*
* @return RouteStackInterface
* @throws Exception\DomainException if unable to find router
*/
protected function getRouter()
{
if ($this->router) {
return $this->router;
}

$event = $this->getEvent();
$router = $event->getRouter();
if (!$router instanceof RouteStackInterface) {
throw new Exception\DomainException('Redirect plugin requires event compose a router');
}
$this->router = $router;
return $this->router;
}

/**
* Get the response
*
Expand Down
67 changes: 67 additions & 0 deletions tests/ZendTest/Mvc/Controller/Plugin/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Zend\Mvc\Controller\Plugin\Redirect as RedirectPlugin;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\Literal as LiteralRoute;
use Zend\Mvc\Router\Http\Segment as SegmentRoute;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\SimpleRouteStack;
use ZendTest\Mvc\Controller\TestAsset\SampleController;

Expand All @@ -31,10 +33,12 @@ public function setUp()
'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController',
),
)));
$this->router = $router;

$event = new MvcEvent();
$event->setRouter($router);
$event->setResponse($this->response);
$this->event = $event;

$this->controller = new SampleController();
$this->controller->setEvent($event);
Expand Down Expand Up @@ -95,4 +99,67 @@ public function testRedirectToRouteWithoutRouterInEventRaisesDomainException()
$this->setExpectedException('Zend\Mvc\Exception\DomainException', 'event compose a router');
$plugin->toRoute('home');
}

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

public function testPluginWithRouteMatchesReturningNoMatchedRouteNameRaisesExceptionWhenNoRouteProvided()
{
$event = $this->controller->getEvent();
$event->setRouteMatch(new RouteMatch(array()));
$this->setExpectedException('Zend\Mvc\Exception\RuntimeException', 'matched');
$url = $this->plugin->toRoute();
}

public function testPassingNoArgumentsWithValidRouteMatchGeneratesUrl()
{
$routeMatch = new RouteMatch(array());
$routeMatch->setMatchedRouteName('home');
$this->controller->getEvent()->setRouteMatch($routeMatch);
$response = $this->plugin->toRoute();
$headers = $response->getHeaders();
$location = $headers->get('Location');
$this->assertEquals('/', $location->getFieldValue());
}

public function testCanReuseMatchedParameters()
{
$this->router->addRoute('replace', SegmentRoute::factory(array(
'route' => '/:controller/:action',
'defaults' => array(
'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController',
),
)));
$routeMatch = new RouteMatch(array(
'controller' => 'foo',
));
$routeMatch->setMatchedRouteName('replace');
$this->controller->getEvent()->setRouteMatch($routeMatch);
$response = $this->plugin->toRoute('replace', array('action' => 'bar'), array(), true);
$headers = $response->getHeaders();
$location = $headers->get('Location');
$this->assertEquals('/foo/bar', $location->getFieldValue());
}

public function testCanPassBooleanValueForThirdArgumentToAllowReusingRouteMatches()
{
$this->router->addRoute('replace', SegmentRoute::factory(array(
'route' => '/:controller/:action',
'defaults' => array(
'controller' => 'ZendTest\Mvc\Controller\TestAsset\SampleController',
),
)));
$routeMatch = new RouteMatch(array(
'controller' => 'foo',
));
$routeMatch->setMatchedRouteName('replace');
$this->controller->getEvent()->setRouteMatch($routeMatch);
$response = $this->plugin->toRoute('replace', array('action' => 'bar'), true);
$headers = $response->getHeaders();
$location = $headers->get('Location');
$this->assertEquals('/foo/bar', $location->getFieldValue());
}
}

0 comments on commit a508880

Please sign in to comment.