diff --git a/src/Strategy/ResponseTypeStrategyTrait.php b/src/Strategy/ResponseTypeStrategyTrait.php index ace6eb2..087bf89 100644 --- a/src/Strategy/ResponseTypeStrategyTrait.php +++ b/src/Strategy/ResponseTypeStrategyTrait.php @@ -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); diff --git a/tests/Routing/Strategy/ResponseTypeStrategyTraitTest.php b/tests/Routing/Strategy/ResponseTypeStrategyTraitTest.php index 5016391..30fa8dd 100644 --- a/tests/Routing/Strategy/ResponseTypeStrategyTraitTest.php +++ b/tests/Routing/Strategy/ResponseTypeStrategyTraitTest.php @@ -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)