Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

unit tests: MDL-22175 Further improvements unit test failure display.

Exceptions are now also shown with a pruned stack trace.
And 2.0 code for displaying PHP5 error types backported to 1.9.
  • Loading branch information...
commit 7e3078db0abd92231741d27ee43d858efb54e3cb 1 parent a6fbcfa
@timhunt timhunt authored
View
66 admin/report/unittest/ex_reporter.php
@@ -73,13 +73,30 @@ function paintFail($message) {
* Called when an error (uncaught exception or PHP error) needs to be output.
*/
function paintError($message) {
- // Explicitly call grandparent, not parent::paintFail.
+ // Explicitly call grandparent, not parent::paintError.
SimpleScorer::paintError($message);
$this->_paintPassFail('exception', $message);
}
/**
- * Private method. Used by printPass/Fail/Error.
+ * Called when a caught exception needs to be output.
+ */
+ function paintException($exception) {
+ // Explicitly call grandparent, not parent::paintException.
+ SimpleScorer::paintException($exception);
+ $message = 'Unexpected exception of type [' . get_class($exception) .
+ '] with message ['. $exception->getMessage() .
+ '] in ['. $exception->getFile() .
+ ' line ' . $exception->getLine() . ']';
+ $stacktrace = null;
+ if (method_exists($exception, 'getTrace')) {
+ $stacktrace = $exception->getTrace();
+ }
+ $this->_paintPassFail('exception', $message, $stacktrace);
+ }
+
+ /**
+ * Private method. Used by printPass/Fail/Error/Exception.
*/
function _paintPassFail($passorfail, $message, $stacktrace = null) {
global $FULLME, $CFG;
@@ -115,8 +132,8 @@ function _paintPassFail($passorfail, $message, $stacktrace = null) {
$dotsadded = true;
}
}
- if ($interestinglines > 1) {
- echo print_object($filteredstacktrace);
+ if ($interestinglines > 1 || $passorfail == 'exception') {
+ echo '<div class="notifytiny">' . format_backtrace($filteredstacktrace) . "</div>\n\n";
}
}
print_simple_box_end();
@@ -212,4 +229,45 @@ function get_string($identifier, $a = NULL) {
return get_string($identifier, 'simpletest', $a);
}
}
+
+/**
+ * Formats a backtrace ready for output.
+ *
+ * @param array $callers backtrace array, as returned by debug_backtrace().
+ * @param boolean $plaintext if false, generates HTML, if true generates plain text.
+ * @return string formatted backtrace, ready for output.
+ */
+function format_backtrace($callers, $plaintext = false) {
+ // do not use $CFG->dirroot because it might not be available in destructors
+ $dirroot = dirname(dirname(__FILE__));
+
+ if (empty($callers)) {
+ return '';
+ }
+
+ $from = $plaintext ? '' : '<ul style="text-align: left">';
+ foreach ($callers as $caller) {
+ if (!isset($caller['line'])) {
+ $caller['line'] = '?'; // probably call_user_func()
+ }
+ if (!isset($caller['file'])) {
+ $caller['file'] = 'unknownfile'; // probably call_user_func()
+ }
+ $from .= $plaintext ? '* ' : '<li>';
+ $from .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']);
+ if (isset($caller['function'])) {
+ $from .= ': call to ';
+ if (isset($caller['class'])) {
+ $from .= $caller['class'] . $caller['type'];
+ }
+ $from .= $caller['function'] . '()';
+ } else if (isset($caller['exception'])) {
+ $from .= ': '.$caller['exception'].' thrown';
+ }
+ $from .= $plaintext ? "\n" : '</li>';
+ }
+ $from .= $plaintext ? '' : '</ul>';
+
+ return $from;
+}
?>
View
8 lib/simpletestlib/errors.php
@@ -210,6 +210,14 @@ function getSeverityAsString($severity) {
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE');
+ // Moodle additions, to prevent notices in PHP5.
+ if (defined('E_RECOVERABLE_ERROR')) {
+ $map[E_RECOVERABLE_ERROR] = 'E_RECOVERABLE_ERROR';
+ }
+ if (defined('E_DEPRECATED')) {
+ $map[E_DEPRECATED] = 'E_DEPRECATED';
+ }
+ // End Moodle additions.
return $map[$severity];
}
}
View
1  lib/simpletestlib/readme_moodle.txt
@@ -2,6 +2,7 @@ Description of Simpletest 1.0.1beta library import into Moodle
Changes:
* test_case.php - added our global $CFG before include() MDL-10064
+ * errors.php - added extra PHP5 error types - otherwise you sometimes get PHP notices when runnging the tests.
skodak
Please sign in to comment.
Something went wrong with that request. Please try again.