From af9fbf9c50e81f4a1b51ecf821839edda332b352 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 27 Apr 2015 11:46:36 -0400 Subject: [PATCH] Refactor constructor parameters for headers and body to Message --- src/Message/Message.php | 20 ++++++++++++++++++-- src/Message/Request.php | 14 +------------- src/Message/Response.php | 14 +------------- test/tests/unit/Message/MessageTest.php | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/Message/Message.php b/src/Message/Message.php index f39e8ac..f26ad9d 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -14,10 +14,26 @@ abstract class Message implements MessageInterface /** @var string */ 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->body = new NullStream(); + 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(); + } } public function __clone() diff --git a/src/Message/Request.php b/src/Message/Request.php index bf409ad..749d1fc 100644 --- a/src/Message/Request.php +++ b/src/Message/Request.php @@ -29,7 +29,7 @@ class Request extends Message implements RequestInterface array $headers = null, StreamInterface $body = null ) { - parent::__construct(); + parent::__construct($headers, $body); if ($uri !== null) { $this->uri = $uri; @@ -38,18 +38,6 @@ class Request extends Message implements RequestInterface } $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() diff --git a/src/Message/Response.php b/src/Message/Response.php index 3ed2ad6..d0bc04c 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -26,21 +26,9 @@ class Response extends Message implements ResponseInterface */ public function __construct($statusCode = 500, array $headers = null, StreamInterface $body = null) { - parent::__construct(); + parent::__construct($headers, $body); $this->statusCode = $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; - } } // ------------------------------------------------------------------------ diff --git a/test/tests/unit/Message/MessageTest.php b/test/tests/unit/Message/MessageTest.php index c294eaf..7e94eaa 100644 --- a/test/tests/unit/Message/MessageTest.php +++ b/test/tests/unit/Message/MessageTest.php @@ -17,6 +17,28 @@ class MessageTest extends \PHPUnit_Framework_TestCase $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 */