Refactor constructor parameters for headers and body to Message

This commit is contained in:
PJ Dietz 2015-04-27 11:46:36 -04:00
parent cee55cada0
commit af9fbf9c50
4 changed files with 42 additions and 28 deletions

View File

@ -14,11 +14,27 @@ abstract class Message implements MessageInterface
/** @var string */ /** @var string */
protected $protcolVersion = "1.1"; protected $protcolVersion = "1.1";
public function __construct() /**
* @param array $headers
* @param StreamInterface $body
*/
public function __construct(array $headers = null, StreamInterface $body = null)
{ {
$this->headers = new HeaderCollection(); $this->headers = new HeaderCollection();
if ($headers) {
foreach ($headers as $name => $values) {
foreach ($values as $value) {
$this->headers[$name] = $value;
}
}
}
if ($body !== null) {
$this->body = $body;
} else {
$this->body = new NullStream(); $this->body = new NullStream();
} }
}
public function __clone() public function __clone()
{ {

View File

@ -29,7 +29,7 @@ class Request extends Message implements RequestInterface
array $headers = null, array $headers = null,
StreamInterface $body = null StreamInterface $body = null
) { ) {
parent::__construct(); parent::__construct($headers, $body);
if ($uri !== null) { if ($uri !== null) {
$this->uri = $uri; $this->uri = $uri;
@ -38,18 +38,6 @@ class Request extends Message implements RequestInterface
} }
$this->method = $method; $this->method = $method;
if ($headers) {
foreach ($headers as $name => $values) {
foreach ($values as $value) {
$this->headers[$name] = $value;
}
}
}
if ($body !== null) {
$this->body = $body;
}
} }
public function __clone() public function __clone()

View File

@ -26,21 +26,9 @@ class Response extends Message implements ResponseInterface
*/ */
public function __construct($statusCode = 500, array $headers = null, StreamInterface $body = null) public function __construct($statusCode = 500, array $headers = null, StreamInterface $body = null)
{ {
parent::__construct(); parent::__construct($headers, $body);
$this->statusCode = $statusCode; $this->statusCode = $statusCode;
$this->reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($statusCode); $this->reasonPhrase = $this->getDefaultReasonPhraseForStatusCode($statusCode);
if ($headers) {
foreach ($headers as $name => $values) {
foreach ($values as $value) {
$this->headers[$name] = $value;
}
}
}
if ($body !== null) {
$this->body = $body;
}
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -17,6 +17,28 @@ class MessageTest extends \PHPUnit_Framework_TestCase
$this->assertNotNull($message); $this->assertNotNull($message);
} }
/**
* @covers WellRESTed\Message\Message::__construct
*/
public function testSetsHeadersOnConstruction()
{
$headers = ["X-foo" => ["bar", "baz"]];
$body = null;
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message', [$headers, $body]);
$this->assertEquals(["bar", "baz"], $message->getHeader("X-foo"));
}
/**
* @covers WellRESTed\Message\Message::__construct
*/
public function testSetsBodyOnConstruction()
{
$headers = null;
$body = $this->prophesize('\Psr\Http\Message\StreamInterface');
$message = $this->getMockForAbstractClass('\WellRESTed\Message\Message', [$headers, $body->reveal()]);
$this->assertSame($body->reveal(), $message->getBody());
}
/** /**
* @covers WellRESTed\Message\Message::__clone * @covers WellRESTed\Message\Message::__clone
*/ */