From 28701f9a3ba09727e01c01c6cc899743f29cc255 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Tue, 17 Jun 2014 21:00:26 +1000 Subject: [PATCH] Backend and frontend controllers now return a Response object and support setting the status code --- modules/backend/classes/Controller.php | 22 +++++++++++++- modules/cms/classes/Controller.php | 3 ++ tests/unit/cms/classes/ControllerTest.php | 36 +++++++++++++---------- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/modules/backend/classes/Controller.php b/modules/backend/classes/Controller.php index 2dfab7f8d..cef030bcc 100644 --- a/modules/backend/classes/Controller.php +++ b/modules/backend/classes/Controller.php @@ -90,6 +90,11 @@ class Controller extends Extendable */ protected $guarded = []; + /** + * @var int Response status code + */ + protected $statusCode = 200; + /** * Constructor. */ @@ -173,7 +178,12 @@ class Controller extends Extendable /* * Execute page action */ - return $this->execPageAction($action, $params); + $result = $this->execPageAction($action, $params); + + if (!is_string($result)) + return $result; + + return Response::make($result, $this->statusCode); } /** @@ -425,6 +435,16 @@ class Controller extends Extendable return $id; } + /** + * Sets the status code for the current web response. + * @param int $code Status code + */ + public function setStatusCode($code) + { + $this->statusCode = (int) $code; + return $this; + } + /** * Sets standard page variables in the case of a controller error. */ diff --git a/modules/cms/classes/Controller.php b/modules/cms/classes/Controller.php index b8c741057..188900578 100644 --- a/modules/cms/classes/Controller.php +++ b/modules/cms/classes/Controller.php @@ -209,6 +209,9 @@ class Controller extends BaseController if ($event = $this->fireEvent('page.display', [$this, $url, $page], true)) return $event; + if (!is_string($result)) + return $result; + return Response::make($result, $this->statusCode); } diff --git a/tests/unit/cms/classes/ControllerTest.php b/tests/unit/cms/classes/ControllerTest.php index 51c1de4ca..e34646b30 100644 --- a/tests/unit/cms/classes/ControllerTest.php +++ b/tests/unit/cms/classes/ControllerTest.php @@ -35,9 +35,10 @@ class ControllerTest extends TestCase $theme->load('test'); $controller = new Controller($theme); $response = $controller->run('/some-page-that-doesnt-exist'); - $this->assertInternalType('string', $response); - $page404Content = file_get_contents($theme->getPath().'/pages/404.htm'); - $this->assertEquals('

Page not found

', $response); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); + $content = $response->getContent(); + $this->assertInternalType('string', $content); + $this->assertEquals('

Page not found

', $content); } public function testRoot() @@ -49,9 +50,10 @@ class ControllerTest extends TestCase $theme->load('test'); $controller = new Controller($theme); $response = $controller->run('/'); - $this->assertInternalType('string', $response); - $content = file_get_contents($theme->getPath().'/pages/index.htm'); - $this->assertEquals('

My Webpage

', trim($response)); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); + $content = $response->getContent(); + $this->assertInternalType('string', $content); + $this->assertEquals('

My Webpage

', trim($content)); } /** @@ -75,7 +77,9 @@ class ControllerTest extends TestCase $theme->load('test'); $controller = new Controller($theme); $response = $controller->run('/with-layout'); - $this->assertEquals('

Hey

', $response); + $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); + $content = $response->getContent(); + $this->assertEquals('

Hey

', $content); } public function testPartials() @@ -86,7 +90,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/with-partials'); + $response = $controller->run('/with-partials')->getContent(); $this->assertEquals('
LAYOUT PARTIAL

Hey PAGE PARTIAL Homer Simpson A partial

', $response); } @@ -95,7 +99,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/with-content'); + $response = $controller->run('/with-content')->getContent(); $this->assertEquals('
LAYOUT CONTENT

Hey PAGE CONTENT A content

', $response); } @@ -104,7 +108,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/with-placeholder'); + $response = $controller->run('/with-placeholder')->getContent(); $this->assertEquals("
LAYOUT CONTENT BLOCK\n DEFAULT

Hey PAGE CONTENT

SECOND BLOCK", $response); } @@ -113,7 +117,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/apage'); + $response = $controller->run('/apage')->getContent(); $this->assertEquals("
LAYOUT CONTENT

This page is a subdirectory

", $response); } @@ -126,7 +130,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/no-partial'); + $response = $controller->run('/no-partial')->getContent(); } public function testPageLifeCycle() @@ -134,7 +138,7 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/cycle-test'); + $response = $controller->run('/cycle-test')->getContent(); $this->assertEquals('12345', $response); } @@ -277,10 +281,10 @@ class ControllerTest extends TestCase $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/with-component'); + $response = $controller->run('/with-component')->getContent(); $page = PHPUnit_Framework_Assert::readAttribute($controller, 'page'); $this->assertArrayHasKey('testArchive', $page->components); - + $component = $page->components['testArchive']; $details = $component->componentDetails(); @@ -306,7 +310,7 @@ ESC; $theme = new Theme(); $theme->load('test'); $controller = new Controller($theme); - $response = $controller->run('/with-components'); + $response = $controller->run('/with-components')->getContent(); $page = PHPUnit_Framework_Assert::readAttribute($controller, 'page'); $this->assertArrayHasKey('firstAlias', $page->components);