Rename Responding\Responder Transmission\Transmitter

This commit is contained in:
PJ Dietz 2015-05-10 20:17:26 -04:00
parent 0f9c5079f9
commit 64eb5aecdd
11 changed files with 95 additions and 82 deletions

View File

@ -1,17 +0,0 @@
<?php
namespace WellRESTed\Responder;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface ResponderInterface
{
/**
* Outputs a response.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response Response to output
*/
public function respond(ServerRequestInterface $request, ResponseInterface $response);
}

View File

@ -9,9 +9,9 @@ use WellRESTed\Dispatching\DispatcherInterface;
use WellRESTed\Dispatching\DispatchStackInterface; use WellRESTed\Dispatching\DispatchStackInterface;
use WellRESTed\Message\Response; use WellRESTed\Message\Response;
use WellRESTed\Message\ServerRequest; use WellRESTed\Message\ServerRequest;
use WellRESTed\Responder\Responder;
use WellRESTed\Responder\ResponderInterface;
use WellRESTed\Routing\Router; use WellRESTed\Routing\Router;
use WellRESTed\Transmission\Transmitter;
use WellRESTed\Transmission\TransmitterInterface;
class Server implements DispatchStackInterface class Server implements DispatchStackInterface
{ {
@ -89,8 +89,8 @@ class Server implements DispatchStackInterface
return $response; return $response;
}; };
$response = $this->dispatch($request, $response, $next); $response = $this->dispatch($request, $response, $next);
$responder = $this->getResponder(); $transmitter = $this->getTransmitter();
$responder->respond($request, $response); $transmitter->transmit($request, $response);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -122,11 +122,11 @@ class Server implements DispatchStackInterface
/** /**
* Return an instance that will output the response to the client. * Return an instance that will output the response to the client.
* *
* @return ResponderInterface * @return TransmitterInterface
*/ */
protected function getResponder() protected function getTransmitter()
{ {
return new Responder(); return new Transmitter();
} }
/** /**

View File

@ -1,6 +1,6 @@
<?php <?php
namespace WellRESTed\Responder\Middleware; namespace WellRESTed\Transmission\Middleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;

View File

@ -1,6 +1,6 @@
<?php <?php
namespace WellRESTed\Responder\Middleware; namespace WellRESTed\Transmission\Middleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;

View File

@ -1,16 +1,16 @@
<?php <?php
namespace WellRESTed\Responder; namespace WellRESTed\Transmission;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
use WellRESTed\Dispatching\Dispatcher; use WellRESTed\Dispatching\Dispatcher;
use WellRESTed\Dispatching\DispatcherInterface; use WellRESTed\Dispatching\DispatcherInterface;
use WellRESTed\Responder\Middleware\ContentLengthHandler; use WellRESTed\Transmission\Middleware\ContentLengthHandler;
use WellRESTed\Responder\Middleware\HeadHandler; use WellRESTed\Transmission\Middleware\HeadHandler;
class Responder implements ResponderInterface class Transmitter implements TransmitterInterface
{ {
/** @var int */ /** @var int */
private $chunkSize = 0; private $chunkSize = 0;
@ -27,12 +27,17 @@ class Responder implements ResponderInterface
} }
/** /**
* Outputs a response. * Outputs a response to the client.
*
* This method outputs the status line, headers, and body to the client.
*
* This method will also provide a Content-length header if needed and
* supress the body for HEAD requests.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param ResponseInterface $response Response to output * @param ResponseInterface $response Response to output
*/ */
public function respond(ServerRequestInterface $request, ResponseInterface $response) public function transmit(ServerRequestInterface $request, ResponseInterface $response)
{ {
// Prepare the response for output. // Prepare the response for output.
$response = $this->prepareResponse($request, $response); $response = $this->prepareResponse($request, $response);

View File

@ -0,0 +1,25 @@
<?php
namespace WellRESTed\Transmission;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface TransmitterInterface
{
/**
* Outputs a response to the client.
*
* This method MUST output the status line, headers, and body to the client.
*
* This method MUST NOT alter the response body, unless it is to remove the
* body for a HEAD request.
*
* Implementations MAY add response headers to ensure expected headers are
* presents but MUST NOT alter existing headers.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response Response to output
*/
public function transmit(ServerRequestInterface $request, ResponseInterface $response);
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace WellRESTed\Responder; namespace WellRESTed\Transmission;
class HeaderStack class HeaderStack
{ {

View File

@ -14,7 +14,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
private $dispatcher; private $dispatcher;
private $request; private $request;
private $response; private $response;
private $responder; private $transmitter;
private $server; private $server;
public function setUp() public function setUp()
@ -22,8 +22,8 @@ class ServerTest extends \PHPUnit_Framework_TestCase
parent::setUp(); parent::setUp();
$this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface');
$this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$this->responder = $this->prophesize('WellRESTed\Responder\ResponderInterface'); $this->transmitter = $this->prophesize('WellRESTed\Transmission\TransmitterInterface');
$this->responder->respond(Argument::cetera())->willReturn(); $this->transmitter->transmit(Argument::cetera())->willReturn();
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface'); $this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
$this->dispatcher->dispatch(Argument::cetera())->will( $this->dispatcher->dispatch(Argument::cetera())->will(
function ($args) { function ($args) {
@ -33,7 +33,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
); );
$this->server = $this->getMockBuilder('WellRESTed\Server') $this->server = $this->getMockBuilder('WellRESTed\Server')
->setMethods(["getDispatcher", "getRequest", "getResponse", "getResponder"]) ->setMethods(["getDispatcher", "getRequest", "getResponse", "getTransmitter"])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->server->expects($this->any()) $this->server->expects($this->any())
@ -46,8 +46,8 @@ class ServerTest extends \PHPUnit_Framework_TestCase
->method("getResponse") ->method("getResponse")
->will($this->returnValue($this->response->reveal())); ->will($this->returnValue($this->response->reveal()));
$this->server->expects($this->any()) $this->server->expects($this->any())
->method("getResponder") ->method("getTransmitter")
->will($this->returnValue($this->responder->reveal())); ->will($this->returnValue($this->transmitter->reveal()));
$this->server->__construct(); $this->server->__construct();
} }
@ -132,7 +132,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase
public function testRespondSendsResponseToResponder() public function testRespondSendsResponseToResponder()
{ {
$this->server->respond(); $this->server->respond();
$this->responder->respond( $this->transmitter->transmit(
$this->request->reveal(), $this->request->reveal(),
$this->response->reveal() $this->response->reveal()
)->shouldHaveBeenCalled(); )->shouldHaveBeenCalled();

View File

@ -1,13 +1,13 @@
<?php <?php
namespace WellRESTed\Test\Unit\Routing\Hook; namespace WellRESTed\Test\Unit\Transmission\Middleware;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Responder\Middleware\ContentLengthHandler; use WellRESTed\Transmission\Middleware\ContentLengthHandler;
/** /**
* @covers WellRESTed\Responder\Middleware\ContentLengthHandler * @covers WellRESTed\Transmission\Middleware\ContentLengthHandler
* @group responder * @group transmission
*/ */
class ContentLengthHandlerTest extends \PHPUnit_Framework_TestCase class ContentLengthHandlerTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -1,14 +1,14 @@
<?php <?php
namespace WellRESTed\Test\Unit\Responder\Middleware; namespace WellRESTed\Test\Unit\Transmission\Middleware;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Responder\Middleware\HeadHandler; use WellRESTed\Transmission\Middleware\HeadHandler;
/** /**
* @covers WellRESTed\Responder\Middleware\HeadHandler * @covers WellRESTed\Transmission\Middleware\HeadHandler
* @uses WellRESTed\Message\NullStream * @uses WellRESTed\Message\NullStream
* @group responder * @group transmission
*/ */
class HeadHandlerTest extends \PHPUnit_Framework_TestCase class HeadHandlerTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -1,23 +1,23 @@
<?php <?php
namespace WellRESTed\Test\Unit\Responder; namespace WellRESTed\Test\Unit\Transmission;
use Prophecy\Argument; use Prophecy\Argument;
use WellRESTed\Responder\HeaderStack; use WellRESTed\Transmission\HeaderStack;
use WellRESTed\Responder\Responder; use WellRESTed\Transmission\Transmitter;
require_once __DIR__ . "/../../../src/HeaderStack.php"; require_once __DIR__ . "/../../../src/HeaderStack.php";
/** /**
* @coversDefaultClass WellRESTed\Responder\Responder * @coversDefaultClass WellRESTed\Transmission\Transmitter
* @uses WellRESTed\Responder\Responder * @uses WellRESTed\Transmission\Transmitter
* @uses WellRESTed\Responder\Middleware\ContentLengthHandler * @uses WellRESTed\Transmission\Middleware\ContentLengthHandler
* @uses WellRESTed\Responder\Middleware\HeadHandler * @uses WellRESTed\Transmission\Middleware\HeadHandler
* @uses WellRESTed\Dispatching\Dispatcher * @uses WellRESTed\Dispatching\Dispatcher
* @uses WellRESTed\Dispatching\DispatchStack * @uses WellRESTed\Dispatching\DispatchStack
* @group responder * @group transmission
*/ */
class ResponderTest extends \PHPUnit_Framework_TestCase class TransmitterTest extends \PHPUnit_Framework_TestCase
{ {
private $request; private $request;
private $response; private $response;
@ -48,12 +48,12 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testCreatesInstance() public function testCreatesInstance()
{ {
$responder = new Responder(); $transmitter = new Transmitter();
$this->assertNotNull($responder); $this->assertNotNull($transmitter);
} }
/** /**
* @covers ::respond * @covers ::transmit
* @covers ::getStatusLine * @covers ::getStatusLine
*/ */
public function testSendStatusCodeWithReasonPhrase() public function testSendStatusCodeWithReasonPhrase()
@ -61,13 +61,13 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
$this->response->getStatusCode()->willReturn("200"); $this->response->getStatusCode()->willReturn("200");
$this->response->getReasonPhrase()->willReturn("Ok"); $this->response->getReasonPhrase()->willReturn("Ok");
$responder = new Responder(); $transmitter = new Transmitter();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders()); $this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders());
} }
/** /**
* @covers ::respond * @covers ::transmit
* @covers ::getStatusLine * @covers ::getStatusLine
*/ */
public function testSendStatusCodeWithoutReasonPhrase() public function testSendStatusCodeWithoutReasonPhrase()
@ -75,13 +75,13 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
$this->response->getStatusCode()->willReturn("999"); $this->response->getStatusCode()->willReturn("999");
$this->response->getReasonPhrase()->willReturn(null); $this->response->getReasonPhrase()->willReturn(null);
$responder = new Responder(); $transmitter = new Transmitter();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders()); $this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders());
} }
/** /**
* @covers ::respond * @covers ::transmit
* @dataProvider headerProvider * @dataProvider headerProvider
*/ */
public function testSendsHeaders($header) public function testSendsHeaders($header)
@ -91,8 +91,8 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
"X-foo" => ["bar", "baz"], "X-foo" => ["bar", "baz"],
]); ]);
$responder = new Responder(); $transmitter = new Transmitter();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->assertContains($header, HeaderStack::getHeaders()); $this->assertContains($header, HeaderStack::getHeaders());
} }
@ -106,7 +106,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::respond * @covers ::transmit
* @covers ::outputBody * @covers ::outputBody
*/ */
public function testOutputsBody() public function testOutputsBody()
@ -116,10 +116,10 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
$this->body->isReadable()->willReturn(true); $this->body->isReadable()->willReturn(true);
$this->body->__toString()->willReturn($content); $this->body->__toString()->willReturn($content);
$responder = new Responder(); $transmitter = new Transmitter();
ob_start(); ob_start();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$captured = ob_get_contents(); $captured = ob_get_contents();
ob_end_clean(); ob_end_clean();
@ -127,7 +127,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @covers ::respond * @covers ::transmit
* @covers ::setChunkSize * @covers ::setChunkSize
* @covers ::outputBody * @covers ::outputBody
*/ */
@ -152,11 +152,11 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
} }
); );
$responder = new Responder(); $transmitter = new Transmitter();
$responder->setChunkSize($chunkSize); $transmitter->setChunkSize($chunkSize);
ob_start(); ob_start();
$responder->respond($this->request->reveal(), $this->response->reveal(), $chunkSize); $transmitter->transmit($this->request->reveal(), $this->response->reveal(), $chunkSize);
$captured = ob_get_contents(); $captured = ob_get_contents();
ob_end_clean(); ob_end_clean();
@ -164,7 +164,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @cover ::prepareResponse * @covers ::prepareResponse
*/ */
public function testAddContentLengthHeader() public function testAddContentLengthHeader()
{ {
@ -176,13 +176,13 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
$this->body->__toString()->willReturn(""); $this->body->__toString()->willReturn("");
$this->body->getSize()->willReturn($bodySize); $this->body->getSize()->willReturn($bodySize);
$responder = new Responder(); $transmitter = new Transmitter();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->response->withHeader("Content-length", $bodySize)->shouldHaveBeenCalled(); $this->response->withHeader("Content-length", $bodySize)->shouldHaveBeenCalled();
} }
/** /**
* @cover ::prepareResponse * @covers ::prepareResponse
*/ */
public function testReplacesBodyForHeadRequeset() public function testReplacesBodyForHeadRequeset()
{ {
@ -192,8 +192,8 @@ class ResponderTest extends \PHPUnit_Framework_TestCase
$this->body->isReadable()->willReturn(true); $this->body->isReadable()->willReturn(true);
$this->body->__toString()->willReturn(""); $this->body->__toString()->willReturn("");
$responder = new Responder(); $transmitter = new Transmitter();
$responder->respond($this->request->reveal(), $this->response->reveal()); $transmitter->transmit($this->request->reveal(), $this->response->reveal());
$this->response->withBody(Argument::any())->shouldHaveBeenCalled(); $this->response->withBody(Argument::any())->shouldHaveBeenCalled();
} }
} }