Add RootController::StopWithRedirect() as a redirect shortcut.
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 31 Mar 2014 00:55:15 +0000 (20:55 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 31 Mar 2014 00:55:15 +0000 (20:55 -0400)
http/root_controller.php
testing/tests/http/root_controller_test.php

index 97dd1376328e16da4d1a74cb7fb2a526b743ad28..03e474770109f9e8fc55d522a124d4a34f53e5fa 100644 (file)
@@ -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().
   */
index 9a2d74cdbaaf095be7d06b1d2542c817a4ea59f7..bd10a93fec76105936599d9642bb281497366937 100644 (file)
@@ -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(