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

Add shortcut method for empty responses #58

Merged
merged 10 commits into from
Jun 23, 2015
42 changes: 42 additions & 0 deletions src/Response/EmptyResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Diactoros\Response;

use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;

/**
* A class representing empty HTTP responses.
*/
class EmptyResponse extends Response
{
/**
* Create an empty response with the given status code.
*
* @param int $status Status code for the response, if any.
* @param array $headers Headers for the response, if any.
*/
public function __construct($status = 204, array $headers = [])
{
$body = new Stream('php://temp', 'r');
parent::__construct($body, $status, $headers);
}

/**
* Create an empty response with the given headers.
*
* @param array $headers Headers for the response.
* @return EmptyResponse
*/
public static function withHeaders(array $headers)
{
return new static(204, $headers);
}
}
4 changes: 2 additions & 2 deletions src/Response/StringResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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
* body to the stream; if no content-type header was provided, the given
* $contentType is injected for it.
*
* @param string $body
Expand All @@ -85,7 +85,7 @@ private static function createResponse($body, $status, array $headers, $contentT
$response = new Response('php://temp', $status, $headers);
$response->getBody()->write($body);

if ($response->hasHeader('content-type')) {
if (empty($contentType) || $response->hasHeader('content-type')) {
return $response;
}

Expand Down
33 changes: 33 additions & 0 deletions test/Response/EmptyResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Diactoros\Response;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Diactoros\Response\EmptyResponse;

class EmptyResponseTest extends TestCase
{
public function testConstructor()
{
$response = new EmptyResponse(201);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertEquals('', (string) $response->getBody());
$this->assertEquals(201, $response->getStatusCode());
}

public function testHeaderConstructor()
{
$response = EmptyResponse::withHeaders(['x-empty' => ['true']]);
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
$this->assertEquals('', (string) $response->getBody());
$this->assertEquals(204, $response->getStatusCode());
$this->assertEquals('true', $response->getHeaderLine('x-empty'));
}
}