Skip to content

Commit

Permalink
MDL-80844 phpunit: Only some tests can use the debugging sink
Browse files Browse the repository at this point in the history
The only tests that have COMPLETE* support for the debugging sink
are the advanced_testcase and the database_driver ones (store and
report). So we must ensure that the rest of tests don't use the
debugging sink at all.

Right now we are using it for storing, but later there is not
reporting, so any debugging happening within non advanced tests
is not detected.

This commit just ensures that we stop making that storing for
non advanced/database_driver tests. Nothing more, nothing less.

* Note that we have had to add a few missing bits to the
database_driver testcase because it was not 100% complete. Now
it behaves 100% the same than the advanced_testcase one regarding
the debugging sink.
  • Loading branch information
stronk7 committed Feb 15, 2024
1 parent d3ad77e commit 6e2c5b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/phpunit/classes/database_driver_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ public function runBare(): void {
try {
parent::runBare();

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

} catch (Exception $ex) {
$e = $ex;
} catch (Throwable $ex) {
Expand Down
23 changes: 22 additions & 1 deletion lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,29 @@ public static function debugging_triggered($message, $level, $from) {
// we need normal debugging outside of tests to find problems in our phpunit integration.
$backtrace = debug_backtrace();

// Only for advanced_testcase, database_driver_testcase (and descendants). Others aren't
// able to manage the debugging sink, so any debugging has to be output normally and, hopefully,
// PHPUnit execution will catch that unexpected output properly.
$sinksupport = false;
foreach ($backtrace as $bt) {
if (isset($bt['object']) and is_object($bt['object'])
if (isset($bt['object']) && is_object($bt['object'])
&& (
$bt['object'] instanceof advanced_testcase ||
$bt['object'] instanceof database_driver_testcase)
) {
$sinksupport = true;
break;
}
}
if (!$sinksupport) {
return false;
}

// Verify that we are inside a PHPUnit test (little bit redundant, because
// we already have checked above that this is an advanced/database_driver
// testcase, but let's keep things double safe for now).
foreach ($backtrace as $bt) {
if (isset($bt['object']) && is_object($bt['object'])
&& $bt['object'] instanceof PHPUnit\Framework\TestCase) {
$debug = new stdClass();
$debug->message = $message;
Expand Down

0 comments on commit 6e2c5b7

Please sign in to comment.