summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2014-04-08 22:42:43 +0200
committerThomas Müller <thomas.mueller@tmit.eu>2014-04-08 22:42:43 +0200
commit73ac3d0fcd3aebade34efdbca6f48520b75729d6 (patch)
treecd8760ae45b22e1df96d4f78b4de2c914643823f
parentc1345d3a2d7ba18859bc836f23628322b8ebc0e0 (diff)
parenta1aacc18dff11351b555304a5361a73e13684bd1 (diff)
downloadnextcloud-server-73ac3d0fcd3aebade34efdbca6f48520b75729d6.tar.gz
nextcloud-server-73ac3d0fcd3aebade34efdbca6f48520b75729d6.zip
Merge pull request #7643 from owncloud/chainable_response
Chainable Response in AppFramework
-rw-r--r--lib/public/appframework/http/jsonresponse.php3
-rw-r--r--lib/public/appframework/http/response.php13
-rw-r--r--lib/public/appframework/http/templateresponse.php6
-rw-r--r--tests/lib/appframework/http/JSONResponseTest.php10
-rw-r--r--tests/lib/appframework/http/ResponseTest.php20
-rw-r--r--tests/lib/appframework/http/TemplateResponseTest.php10
6 files changed, 62 insertions, 0 deletions
diff --git a/lib/public/appframework/http/jsonresponse.php b/lib/public/appframework/http/jsonresponse.php
index b54b23a34e6..6628c4514d9 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 d223621d4fd..45402d9b3b3 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 2200a38beca..f5baf788ada 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 534c54cbcee..b9b7c7d6382 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 063ab8b5d33..27350725d79 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 a583d9da14f..0b158edff6f 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());
+ }
+
}