Add named constructor on JsonResponse

To make easier construction with raw json
This commit is contained in:
Timothée Barray 2016-08-05 22:43:03 +02:00
parent 64ace101fb
commit 6d5a65dd8b
2 changed files with 14 additions and 0 deletions

View File

@ -58,6 +58,14 @@ class JsonResponse extends Response
return new static($data, $status, $headers);
}
/**
* Make easier the creation of JsonResponse from raw json.
*/
public static function fromJsonString($data = null, $status = 200, $headers = array())
{
return new static($data, $status, $headers, true);
}
/**
* Sets the JSONP callback.
*

View File

@ -198,6 +198,12 @@ class JsonResponseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
}
public function testItAcceptsJsonAsString()
{
$response = JsonResponse::fromJsonString('{"foo":"bar"}');
$this->assertSame('{"foo":"bar"}', $response->getContent());
}
/**
* @expectedException \InvalidArgumentException
*/