This commit is contained in:
Eloy Lafuente (stronk7) 2016-10-26 02:16:10 +02:00
commit 7c7928b2d8
3 changed files with 45 additions and 31 deletions

View File

@ -82,10 +82,10 @@ abstract class advanced_testcase extends base_testcase {
$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) {
@ -280,6 +280,9 @@ abstract class advanced_testcase extends base_testcase {
*/
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) {
@ -290,12 +293,13 @@ abstract class advanced_testcase extends base_testcase {
}
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);
@ -303,8 +307,6 @@ abstract class advanced_testcase extends base_testcase {
if ($debuglevel !== null) {
$this->assertSame($debuglevel, $debug->level, $message);
}
$this->resetDebugging();
}
/**
@ -322,6 +324,9 @@ abstract class advanced_testcase extends base_testcase {
}
$debugging = $this->getDebuggingMessages();
$message .= "\n".phpunit_util::display_debugging_messages(true);
$this->resetDebugging();
$this->assertEquals($expectedcount, count($debugging), $message);
if ($debugmessages) {
@ -341,8 +346,6 @@ abstract class advanced_testcase extends base_testcase {
$this->assertSame($debuglevel, $debugging[$key]->level, $message);
}
}
$this->resetDebugging();
}
/**
@ -356,6 +359,8 @@ abstract class advanced_testcase extends base_testcase {
if ($message === '') {
$message = 'Expectation failed, debugging() was triggered.';
}
$message .= "\n".phpunit_util::display_debugging_messages(true);
$this->resetDebugging();
$this->assertEquals(0, $count, $message);
}

View File

@ -598,28 +598,19 @@ class phpunit_util extends testing_util {
$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 (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;
return true;
}
}
if (!$intest) {
return false;
}
$debug = new stdClass();
$debug->message = $message;
$debug->level = $level;
$debug->from = $from;
self::$debuggings[] = $debug;
return true;
return false;
}
/**
@ -640,16 +631,24 @@ class phpunit_util extends testing_util {
/**
* 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;
}

View File

@ -71,6 +71,16 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
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;