From 78804a1dc21baf4763116e17c4440501fe79a6bc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 30 Mar 2014 20:55:15 -0400 Subject: [PATCH] Add RootController::StopWithRedirect() as a redirect shortcut. --- http/root_controller.php | 12 +++++++++++ testing/tests/http/root_controller_test.php | 23 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) 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( -- 2.43.5