Browse Source

Add _stop() to RedirectRoute.

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

Refs #2143
mark_story 14 years ago
parent
commit
6d6aa3cb75

+ 22 - 1
lib/Cake/Routing/Route/RedirectRoute.php

@@ -38,6 +38,13 @@ class RedirectRoute extends CakeRoute {
 	public $redirect;
 
 /**
+ * Flag for disabling exit() when this route parses a url.
+ *
+ * @var boolean
+ */
+	public $stop = true;
+
+/**
  * Constructor
  *
  * @param string $template Template string with parameter placeholders
@@ -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);
+		}
+	}
+}

+ 8 - 0
lib/Cake/Test/Case/Routing/Route/RedirectRouteTest.php

@@ -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)));

+ 1 - 0
lib/Cake/Test/Case/Routing/RouterTest.php

@@ -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');