Skip to content

Commit

Permalink
Faulty non-Illuminate responses are now handled properly. Close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Jun 26, 2015
1 parent 2143d66 commit 22113ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/ResultFormatter/OptimusResultFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public function formatResult(Response $response) {
private function formatData(Response $response)
{
if (!$response->isSuccessful()) {
$exception = $response instanceof LaravelResponse ?
$response->exception :
/* TODO: */ null;

return $this->formatException($exception);
// Response will have an exception attached
if ($response instanceof LaravelResponse && $response->exception instanceof Exception) {
return $this->formatException($response->exception);
}
}

$content = $response->getContent();
Expand All @@ -49,4 +48,4 @@ private function formatException(Exception $exception)
];
}

}
}
43 changes: 35 additions & 8 deletions tests/ResultFormatter/OptimusResultFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function testNormalResultIsFormattedProperly()

$formatted = $this->formatter->formatResult($response);

$this->assertEquals('stdClass', get_class($formatted));
$this->assertEquals(200, $formatted->statusCode);
$this->assertEquals('<html>', $formatted->data);
$this->assertNormalResponse($formatted);
}

public function testJsonResultIsFormattedProperly()
Expand All @@ -40,9 +38,38 @@ public function testJsonResultIsFormattedProperly()

$formatted = $this->formatter->formatResult($response);

$this->assertEquals('stdClass', get_class($formatted));
$this->assertEquals(200, $formatted->statusCode);
$this->assertEquals('json', $formatted->data->format);
$this->assertJsonResponse($formatted);
}

public function testHandlingNonIlluminateResponseErrorResponses()
{
$response = JsonResponse::create([
'format' => 'json'
], 500);

$formatted = $this->formatter->formatResult($response);

$this->assertJsonResponse($formatted, 500);

$response = Response::create('<html>', 500);

$formatted = $this->formatter->formatResult($response);

$this->assertNormalResponse($formatted, 500);
}

private function assertJsonResponse($response, $statusCode = 200)
{
$this->assertEquals('stdClass', get_class($response));
$this->assertEquals($statusCode, $response->statusCode);
$this->assertEquals('json', $response->data->format);
}

private function assertNormalResponse($response, $statusCode = 200)
{
$this->assertEquals('stdClass', get_class($response));
$this->assertEquals($statusCode, $response->statusCode);
$this->assertEquals('<html>', $response->data);
}

public function testExceptionResultIsFormattedProperly()
Expand All @@ -61,5 +88,5 @@ public function testExceptionResultIsFormattedProperly()
$this->assertEquals("integer", gettype($formatted->data->line));
}
}
}

}

0 comments on commit 22113ca

Please sign in to comment.