From ef151737947ae56372285f31e3e0f091f5a80170 Mon Sep 17 00:00:00 2001 From: Russell Smith Date: Tue, 11 Oct 2016 16:33:26 +1100 Subject: [PATCH] MDL-56354 unittests: Put debug messages in the failure notice. --- lib/phpunit/classes/advanced_testcase.php | 21 +++++++---- lib/phpunit/classes/util.php | 45 +++++++++++------------ lib/phpunit/tests/advanced_test.php | 10 +++++ 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index 6a9964bc649..ee05a32506b 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -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); } diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index bae7dd4673c..d91528cf55c 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -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; } diff --git a/lib/phpunit/tests/advanced_test.php b/lib/phpunit/tests/advanced_test.php index ebd4a556dae..344a5bd2307 100644 --- a/lib/phpunit/tests/advanced_test.php +++ b/lib/phpunit/tests/advanced_test.php @@ -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;