MDL-84343 phpunit: handle warnings on failing tests

Signed-off-by: Simey Lameze <[email protected]>
This commit is contained in:
Simey Lameze
2025-02-04 19:00:29 +08:00
parent 1bd9705200
commit 635522dc3c
4 changed files with 27 additions and 16 deletions
+3
View File
@@ -290,7 +290,10 @@ final class helper_test extends \advanced_testcase {
$generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
$generator->create_preset(['name' => 'Preset 1']);
$invokable = self::get_invokable();
set_error_handler($invokable, E_WARNING);
$presetid = helper::change_default_preset($preset);
restore_error_handler();
if (empty($settings) && empty($plugins)) {
// The preset hasn't been applied.
+3
View File
@@ -438,7 +438,10 @@ final class manager_test extends \advanced_testcase {
// Call the method to be tested.
$manager = new manager();
try {
$invokable = self::get_invokable();
set_error_handler($invokable, E_WARNING);
list($xml, $preset, $settingsfound, $pluginsfound) = $manager->import_preset($filecontents);
restore_error_handler();
} catch (\exception $e) {
if ($expectedexception) {
$this->assertInstanceOf($expectedexception, $e);
@@ -81,6 +81,9 @@ final class import_test extends \advanced_testcase {
$action = new import();
$sink = $this->redirectEvents();
try {
// Suppress warnings and load XML.
$invokable = self::get_invokable();
set_error_handler($invokable, E_WARNING);
$action->execute();
} catch (\exception $e) {
// If import action was successfull, redirect should be called so we will encounter an
@@ -90,6 +93,7 @@ final class import_test extends \advanced_testcase {
} else {
$this->assertInstanceOf(\moodle_exception::class, $e);
}
restore_error_handler();
} finally {
if ($expecteddebugging) {
$this->assertDebuggingCalled();
+17 -16
View File
@@ -627,7 +627,12 @@ final class advanced_test extends \advanced_testcase {
$this->assertSame('99', $CFG->timezone);
$this->assertSame('Europe/Prague', date_default_timezone_get());
// Catch warning for invalid 'xxx' timezone.
set_error_handler(function ($errno, $errstr): void {
$this->assertStringContainsString('Unknown or bad timezone', $errstr);
}, E_WARNING);
$this->setTimezone('xxx', 'Europe/Prague');
restore_error_handler();
$this->assertSame('xxx', $CFG->timezone);
$this->assertSame('Europe/Prague', date_default_timezone_get());
@@ -635,22 +640,18 @@ final class advanced_test extends \advanced_testcase {
$this->assertSame('Australia/Perth', $CFG->timezone);
$this->assertSame('Australia/Perth', date_default_timezone_get());
try {
$this->setTimezone('Pacific/Auckland', '');
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
try {
$this->setTimezone('Pacific/Auckland', 'xxxx');
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
try {
$this->setTimezone('Pacific/Auckland', null);
} catch (\Exception $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
// Catch warnings for other invalid cases.
$invalidtimezones = ['', 'xxxx', null];
foreach ($invalidtimezones as $invalidtz) {
set_error_handler(function ($errno, $errstr): void {
$this->assertStringContainsString('Unknown or bad timezone', $errstr);
}, E_WARNING);
try {
$this->setTimezone('Pacific/Auckland', $invalidtz);
} catch (\Throwable $e) {
$this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
}
restore_error_handler();
}
}