Skip to content

Commit

Permalink
Changing HtmlReporter to print out testname and stack trace instead o…
Browse files Browse the repository at this point in the history
…f trying to guess where things went wrong.
  • Loading branch information
markstory committed Jul 15, 2010
1 parent 4759b7a commit 71af126
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cake/tests/lib/reporter/cake_base_reporter.php
Expand Up @@ -159,7 +159,7 @@ public function paintResult(PHPUnit_Framework_TestResult $result) {
* @param float $time
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
$this->paintException($e);
$this->paintException($e, $test);
}

/**
Expand All @@ -170,7 +170,7 @@ public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
* @param float $time
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
$this->paintFail($e);
$this->paintFail($e, $test);
}

/**
Expand Down
42 changes: 22 additions & 20 deletions cake/tests/lib/reporter/cake_html_reporter.php
Expand Up @@ -227,29 +227,16 @@ public function paintDocumentEnd() {
* the context of the other tests.
* @return void
*/
public function paintFail($message) {
$context = $message->getTrace();
$realContext = $context[3];
$class = new ReflectionClass($realContext['class']);

$deeper = false;
if ($class->getParentClass()) {
$deeper = $class->getParentClass()->getName() === 'PHPUnit_Framework_TestCase';
}
$deeper = $deeper || !$class->isSubclassOf('PHPUnit_Framework_TestCase');
if ($deeper) {
$realContext = $context[4];
$context = $context[3];
} else {
$context = $context[2];
}
public function paintFail($message, $test) {
$trace = $this->_getStackTrace($message);
$testName = get_class($test) . '(' . $test->getName() . ')';

echo "<li class='fail'>\n";
echo "<span>Failed</span>";
echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString()) . "</pre></div>\n";
echo "<div class='msg'>" . sprintf(__('File: %s'), $context['file']) . "</div>\n";
echo "<div class='msg'>" . sprintf(__('Method: %s'), $realContext['function']) . "</div>\n";
echo "<div class='msg'>" . sprintf(__('Line: %s'), $context['line']) . "</div>\n";

echo "<div class='msg'>" . sprintf(__('Test case: %s'), $testName) . "</div>\n";
echo "<div class='msg'>" . __('Stack trace:') . '<br />' . $trace . "</div>\n";
echo "</li>\n";
}

Expand Down Expand Up @@ -278,7 +265,7 @@ public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
* @param Exception $exception Exception to display.
* @return void
*/
public function paintException($exception) {
public function paintException($exception, $test) {
echo "<li class='fail'>\n";
echo "<span>Exception</span>";
$message = 'Unexpected exception of type [' . get_class($exception) .
Expand Down Expand Up @@ -323,6 +310,21 @@ protected function _htmlEntities($message) {
return htmlentities($message, ENT_COMPAT, $this->_characterSet);
}

/**
* Gets a formatted stack trace.
*
* @param Exception $e Exception to get a stack trace for.
* @return string Generated stack trace.
*/
protected function _getStackTrace(Exception $e) {
$trace = $e->getTrace();
$out = array();
foreach ($trace as $frame) {
$out[] = $frame['file'] . ' : ' . $frame['line'];
}
return implode('<br />', $out);
}

/**
* A test suite started.
*
Expand Down

0 comments on commit 71af126

Please sign in to comment.