Merge branch 'MDL-83468-main' of https://github.com/andrewnicols/moodle
This commit is contained in:
@@ -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) {
|
||||
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);
|
||||
@@ -698,8 +685,8 @@ abstract class advanced_testcase extends base_testcase {
|
||||
$params['userid'] = $matchuserid;
|
||||
}
|
||||
|
||||
$lock = $this->createMock(\core\lock\lock::class);
|
||||
$cronlock = $this->createMock(\core\lock\lock::class);
|
||||
$lock = $this->createStub(\core\lock\lock::class);
|
||||
$cronlock = $this->createStub(\core\lock\lock::class);
|
||||
|
||||
$tasks = $DB->get_recordset('task_adhoc', $params);
|
||||
foreach ($tasks as $record) {
|
||||
|
||||
@@ -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,8 @@ 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;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Base class for PHPUnit test cases customised for Moodle
|
||||
@@ -54,12 +55,57 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
|
||||
* @deprecated 3.0
|
||||
*/
|
||||
public static function assertTag($matcher, $actual, $message = '', $ishtml = true) {
|
||||
$dom = (new PHPUnit\Util\Xml\Loader)->load($actual, $ishtml);
|
||||
if ($ishtml) {
|
||||
$dom = self::loadHTML($actual);
|
||||
} else {
|
||||
$dom = (new PHPUnit\Util\Xml\Loader)->load($actual);
|
||||
}
|
||||
$tags = self::findNodes($dom, $matcher, $ishtml);
|
||||
$matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
|
||||
self::assertTrue($matched, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load HTML into a DomDocument.
|
||||
*
|
||||
* Note: THis is a replacement for functionality removed from PHPUnit 10.
|
||||
*
|
||||
* @param string $actual
|
||||
* @throws \PHPUnit\Util\Xml\XmlException
|
||||
* @return DOMDocument
|
||||
*/
|
||||
public static function loadHTML(string $actual): DOMDocument {
|
||||
if ($actual === '') {
|
||||
throw new \PHPUnit\Util\Xml\XmlException('Could not load XML from empty string');
|
||||
}
|
||||
|
||||
$document = new DOMDocument;
|
||||
$document->preserveWhiteSpace = false;
|
||||
|
||||
$internal = libxml_use_internal_errors(true);
|
||||
$message = '';
|
||||
$reporting = error_reporting(0);
|
||||
|
||||
$loaded = $document->loadHTML($actual);
|
||||
|
||||
foreach (libxml_get_errors() as $error) {
|
||||
$message .= "\n" . $error->message;
|
||||
}
|
||||
|
||||
libxml_use_internal_errors($internal);
|
||||
error_reporting($reporting);
|
||||
|
||||
if ($loaded === false) {
|
||||
if ($message === '') {
|
||||
$message = 'Could not load XML for unknown reason';
|
||||
}
|
||||
|
||||
throw new \PHPUnit\Util\Xml\XmlException($message);
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: we are overriding this method to remove the deprecated error
|
||||
* @see https://tracker.moodle.org/browse/MDL-47129
|
||||
@@ -72,7 +118,11 @@ abstract class base_testcase extends PHPUnit\Framework\TestCase {
|
||||
* @deprecated 3.0
|
||||
*/
|
||||
public static function assertNotTag($matcher, $actual, $message = '', $ishtml = true) {
|
||||
$dom = (new PHPUnit\Util\Xml\Loader)->load($actual, $ishtml);
|
||||
if ($ishtml) {
|
||||
$dom = self::loadHTML($actual);
|
||||
} else {
|
||||
$dom = (new PHPUnit\Util\Xml\Loader)->load($actual);
|
||||
}
|
||||
$tags = self::findNodes($dom, $matcher, $ishtml);
|
||||
$matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
|
||||
self::assertFalse($matched, $message);
|
||||
@@ -605,7 +655,56 @@ 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 = [];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the test is running in isolation.
|
||||
*
|
||||
* Note: This was previously a public method of the TestCase, but as removed in PHPUnit 10.
|
||||
* There is no direct replacement, but we can use reflection to access the protected property.
|
||||
* @return bool
|
||||
*/
|
||||
public function isInIsolation(): bool {
|
||||
$rc = new \ReflectionClass(TestCase::class);
|
||||
$rcp = $rc->getProperty('inIsolation');
|
||||
|
||||
return $rcp->getValue($this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,52 +26,38 @@
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the test.
|
||||
*
|
||||
* Replaces the original PHPUnit method.
|
||||
* @return string
|
||||
*/
|
||||
final public function getName(): string {
|
||||
return $this->name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -55,14 +57,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);
|
||||
}
|
||||
|
||||
@@ -100,9 +99,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;
|
||||
@@ -111,7 +110,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);
|
||||
@@ -121,7 +121,6 @@ abstract class database_driver_testcase extends base_testcase {
|
||||
$dbman->drop_table($table);
|
||||
}
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass(): void {
|
||||
@@ -133,34 +132,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 {
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user