Skip to content

Commit

Permalink
feat: Return ResposneInterface from run in order to make the applicat…
Browse files Browse the repository at this point in the history
…ion works with tools like roadrunner
  • Loading branch information
damianopetrungaro committed Oct 3, 2018
1 parent b87decb commit e1d8666
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,21 @@ public function __construct(
*
* @throws RuntimeException
*/
public function run(MatchablePipelineCollectionInterface $pipelines): void
public function run(MatchablePipelineCollectionInterface $pipelines): ResponseInterface
{
try {
// If a pipeline match print the response and return
if ($handledResponse = $this->handlePipeline($pipelines)) {
$this->sendResponse($handledResponse);

return;
return $handledResponse;
}

// If a route pattern matched but the http verbs was different, print a '405 response'
// If no route pattern matched, print a '404 response'
$statusCode = $this->matchableRequest->isPatternMatched() ? StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED : StatusCodeInterface::STATUS_NOT_FOUND;
$this->sendResponse($this->invalidRequestHandler->__invoke($this->matchableRequest->request(), $this->response->withStatus($statusCode)));

return $this->invalidRequestHandler->__invoke($this->matchableRequest->request(), $this->response->withStatus($statusCode));
} catch (Throwable $e) {
$this->sendResponse($this->errorHandler->__invoke($e, $this->request, $this->response));
return $this->errorHandler->__invoke($e, $this->request, $this->response);
}
}

Expand Down Expand Up @@ -124,7 +123,7 @@ private function handlePipeline(MatchablePipelineCollectionInterface $pipelines)
*
* @throws RuntimeException
*/
protected function sendResponse(ResponseInterface $response): void
public function sendResponse(ResponseInterface $response): void
{
// Send all the headers
\http_response_code($response->getStatusCode());
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testAppStackIsProperlyExecutedAndReturnRepsone()

$app = AppFactory::buildFromContainer($container);
$app->pipe(['PreStageOne', 'PreStageTwo']);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(200, \http_response_code());
}

Expand Down Expand Up @@ -121,7 +121,7 @@ public function testAppStackIsProperlyExecutedAndReturnString()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(200, \http_response_code());
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public function testRunReturnNotFoundResponse()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(404, \http_response_code());
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public function testRunReturnMethodNotAllowedResponse()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(405, \http_response_code());
}

Expand Down Expand Up @@ -267,7 +267,7 @@ public function testRunReturnExceptionResponse()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(500, \http_response_code());
}

Expand Down Expand Up @@ -314,7 +314,7 @@ public function testRunReturnNothingForNotReadableBody()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(404, \http_response_code());
}

Expand Down Expand Up @@ -364,7 +364,7 @@ public function testRunBodyByStreamChunk()
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(404, \http_response_code());
}

Expand Down Expand Up @@ -410,7 +410,7 @@ public function testAllHeaderAreProperlySent($headerToSend, $expectedHeaderSent)
$pipelineCollection = $pipelineCollection->reveal();

$app = AppFactory::buildFromContainer($container);
$app->run($pipelineCollection);
$app->sendResponse($app->run($pipelineCollection));
$this->assertSame(404, \http_response_code());
$this->assertSame($expectedHeaderSent, \xdebug_get_headers());
}
Expand Down

0 comments on commit e1d8666

Please sign in to comment.