diff --git a/src/Routing/Hook/ContentLengthHook.php b/src/Responder/Middleware/ContentLengthHandler.php similarity index 90% rename from src/Routing/Hook/ContentLengthHook.php rename to src/Responder/Middleware/ContentLengthHandler.php index 3ec892f..7bec7c5 100644 --- a/src/Routing/Hook/ContentLengthHook.php +++ b/src/Responder/Middleware/ContentLengthHandler.php @@ -1,6 +1,6 @@ getStatusLine($response)); diff --git a/src/Responder/ResponderInterface.php b/src/Responder/ResponderInterface.php new file mode 100644 index 0000000..aa32432 --- /dev/null +++ b/src/Responder/ResponderInterface.php @@ -0,0 +1,17 @@ +response->hasHeader("Content-length")->willReturn(false); $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); - $hook = new ContentLengthHook(); + $hook = new ContentLengthHandler(); $response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->assertEquals([1024], $response->getHeader("Content-length")); @@ -53,7 +53,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase $this->response->hasHeader("Content-length")->willReturn(false); $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); - $hook = new ContentLengthHook(); + $hook = new ContentLengthHandler(); $response = $this->response->reveal(); $response = $hook->dispatch($this->request->reveal(), $response, $this->next); @@ -67,7 +67,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase $this->response->hasHeader("Content-length")->willReturn(true); $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); - $hook = new ContentLengthHook(); + $hook = new ContentLengthHandler(); $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); @@ -78,7 +78,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase $this->response->hasHeader("Content-length")->willReturn(false); $this->response->getHeaderLine("Transfer-encoding")->willReturn("CHUNKED"); - $hook = new ContentLengthHook(); + $hook = new ContentLengthHandler(); $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); @@ -90,7 +90,7 @@ class ContentLengthHookTest extends \PHPUnit_Framework_TestCase $this->response->getHeaderLine("Transfer-encoding")->willReturn(""); $this->body->getSize()->willReturn(null); - $hook = new ContentLengthHook(); + $hook = new ContentLengthHandler(); $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->response->withHeader(Argument::cetera())->shouldNotHaveBeenCalled(); diff --git a/test/tests/unit/Routing/Hook/HeadHookTest.php b/test/tests/unit/Responder/Middleware/HeadHandlerTest.php similarity index 73% rename from test/tests/unit/Routing/Hook/HeadHookTest.php rename to test/tests/unit/Responder/Middleware/HeadHandlerTest.php index c80e04a..d3941ee 100644 --- a/test/tests/unit/Routing/Hook/HeadHookTest.php +++ b/test/tests/unit/Responder/Middleware/HeadHandlerTest.php @@ -1,15 +1,16 @@ request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->response->getBody()->willReturn($this->body->reveal()); - $this->response->withBody(Argument::any())->will(function ($args) { - $this->getBody()->willReturn($args[0]); - return $this; - }); + $this->response->withBody(Argument::any())->will( + function ($args) { + $this->getBody()->willReturn($args[0]); + return $this; + } + ); $this->next = function ($request, $response) { return $response; }; @@ -36,7 +39,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase public function testReplacesBodyForHeadRequest() { $this->request->getMethod()->willReturn("HEAD"); - $hook = new HeadHook(); + $hook = new HeadHandler(); $response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->assertSame(0, $response->getBody()->getSize()); } @@ -44,7 +47,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase public function testMultipleDispatchesHaveNoEffect() { $this->request->getMethod()->willReturn("HEAD"); - $hook = new HeadHook(); + $hook = new HeadHandler(); $response = $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $hook->dispatch($this->request->reveal(), $response, $this->next); $this->response->withBody(Argument::any())->shouldHaveBeenCalledTimes(1); @@ -53,7 +56,7 @@ class HeadHookTest extends \PHPUnit_Framework_TestCase public function testDoesNotReplaceBodyForNonHeadRequests() { $this->request->getMethod()->willReturn("GET"); - $hook = new HeadHook(); + $hook = new HeadHandler(); $hook->dispatch($this->request->reveal(), $this->response->reveal(), $this->next); $this->response->withBody(Argument::any())->shouldNotHaveBeenCalled(); } diff --git a/test/tests/unit/Routing/ResponderTest.php b/test/tests/unit/Responder/ResponderTest.php similarity index 70% rename from test/tests/unit/Routing/ResponderTest.php rename to test/tests/unit/Responder/ResponderTest.php index a2d17bc..3301373 100644 --- a/test/tests/unit/Routing/ResponderTest.php +++ b/test/tests/unit/Responder/ResponderTest.php @@ -1,18 +1,20 @@ body = $this->prophesize('\Psr\Http\Message\StreamInterface'); $this->body->isReadable()->willReturn(false); + $this->request = $this->prophesize('\Psr\Http\Message\ServerRequestInterface'); $this->response = $this->prophesize('\Psr\Http\Message\ResponseInterface'); $this->response->getHeaders()->willReturn([]); $this->response->getProtocolVersion()->willReturn("1.1"); @@ -35,7 +38,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase $this->response->getReasonPhrase()->willReturn("Ok"); $responder = new Responder(); - $responder->respond($this->response->reveal()); + $responder->respond($this->request->reveal(), $this->response->reveal()); $this->assertContains("HTTP/1.1 200 Ok", HeaderStack::getHeaders()); } @@ -45,7 +48,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase $this->response->getReasonPhrase()->willReturn(null); $responder = new Responder(); - $responder->respond($this->response->reveal()); + $responder->respond($this->request->reveal(), $this->response->reveal()); $this->assertContains("HTTP/1.1 999", HeaderStack::getHeaders()); } @@ -60,7 +63,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase ]); $responder = new Responder(); - $responder->respond($this->response->reveal()); + $responder->respond($this->request->reveal(), $this->response->reveal()); $this->assertContains($header, HeaderStack::getHeaders()); } @@ -83,7 +86,7 @@ class ResponderTest extends \PHPUnit_Framework_TestCase $responder = new Responder(); ob_start(); - $responder->respond($this->response->reveal()); + $responder->respond($this->request->reveal(), $this->response->reveal()); $captured = ob_get_contents(); ob_end_clean(); @@ -99,21 +102,23 @@ class ResponderTest extends \PHPUnit_Framework_TestCase $this->body->isReadable()->willReturn(true); $this->body->rewind()->willReturn(true); $this->body->eof()->willReturn(false); - $this->body->read(Argument::any())->will(function ($args) use ($content, &$position) { - $chunkSize = $args[0]; - $chunk = substr($content, $position, $chunkSize); - $position += $chunkSize; - if ($position >= strlen($content)) { - $this->eof()->willReturn(true); + $this->body->read(Argument::any())->will( + function ($args) use ($content, &$position) { + $chunkSize = $args[0]; + $chunk = substr($content, $position, $chunkSize); + $position += $chunkSize; + if ($position >= strlen($content)) { + $this->eof()->willReturn(true); + } + return $chunk; } - return $chunk; - }); + ); $responder = new Responder(); $responder->setChunkSize($chunkSize); ob_start(); - $responder->respond($this->response->reveal(), $chunkSize); + $responder->respond($this->request->reveal(), $this->response->reveal(), $chunkSize); $captured = ob_get_contents(); ob_end_clean();