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

Commit

Permalink
refresh() method in Redirect plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
marek.nos committed Aug 2, 2012
1 parent 84abe1c commit 092fffd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion library/Zend/Mvc/Controller/Plugin/Redirect.php
Expand Up @@ -28,6 +28,24 @@ class Redirect extends AbstractPlugin
protected $response;
protected $router;

/**
* Gets current route name
*
* @return string
*/
protected function getCurrentRoute ()
{
return $this->getEvent()->getRouteMatch()->getMatchedRouteName();
}

/**
* Gets current route params
*/
protected function getCurrentRouteParams()
{
return $this->getEvent()->getRouteMatch()->getParams();
}

/**
* Generates a URL based on a route
*
Expand All @@ -38,8 +56,14 @@ 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(), array $options = array())
{
// Refresh if null provided
if (is_null($route)) {
$route = $this->getCurrentRoute();
$params = array_merge($this->getCurrentRouteParams(), $params);
}

$response = $this->getResponse();
$router = $this->getRouter();

Expand All @@ -64,6 +88,16 @@ public function toUrl($url)
return $response;
}

/**
* Refresh to current route
*
* @return string
*/
public function refresh()
{
return $this->toRoute($this->getCurrentRoute(), $this->getCurrentRouteParams());
}

/**
* Get the router
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Zend/Mvc/Controller/Plugin/RedirectTest.php
Expand Up @@ -12,10 +12,12 @@

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Response;
use Zend\Route\Request;
use Zend\Mvc\Controller\Plugin\Redirect as RedirectPlugin;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\Literal as LiteralRoute;
use Zend\Mvc\Router\SimpleRouteStack;
use Zend\Mvc\Router\RouteMatch;
use ZendTest\Mvc\Controller\TestAsset\SampleController;

class RedirectTest extends TestCase
Expand All @@ -32,9 +34,13 @@ public function setUp()
),
)));

$routeMatch = new RouteMatch(array());
$routeMatch->setMatchedRouteName('home');

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

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

public function testPluginCanRefreshToRouteWhenProperlyConfigured()
{
$response = $this->plugin->refresh();
$this->assertTrue($response->isRedirect());
$headers = $response->getHeaders();
$location = $headers->get('Location');
$this->assertEquals('/', $location->getFieldValue());
}

public function testPluginCanRedirectToRouteWithNullWhenProperlyConfigured()
{
$response = $this->plugin->toRoute();
$this->assertTrue($response->isRedirect());
$headers = $response->getHeaders();
$location = $headers->get('Location');
$this->assertEquals('/', $location->getFieldValue());
}
}

0 comments on commit 092fffd

Please sign in to comment.