MDL-83468 phpunit: Fix various tests after phpunit 10

This commit is contained in:
Andrew Nicols
2025-01-20 16:47:55 +01:00
committed by Sara Arjona
parent b95a8c6ecc
commit bc75ef8bbc
51 changed files with 717 additions and 659 deletions
+16 -29
View File
@@ -20,6 +20,8 @@ use core\http_client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
/**
* Advanced PHPUnit test case customised for Moodle.
@@ -46,21 +48,16 @@ abstract class advanced_testcase extends base_testcase {
* Note: use setUp() or setUpBeforeClass() in your test cases.
*
* @param string $name
* @param array $data
* @param string $dataName
*/
final public function __construct($name = null, array $data = [], $dataname = '') {
parent::__construct($name, $data, $dataname);
final public function __construct(string $name = null) {
parent::__construct($name);
$this->setBackupGlobals(false);
$this->setBackupStaticAttributes(false);
$this->setPreserveGlobalState(false);
}
/**
* Runs the bare test sequence.
*/
final public function runBare(): void {
#[Before]
final public function setup_test_environment(): void {
global $CFG, $DB;
if (phpunit_util::$lastdbwrites != $DB->perf_get_writes()) {
@@ -71,28 +68,18 @@ abstract class advanced_testcase extends base_testcase {
$this->testdbtransaction = $DB->start_delegated_transaction();
}
try {
$this->setCurrentTimeStart();
parent::runBare();
} catch (Exception $ex) {
$e = $ex;
} catch (Throwable $ex) {
// Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
$e = $ex;
} finally {
// Reset global state after test and test failure.
$CFG = phpunit_util::get_global_backup('CFG');
$DB = phpunit_util::get_global_backup('DB');
$this->setCurrentTimeStart();
}
// We need to reset the autoloader.
\core_component::reset();
}
#[After]
final public function teardown_test_environment(): void {
global $CFG, $DB;
// Reset global state after test and test failure.
$CFG = phpunit_util::get_global_backup('CFG');
$DB = phpunit_util::get_global_backup('DB');
if (isset($e)) {
// Cleanup after failed expectation.
self::resetAllData();
throw $e;
}
// We need to reset the autoloader.
\core_component::reset();
// Deal with any debugging messages.
$debugerror = phpunit_util::display_debugging_messages(true);
+36 -1
View File
@@ -1,5 +1,4 @@
<?php
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -25,6 +24,7 @@ use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
/**
* Base class for PHPUnit test cases customised for Moodle
@@ -605,7 +605,42 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
* @return int
*/
protected static function getInvocationCount(InvocationOrder $counter): int {
if (method_exists($counter, 'numberOfInvocations')) {
return $counter->numberOfInvocations();
}
return $counter->getInvocationCount();
}
// phpcs:enable
/**
* Get an invokable object for testing.
*
* This is a helper method to create an invokable object for testing which can be used to
* track invocations, including arguments provided.
*
* This can be useful for modifications to the error handler.
*
* @return object
*/
protected static function get_invokable() {
return new class {
private array $invocations = [];
public function __invoke(...$args) {
$this->invocations[] = $args;
}
public function get_invocations(): array {
return $this->invocations;
}
public function get_invocation_count(): int {
return count($this->invocations);
}
public function reset(): void {
$this->invocations = [];
}
};
}
}
+6 -39
View File
@@ -1,4 +1,5 @@
<?php
use PHPUnit\Framework\Attributes\After;
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -14,16 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Basic test case.
*
* @package core
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* The simplest PHPUnit test case customised for Moodle
*
@@ -35,50 +26,26 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class basic_testcase extends base_testcase {
/**
* Constructs a test case with the given name.
*
* Note: use setUp() or setUpBeforeClass() in your test cases.
*
* @param string $name
* @param array $data
* @param string $dataName
*/
final public function __construct($name = null, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
final public function __construct($name = null) {
parent::__construct($name);
$this->setBackupGlobals(false);
$this->setBackupStaticAttributes(false);
$this->setRunTestInSeparateProcess(false);
}
/**
* Runs the bare test sequence and log any changes in global state or database.
* @return void
*/
final public function runBare(): void {
#[After]
final public function test_teardown(): void {
global $DB;
try {
parent::runBare();
} catch (Exception $ex) {
$e = $ex;
} catch (Throwable $ex) {
// Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
$e = $ex;
}
if (isset($e)) {
// cleanup after failed expectation
phpunit_util::reset_all_data();
throw $e;
}
if ($DB->is_transaction_started()) {
phpunit_util::reset_all_data();
throw new coding_exception('basic_testcase '.$this->getName().' is not supposed to use database transactions!');
throw new coding_exception('basic_testcase ' . $this->getName() . ' is not supposed to use database transactions!');
}
phpunit_util::reset_all_data(true);
@@ -1,4 +1,6 @@
<?php
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -56,14 +58,11 @@ abstract class database_driver_testcase extends base_testcase {
* Constructs a test case with the given name.
*
* @param string $name
* @param array $data
* @param string $dataName
*/
final public function __construct($name = null, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
final public function __construct($name = null) {
parent::__construct($name);
$this->setBackupGlobals(false);
$this->setBackupStaticAttributes(false);
$this->setRunTestInSeparateProcess(false);
}
@@ -101,9 +100,9 @@ abstract class database_driver_testcase extends base_testcase {
self::$extradb = $d;
}
protected function setUp(): void {
#[Before]
protected function setup_extradb(): void {
global $DB;
parent::setUp();
if (self::$extradb) {
$this->tdb = self::$extradb;
@@ -112,7 +111,8 @@ abstract class database_driver_testcase extends base_testcase {
}
}
protected function tearDown(): void {
#[After]
protected function teardown_extradb(): void {
// delete all test tables
$dbman = $this->tdb->get_manager();
$tables = $this->tdb->get_tables(false);
@@ -122,7 +122,6 @@ abstract class database_driver_testcase extends base_testcase {
$dbman->drop_table($table);
}
}
parent::tearDown();
}
public static function tearDownAfterClass(): void {
@@ -134,34 +133,13 @@ abstract class database_driver_testcase extends base_testcase {
parent::tearDownAfterClass();
}
/**
* Runs the bare test sequence.
* @return void
*/
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) {
// Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
$e = $ex;
}
if (isset($e)) {
if ($this->tdb->is_transaction_started()) {
$this->tdb->force_transaction_rollback();
}
$this->tearDown();
throw $e;
#[After]
public function check_debugging(): void {
// 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);
}
}
@@ -0,0 +1,27 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_phpunit\exception;
/**
* An exception thrown when an error is encountered in the setup, teardown, or other non-test parts of a unit test.
*
* @package core_testing
* @copyright Andrew Lyons <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_exception extends \core\exception\coding_exception {
}
+1 -1
View File
@@ -314,7 +314,7 @@ class phpunit_util extends testing_util {
if ($warnings) {
$warnings = implode("\n", $warnings);
trigger_error($warnings, E_USER_WARNING);
throw new \core_phpunit\exception\test_exception($warnings);
}
}