Skip to content

Commit

Permalink
Modifiy onNotSuccessfullTest method to match the signature of the new…
Browse files Browse the repository at this point in the history
… onNotSuccessfullTest phpunit method and debugResponse method to match the new 500 error Symfony page markup.
  • Loading branch information
petrero committed Jan 16, 2019
1 parent afdddb1 commit e2d133a
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions tests/ApiTestCase.php
Expand Up @@ -64,7 +64,7 @@ public function tearDown(){
}


protected function onNotSuccessfulTest(Exception $e)
protected function onNotSuccessfulTest(\Throwable $t)
{
if ($lastResponse = $this->getLastResponse()) {
$this->printDebug('');
Expand All @@ -75,7 +75,7 @@ protected function onNotSuccessfulTest(Exception $e)
$this->debugResponse($lastResponse);
}

throw $e;
throw $t;
}

private function purgeDatabase(){
Expand All @@ -96,6 +96,7 @@ protected function printLastRequestUrl(){
$this->printDebug('No request was made.');
}
}

protected function debugResponse(ResponseInterface $response)
{
foreach ($response->getHeaders() as $name => $values) {
Expand All @@ -121,39 +122,38 @@ protected function debugResponse(ResponseInterface $response)
if ($isValidHtml) {
$this->printDebug('');
$crawler = new Crawler($body);

// very specific to Symfony's error page
$isError = $crawler->filter('#traces-0')->count() > 0
|| strpos($body, 'looks like something went wrong') !== false;
$isError = $crawler->filter('.trace-line ')->count() > 0
|| strpos($body, 'Symfony Exception') !== false;
if ($isError) {
$this->printDebug('There was an Error!!!!');
$this->printDebug('');
} else {
$this->printDebug('HTML Summary (h1 and h2):');
}

// finds the h1 and h2 tags and prints them only
foreach ($crawler->filter('h1, h2')->extract(array('_text')) as $header) {
// avoid these meaningless headers
if (strpos($header, 'Stack Trace') !== false) {
continue;
}
if (strpos($header, 'Logs') !== false) {
continue;
foreach ($crawler->filter('.exception-message-wrapper h1')->extract(array('_text')) as $text) {
$text = $this->removeLineBreaks($text);
if ($isError) {
$this->printErrorBlock($text);
} else {
$this->printDebug($text);
}
}

// remove line breaks so the message looks nice
$header = str_replace("\n", ' ', trim($header));
// trim any excess whitespace "foo bar" => "foo bar"
$header = preg_replace('/(\s)+/', ' ', $header);

foreach ($crawler
->filter('.trace-line')
->first()
->extract(array('_text')) as $text
){
$text = $this->removeLineBreaks($text);
if ($isError) {
$this->printErrorBlock($header);
$this->printErrorBlock($text);
} else {
$this->printDebug($header);
$this->printDebug($text);
}
}


/*
* When using the test environment, the profiler is not active
* for performance. To help debug, turn it on temporarily in
Expand All @@ -177,6 +177,15 @@ protected function debugResponse(ResponseInterface $response)
}
}

protected function removeLineBreaks($text){
// remove line breaks so the message looks nice
$text = str_replace("\n", ' ', trim($text));
// trim any excess whitespace "foo bar" => "foo bar"
$text = preg_replace('/(\s)+/', ' ', $text);

return $text;
}

/**
* Print a message out - useful for debugging
*
Expand Down

0 comments on commit e2d133a

Please sign in to comment.