Skip to content

Commit

Permalink
Offer better feedback when 500 errors occur.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 24, 2015
1 parent a0273c2 commit abd3c37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Extensions/IntegrationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,19 @@ protected function assertPageLoaded($uri)
$status = $this->statusCode();

try {
$this->assertEquals(200, $status);
} catch (PHPUnitException $e) {
$message = "A GET request to '{$uri}' failed. Got a {$status} code instead.";

$this->assertEquals(200, $status, $message);
} catch (PHPUnitException $e) {
$this->logLatestContent();

throw $e;
if ($status == 500) {
if (method_exists($this, 'handleInternalError')) {
$this->handleInternalError($message);
}
}

throw new PHPUnitException($message);
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/Extensions/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laracasts\Integrated\Extensions;

use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Form;
use Laracasts\Integrated\Emulator;
Expand Down Expand Up @@ -119,4 +120,28 @@ protected function statusCode()
{
return $this->response->getStatusCode();
}

/**
* Provide additional messaging for 500 errors.
*
* @param string|null $message
* @throws PHPUnitException
* @return void
*/
protected function handleInternalError($message = null)
{
$crawler = new Crawler($this->content(), $this->currentPage);

// A little weird, but we need to parse the output HTML to
// figure out the specifics of where the error occurred.
// There might be an easier way to figure this out.

$crawler = $crawler->filter('.exception_title');
$exception = $crawler->filter('abbr')->html();
$location = $crawler->filter('a')->extract('title')[0];

$message .= "\n\n{$exception} on {$location}";

throw new PHPUnitException($message);
}
}

0 comments on commit abd3c37

Please sign in to comment.