Skip to content

Commit

Permalink
MDL-56354 unittests: Put debug messages in the failure notice.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-russ committed Oct 25, 2016
1 parent 919b9df commit ef15173
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
21 changes: 13 additions & 8 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ final public function runBare() {
$DB = phpunit_util::get_global_backup('DB');

// Deal with any debugging messages.
$debugerror = phpunit_util::display_debugging_messages();
$debugerror = phpunit_util::display_debugging_messages(true);
$this->resetDebugging();
if ($debugerror) {
trigger_error('Unexpected debugging() call detected.', E_USER_NOTICE);
if (!empty($debugerror)) {
trigger_error('Unexpected debugging() call detected.'."\n".$debugerror, E_USER_NOTICE);
}

} catch (Exception $ex) {
Expand Down Expand Up @@ -280,6 +280,9 @@ public function resetDebugging() {
*/
public function assertDebuggingCalled($debugmessage = null, $debuglevel = null, $message = '') {
$debugging = $this->getDebuggingMessages();
$debugdisplaymessage = "\n".phpunit_util::display_debugging_messages(true);
$this->resetDebugging();

$count = count($debugging);

if ($count == 0) {
Expand All @@ -290,21 +293,20 @@ public function assertDebuggingCalled($debugmessage = null, $debuglevel = null,
}
if ($count > 1) {
if ($message === '') {
$message = 'Expectation failed, debugging() triggered '.$count.' times.';
$message = 'Expectation failed, debugging() triggered '.$count.' times.'.$debugdisplaymessage;
}
$this->fail($message);
}
$this->assertEquals(1, $count);

$message .= $debugdisplaymessage;
$debug = reset($debugging);
if ($debugmessage !== null) {
$this->assertSame($debugmessage, $debug->message, $message);
}
if ($debuglevel !== null) {
$this->assertSame($debuglevel, $debug->level, $message);
}

$this->resetDebugging();
}

/**
Expand All @@ -322,6 +324,9 @@ public function assertDebuggingCalledCount($expectedcount, $debugmessages = arra
}

$debugging = $this->getDebuggingMessages();
$message .= "\n".phpunit_util::display_debugging_messages(true);
$this->resetDebugging();

$this->assertEquals($expectedcount, count($debugging), $message);

if ($debugmessages) {
Expand All @@ -341,8 +346,6 @@ public function assertDebuggingCalledCount($expectedcount, $debugmessages = arra
$this->assertSame($debuglevel, $debugging[$key]->level, $message);
}
}

$this->resetDebugging();
}

/**
Expand All @@ -356,6 +359,8 @@ public function assertDebuggingNotCalled($message = '') {
if ($message === '') {
$message = 'Expectation failed, debugging() was triggered.';
}
$message .= "\n".phpunit_util::display_debugging_messages(true);
$this->resetDebugging();
$this->assertEquals(0, $count, $message);
}

Expand Down
45 changes: 22 additions & 23 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,28 +598,19 @@ public static function debugging_triggered($message, $level, $from) {
$backtrace = debug_backtrace();

foreach ($backtrace as $bt) {
$intest = false;
if (isset($bt['object']) and is_object($bt['object'])) {
if ($bt['object'] instanceof PHPUnit_Framework_TestCase) {
if (strpos($bt['function'], 'test') === 0) {
$intest = true;
break;
}
}
}
}
if (!$intest) {
return false;
}

$debug = new stdClass();
$debug->message = $message;
$debug->level = $level;
$debug->from = $from;
if (isset($bt['object']) and is_object($bt['object'])
&& $bt['object'] instanceof PHPUnit_Framework_TestCase) {
$debug = new stdClass();
$debug->message = $message;
$debug->level = $level;
$debug->from = $from;

self::$debuggings[] = $debug;
self::$debuggings[] = $debug;

return true;
return true;
}
}
return false;
}

/**
Expand All @@ -640,16 +631,24 @@ public static function get_debugging_messages() {

/**
* Prints out any debug messages accumulated during test execution.
* @return bool false if no debug messages, true if debug triggered
*
* @param bool $return true to return the messages or false to print them directly. Default false.
* @return bool|string false if no debug messages, true if debug triggered or string of messages
*/
public static function display_debugging_messages() {
public static function display_debugging_messages($return = false) {
if (empty(self::$debuggings)) {
return false;
}

$debugstring = '';
foreach(self::$debuggings as $debug) {
echo 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
$debugstring .= 'Debugging: ' . $debug->message . "\n" . trim($debug->from) . "\n";
}

if ($return) {
return $debugstring;
}
echo $debugstring;
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public function test_debugging() {
set_debugging(DEBUG_DEVELOPER);
}

/**
* @test
*
* Annotations are a valid PHPUnit method for running tests. Debugging needs to support them.
*/
public function debugging_called_with_annotation() {
debugging('pokus', DEBUG_MINIMAL);
$this->assertDebuggingCalled('pokus', DEBUG_MINIMAL);
}

public function test_set_user() {
global $USER, $DB, $SESSION;

Expand Down

0 comments on commit ef15173

Please sign in to comment.