Add _stop() to RedirectRoute.

Moved from CakeResponse to RedirectRoute,
as RedirectRoute is the only place its currently needed.

Refs #2143
This commit is contained in:
mark_story 2011-10-22 22:56:50 -04:00
parent 8e69df9013
commit 6d6aa3cb75
3 changed files with 31 additions and 1 deletions

View file

@ -37,6 +37,13 @@ class RedirectRoute extends CakeRoute {
*/
public $redirect;
/**
* Flag for disabling exit() when this route parses a url.
*
* @var boolean
*/
public $stop = true;
/**
* Constructor
*
@ -79,6 +86,7 @@ class RedirectRoute extends CakeRoute {
$this->response->header(array('Location' => Router::url($redirect, true)));
$this->response->statusCode($status);
$this->response->send();
$this->_stop();
}
/**
@ -90,4 +98,17 @@ class RedirectRoute extends CakeRoute {
public function match($url) {
return false;
}
}
/**
* Stop execution of the current script. Wraps exit() making
* testing easier.
*
* @param integer|string $status see http://php.net/exit for values
* @return void
*/
protected function _stop($code = 0) {
if ($this->stop) {
exit($code);
}
}
}

View file

@ -45,43 +45,51 @@ class RedirectRouteTestCase extends CakeTestCase {
*/
public function testParsing() {
$route = new RedirectRoute('/home', array('controller' => 'posts'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true)));
$route = new RedirectRoute('/home', array('controller' => 'posts', 'action' => 'index'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/home');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts', true)));
$this->assertEqual($route->response->statusCode(), 301);
$route = new RedirectRoute('/google', 'http://google.com');
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/google');
$this->assertEqual($route->response->header(), array('Location' => 'http://google.com'));
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('status' => 302));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view', true)));
$this->assertEqual($route->response->statusCode(), 302);
$route = new RedirectRoute('/posts/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/posts/view/2', true)));
$route = new RedirectRoute('/posts/*', '/test', array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/posts/2');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/test', true)));
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'), array('persist' => true));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add/passme/named:param', true)));
$route = new RedirectRoute('/my_controllers/:action/*', array('controller' => 'tags', 'action' => 'add'));
$route->stop = false;
$route->response = $this->getMock('CakeResponse', array('_sendHeader'));
$result = $route->parse('/my_controllers/do_something/passme/named:param');
$this->assertEqual($route->response->header(), array('Location' => Router::url('/tags/add', true)));

View file

@ -2459,6 +2459,7 @@ class RouterTest extends CakeTestCase {
Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302));
$this->assertEqual(count(Router::$routes), 1);
Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
Router::$routes[0]->stop = false;
$this->assertEqual(Router::$routes[0]->options['status'], 302);
Router::parse('/blog');