diff --git a/lib/public/appframework/http/jsonresponse.php b/lib/public/appframework/http/jsonresponse.php index b54b23a34e..6628c4514d 100644 --- a/lib/public/appframework/http/jsonresponse.php +++ b/lib/public/appframework/http/jsonresponse.php @@ -66,9 +66,12 @@ 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; + + return $this; } diff --git a/lib/public/appframework/http/response.php b/lib/public/appframework/http/response.php index d223621d4f..45402d9b3b 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; } @@ -88,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)) { @@ -95,6 +97,8 @@ class Response { } else { $this->headers[$name] = $value; } + + return $this; } @@ -130,9 +134,12 @@ 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; + + return $this; } @@ -165,18 +172,24 @@ class Response { /** * Set the ETag * @param string $ETag + * @return Response Reference to this object */ public function setETag($ETag) { $this->ETag = $ETag; + + return $this; } /** * Set "last modified" date * @param \DateTime $lastModified + * @return Response Reference to this object */ 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..f5baf788ad 100644 --- a/lib/public/appframework/http/templateresponse.php +++ b/lib/public/appframework/http/templateresponse.php @@ -74,9 +74,12 @@ 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; + + return $this; } @@ -104,9 +107,12 @@ 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; + + 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()); + } + }