Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[#52] Review prior to merge
Browse files Browse the repository at this point in the history
- Added method docblocks where missing.
- Added behavior to `json()` method: cast scalars to array prior to attempts to
  serialize.
  • Loading branch information
weierophinney committed Jun 12, 2015
1 parent 61aa834 commit 654e57b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
31 changes: 28 additions & 3 deletions src/Response/StringResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
/**
* String response factory.
*
* A class with helper methods for easily creating proper responses from strings or structured data.
* A class with helper methods for easily creating proper responses from
* strings or structured data.
*/
final class StringResponse
{
/**
* Create a HTML response with the given body text.
* Create an HTML response with the given body text.
*
* @param string $html The response body content, as a string.
* @param int $status Status code for the response, if any.
Expand All @@ -35,26 +36,50 @@ public static function html($html, $status = 200, array $headers = [])
/**
* Create a JSON response with the given array of data.
*
* @param array|\JsonSerializable $data The data to be converted to JSON.
* If the data provided is null, an empty ArrayObject is used; if the data
* is scalar, it is cast to an array prior to serialization.
*
* @param mixed $data The data to be converted to JSON.
* @param int $status Status code for the response, if any.
* @param array $headers Headers for the response, if any.
* @return Response
*/
public static function json($data, $status = 200, array $headers = [])
{
if ($data === null) {
// Use an ArrayObject to force an empty JSON object.
$data = new ArrayObject();
}

if (is_scalar($data)) {
$data = (array) $data;
}

$json = json_encode($data, JSON_UNESCAPED_SLASHES);

return static::createResponse($json, $status, $headers, 'application/json');
}

/**
* This class is non-instantiable.
*/
private function __construct()
{
}

/**
* Create a Response from the provided information.
*
* Creates a Response using a php://temp stream, and writes the provided
* body to the stream; if non content-type header was provided, the given
* $contentType is injected for it.
*
* @param string $body
* @param int $status
* @param array $headers
* @param string $contentType
* @return Response
*/
private static function createResponse($body, $status, array $headers, $contentType)
{
$response = new Response('php://temp', $status, $headers);
Expand Down
47 changes: 41 additions & 6 deletions test/Response/StringResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testHtmlConstructor()

$response = StringResponse::html($body, $status, $headers);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertSame($body, $response->getBody()->__toString());
$this->assertSame($body, (string) $response->getBody());
$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals(['foo-bar'], $response->getHeader('x-custom'));
$this->assertEquals('text/html', $response->getHeaderLine('content-type'));
Expand All @@ -35,17 +35,52 @@ public function testJsonConstructor()
$data = [
'nested' => [
'json' => [
'tree'
]
]
'tree',
],
],
];
$json = '{"nested":{"json":["tree"]}}';

$response = StringResponse::json($data);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertSame($json, $response->getBody()->__toString());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('content-type'));
$this->assertSame($json, (string) $response->getBody());
}

public function testNullValuePassedToJsonRendersEmptyJSONObject()
{
$response = StringResponse::json(null);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('content-type'));
$this->assertSame('{}', (string) $response->getBody());
}

public function scalarValuesForJSON()
{
return [
'false' => [false],
'true' => [true],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'empty-string' => [''],
'string' => ['string'],
];
}

/**
* @dataProvider scalarValuesForJSON
*/
public function testScalarValuePassedToJsonRendersValueWithinJSONArray($value)
{
$response = StringResponse::json($value);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('content-type'));
$this->assertSame(json_encode([$value], JSON_UNESCAPED_SLASHES), (string) $response->getBody());
}

public function testContentTypeCanBeOverwritten()
Expand All @@ -54,7 +89,7 @@ public function testContentTypeCanBeOverwritten()
$json = '{}';

$response = StringResponse::json($data, 200, ['content-type' => 'foo/json']);
$this->assertSame($json, $response->getBody()->__toString());
$this->assertSame($json, (string) $response->getBody());
$this->assertEquals('foo/json', $response->getHeaderLine('content-type'));
}
}

0 comments on commit 654e57b

Please sign in to comment.