From 8f6ea900f2f75bfdfadb7f484ec64ac7ea78623c Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Sun, 9 Mar 2014 23:01:16 +0100 Subject: [PATCH 1/2] Chainable Response in AppFramework --- lib/public/appframework/http/jsonresponse.php | 2 ++ lib/public/appframework/http/response.php | 9 +++++++++ .../appframework/http/templateresponse.php | 4 ++++ .../appframework/http/JSONResponseTest.php | 10 ++++++++++ tests/lib/appframework/http/ResponseTest.php | 20 +++++++++++++++++++ .../http/TemplateResponseTest.php | 10 ++++++++++ 6 files changed, 55 insertions(+) diff --git a/lib/public/appframework/http/jsonresponse.php b/lib/public/appframework/http/jsonresponse.php index b54b23a34e..bcfe469ef1 100644 --- a/lib/public/appframework/http/jsonresponse.php +++ b/lib/public/appframework/http/jsonresponse.php @@ -69,6 +69,8 @@ class JSONResponse extends Response { */ public function setData($data){ $this->data = $data; + + return $this; } diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php index d223621d4f..e704acca7f 100644 --- a/lib/public/appframework/http/response.php +++ b/lib/public/appframework/http/response.php @@ -80,6 +80,7 @@ class Response { $this->addHeader('Cache-Control', 'no-cache, must-revalidate'); } + return $this; } @@ -95,6 +96,8 @@ class Response { } else { $this->headers[$name] = $value; } + + return $this; } @@ -133,6 +136,8 @@ class Response { */ public function setStatus($status) { $this->status = $status; + + return $this; } @@ -168,6 +173,8 @@ class Response { */ public function setETag($ETag) { $this->ETag = $ETag; + + return $this; } @@ -177,6 +184,8 @@ class Response { */ public function setLastModified($lastModified) { $this->lastModified = $lastModified; + + return $this; } diff --git a/lib/public/appframework/http/templateresponse.php b/lib/public/appframework/http/templateresponse.php index 2200a38bec..1ae22dde7f 100644 --- a/lib/public/appframework/http/templateresponse.php +++ b/lib/public/appframework/http/templateresponse.php @@ -77,6 +77,8 @@ class TemplateResponse extends Response { */ public function setParams(array $params){ $this->params = $params; + + return $this; } @@ -107,6 +109,8 @@ class TemplateResponse extends Response { */ public function renderAs($renderAs){ $this->renderAs = $renderAs; + + return $this; } diff --git a/tests/lib/appframework/http/JSONResponseTest.php b/tests/lib/appframework/http/JSONResponseTest.php index 534c54cbce..b9b7c7d638 100644 --- a/tests/lib/appframework/http/JSONResponseTest.php +++ b/tests/lib/appframework/http/JSONResponseTest.php @@ -28,6 +28,7 @@ namespace OC\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http; //require_once(__DIR__ . "/../classloader.php"); @@ -95,4 +96,13 @@ class JSONResponseTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($code, $response->getStatus()); } + public function testChainability() { + $params = array('hi', 'yo'); + $this->json->setData($params) + ->setStatus(Http::STATUS_NOT_FOUND); + + $this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus()); + $this->assertEquals(array('hi', 'yo'), $this->json->getData()); + } + } diff --git a/tests/lib/appframework/http/ResponseTest.php b/tests/lib/appframework/http/ResponseTest.php index 063ab8b5d3..27350725d7 100644 --- a/tests/lib/appframework/http/ResponseTest.php +++ b/tests/lib/appframework/http/ResponseTest.php @@ -117,5 +117,25 @@ class ResponseTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']); } + public function testChainability() { + $lastModified = new \DateTime(null, new \DateTimeZone('GMT')); + $lastModified->setTimestamp(1); + + $this->childResponse->setEtag('hi') + ->setStatus(Http::STATUS_NOT_FOUND) + ->setLastModified($lastModified) + ->cacheFor(33) + ->addHeader('hello', 'world'); + + $headers = $this->childResponse->getHeaders(); + + $this->assertEquals('world', $headers['hello']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus()); + $this->assertEquals('hi', $this->childResponse->getEtag()); + $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']); + $this->assertEquals('max-age=33, must-revalidate', + $headers['Cache-Control']); + + } } diff --git a/tests/lib/appframework/http/TemplateResponseTest.php b/tests/lib/appframework/http/TemplateResponseTest.php index a583d9da14..0b158edff6 100644 --- a/tests/lib/appframework/http/TemplateResponseTest.php +++ b/tests/lib/appframework/http/TemplateResponseTest.php @@ -25,6 +25,7 @@ namespace OC\AppFramework\Http; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Http; class TemplateResponseTest extends \PHPUnit_Framework_TestCase { @@ -98,4 +99,13 @@ class TemplateResponseTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($render, $this->tpl->getRenderAs()); } + public function testChainability() { + $params = array('hi' => 'yo'); + $this->tpl->setParams($params) + ->setStatus(Http::STATUS_NOT_FOUND); + + $this->assertEquals(Http::STATUS_NOT_FOUND, $this->tpl->getStatus()); + $this->assertEquals(array('hi' => 'yo'), $this->tpl->getParams()); + } + } From a1aacc18dff11351b555304a5361a73e13684bd1 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Mon, 10 Mar 2014 09:31:30 +0100 Subject: [PATCH 2/2] Add @return PHPDocs --- lib/public/appframework/http/jsonresponse.php | 1 + lib/public/appframework/http/response.php | 4 ++++ lib/public/appframework/http/templateresponse.php | 2 ++ 3 files changed, 7 insertions(+) diff --git a/lib/public/appframework/http/jsonresponse.php b/lib/public/appframework/http/jsonresponse.php index bcfe469ef1..6628c4514d 100644 --- a/lib/public/appframework/http/jsonresponse.php +++ b/lib/public/appframework/http/jsonresponse.php @@ -66,6 +66,7 @@ class JSONResponse extends Response { * Sets values in the data json array * @param array|object $data an array or object which will be transformed * to JSON + * @return JSONResponse Reference to this object */ public function setData($data){ $this->data = $data; diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php index e704acca7f..45402d9b3b 100644 --- a/lib/public/appframework/http/response.php +++ b/lib/public/appframework/http/response.php @@ -89,6 +89,7 @@ class Response { * function * @param string $name The name of the HTTP header * @param string $value The value, null will delete it + * @return Response Reference to this object */ public function addHeader($name, $value) { if(is_null($value)) { @@ -133,6 +134,7 @@ class Response { /** * Set response status * @param int $status a HTTP status code, see also the STATUS constants + * @return Response Reference to this object */ public function setStatus($status) { $this->status = $status; @@ -170,6 +172,7 @@ class Response { /** * Set the ETag * @param string $ETag + * @return Response Reference to this object */ public function setETag($ETag) { $this->ETag = $ETag; @@ -181,6 +184,7 @@ class Response { /** * Set "last modified" date * @param \DateTime $lastModified + * @return Response Reference to this object */ public function setLastModified($lastModified) { $this->lastModified = $lastModified; diff --git a/lib/public/appframework/http/templateresponse.php b/lib/public/appframework/http/templateresponse.php index 1ae22dde7f..f5baf788ad 100644 --- a/lib/public/appframework/http/templateresponse.php +++ b/lib/public/appframework/http/templateresponse.php @@ -74,6 +74,7 @@ class TemplateResponse extends Response { * Sets template parameters * @param array $params an array with key => value structure which sets template * variables + * @return TemplateResponse Reference to this object */ public function setParams(array $params){ $this->params = $params; @@ -106,6 +107,7 @@ class TemplateResponse extends Response { * settings header and footer, user renders the normal * normal page including footer and header and blank * just renders the plain template + * @return TemplateResponse Reference to this object */ public function renderAs($renderAs){ $this->renderAs = $renderAs;