Browse Source

Merge pull request #7643 from owncloud/chainable_response

Chainable Response in AppFramework
tags/v7.0.0alpha2
Thomas Müller 10 years ago
parent
commit
73ac3d0fcd

+ 3
- 0
lib/public/appframework/http/jsonresponse.php View File

@@ -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;
}



+ 13
- 0
lib/public/appframework/http/response.php View File

@@ -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;
}



+ 6
- 0
lib/public/appframework/http/templateresponse.php View File

@@ -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;
}



+ 10
- 0
tests/lib/appframework/http/JSONResponseTest.php View File

@@ -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());
}

}

+ 20
- 0
tests/lib/appframework/http/ResponseTest.php View File

@@ -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']);

}

}

+ 10
- 0
tests/lib/appframework/http/TemplateResponseTest.php View File

@@ -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());
}

}

Loading…
Cancel
Save