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

Commit 10c1d0a

Browse files
committed
Use new EmptyResponse class instead of separate constructor
1 parent 3cfbfc1 commit 10c1d0a

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

src/Response/EmptyResponse.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Diactoros\Response;
11+
12+
use Zend\Diactoros\Response;
13+
use Zend\Diactoros\Stream;
14+
15+
/**
16+
* A class representing empty HTTP responses.
17+
*/
18+
class EmptyResponse extends Response
19+
{
20+
/**
21+
* Create an empty response with the given status code.
22+
*
23+
* @param int $status Status code for the response, if any.
24+
* @param array $headers Headers for the response, if any.
25+
*/
26+
public function __construct($status = 204, array $headers = [])
27+
{
28+
$body = new Stream('php://temp', 'r');
29+
parent::__construct($body, $status, $headers);
30+
}
31+
32+
/**
33+
* Create an empty response with the given headers.
34+
*
35+
* @param array $headers Headers for the response.
36+
* @return EmptyResponse
37+
*/
38+
public static function withHeaders(array $headers)
39+
{
40+
return new static(204, $headers);
41+
}
42+
}

src/Response/StringResponse.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ public static function json($data, $status = 200, array $headers = [])
6060
return static::createResponse($json, $status, $headers, 'application/json');
6161
}
6262

63-
/**
64-
* Create an empty response with the given status code.
65-
*
66-
* @param int $status Status code for the response, if any.
67-
* @param array $headers Headers for the response, if any.
68-
* @return Response
69-
*/
70-
public static function noContent($status = 204, array $headers = [])
71-
{
72-
return static::createResponse('', $status, $headers, null);
73-
}
74-
7563
/**
7664
* This class is non-instantiable.
7765
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ZendTest\Diactoros\Response;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Zend\Diactoros\Response\EmptyResponse;
14+
15+
class EmptyResponseTest extends TestCase
16+
{
17+
public function testConstructor()
18+
{
19+
$response = new EmptyResponse(201);
20+
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
21+
$this->assertEquals('', (string) $response->getBody());
22+
$this->assertEquals(201, $response->getStatusCode());
23+
}
24+
25+
public function testHeaderConstructor()
26+
{
27+
$response = EmptyResponse::withHeaders(['x-empty' => ['true']]);
28+
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
29+
$this->assertEquals('', (string) $response->getBody());
30+
$this->assertEquals(204, $response->getStatusCode());
31+
$this->assertEquals('true', $response->getHeaderLine('x-empty'));
32+
}
33+
}

test/Response/StringResponseTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ public function testScalarValuePassedToJsonRendersValueWithinJSONArray($value)
8383
$this->assertSame(json_encode([$value], JSON_UNESCAPED_SLASHES), (string) $response->getBody());
8484
}
8585

86-
public function testNoContentConstructor()
87-
{
88-
$response = StringResponse::noContent();
89-
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
90-
$this->assertEquals(204, $response->getStatusCode());
91-
$this->assertEquals('', (string) $response->getBody());
92-
}
93-
9486
public function testContentTypeCanBeOverwritten()
9587
{
9688
$data = null;

0 commit comments

Comments
 (0)