Skip to content

Commit

Permalink
allow null response from routes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Apr 4, 2020
1 parent c59ee40 commit b96d660
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Strategy/ResponseTypeStrategyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ public function setResponseHandler(string $type, $responseHandler): void
/**
* Handle route response.
*
* @param ResponseInterface|ResponseType|string $dispatchedResponse
* @param ResponseInterface|ResponseType|string|null $dispatchedResponse
*
* @return ResponseInterface
*/
protected function handleResponse($dispatchedResponse): ResponseInterface
{
if ($dispatchedResponse === null) {
return $this->responseFactory->createResponse();
}

if (\is_string($dispatchedResponse)) {
$response = $this->responseFactory->createResponse();
$response->getBody()->write($dispatchedResponse);
Expand Down
30 changes: 30 additions & 0 deletions tests/Routing/Strategy/ResponseTypeStrategyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@
*/
class ResponseTypeStrategyTraitTest extends TestCase
{
public function testNullDispatch(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)
->getMock();
/* @var ContainerInterface $container */
$request = $this->getMockBuilder(ServerRequestInterface::class)
->getMock();
/* @var ServerRequestInterface $request */
$response = $this->getMockBuilder(ResponseInterface::class)
->getMock();
/* @var ResponseInterface $response */

$strategy = new ResponseTypeStrategyStub([], new ResponseFactory(), $container);

$callback = function (
ServerRequestInterface $receivedRequest,
ResponseInterface $receivedResponse
) use (
$request,
$response
): void {
static::assertSame($request, $receivedRequest);
static::assertSame($response, $receivedResponse);
};

$return = $strategy($callback, $request, $response, []);

static::assertEquals('', (string) $return->getBody());
}

public function testStringDispatch(): void
{
$container = $this->getMockBuilder(ContainerInterface::class)
Expand Down

0 comments on commit b96d660

Please sign in to comment.