From: Robert Sesek Date: Mon, 31 Mar 2014 00:55:15 +0000 (-0400) Subject: Add RootController::StopWithRedirect() as a redirect shortcut. X-Git-Tag: api-2~8 X-Git-Url: https://src.bluestatic.org/?a=commitdiff_plain;h=78804a1dc21baf4763116e17c4440501fe79a6bc;p=hoplite.git Add RootController::StopWithRedirect() as a redirect shortcut. --- diff --git a/http/root_controller.php b/http/root_controller.php index 97dd137..03e4747 100644 --- a/http/root_controller.php +++ b/http/root_controller.php @@ -145,6 +145,18 @@ class RootController $this->Stop(); } + /*! + Sets the response code to HTTP 302 FOUND and redirects the page to a new + location. + @param string The destination location of the redirect. + */ + public function StopWithRedirect($location) + { + $this->response->headers['Location'] = $location; + $this->StopWithCode(ResponseCode::FOUND); + } + + /*! Wrapper around PHP exit(). */ diff --git a/testing/tests/http/root_controller_test.php b/testing/tests/http/root_controller_test.php index 9a2d74c..bd10a93 100644 --- a/testing/tests/http/root_controller_test.php +++ b/testing/tests/http/root_controller_test.php @@ -129,7 +129,7 @@ class RootControllerTest extends \PHPUnit_Framework_TestCase $mock = $this->ConfigureMock(array('Stop'), $globals); $mock->request()->url = 'another/action'; - + $mock->expects($this->once()) ->method('Stop'); @@ -161,6 +161,27 @@ class RootControllerTest extends \PHPUnit_Framework_TestCase $mock->Stop(); } + public function testStopWithRedirect() + { + $globals = array(); + $mock = $this->ConfigureMock(array('_Exit'), $globals); + $mock->expects($this->once()) + ->method('_Exit'); + + $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock)); + $output_filter->expects($this->once()) + ->method('FilterOutput') + ->with($this->isInstanceOf('hoplite\http\Request'), + $this->isInstanceOf('hoplite\http\Response')); + + $mock->set_output_filter($output_filter); + + $mock->StopWithRedirect('/foo/bar'); + + $this->assertEquals('/foo/bar', $mock->response()->headers['Location']); + $this->assertEquals(http\ResponseCode::FOUND, $mock->response()->response_code); + } + public function testAbsolutify() { $globals = array(