MDL-81521 core: Update all possibly data providers to be static

Note: Some data providers could not be automatically be converted to
being static. These will be handled in a separate issue.
This commit is contained in:
Andrew Nicols
2024-11-15 12:50:44 +08:00
parent e1d05182a6
commit c370c0cc5e
407 changed files with 1449 additions and 1430 deletions
+20 -17
View File
@@ -191,7 +191,7 @@ abstract class advanced_testcase extends base_testcase {
* @param array $files full paths to CSV or XML files to load.
* @return phpunit_dataset
*/
protected function dataset_from_files(array $files) {
protected static function dataset_from_files(array $files) {
// We ignore $delimiter, $enclosure and $escape, use the default ones in your fixtures.
$dataset = new phpunit_dataset();
$dataset->from_files($files);
@@ -208,7 +208,7 @@ abstract class advanced_testcase extends base_testcase {
* @param string $table name of the table which the file belongs to (only for CSV files).
* @return phpunit_dataset
*/
protected function dataset_from_string(string $content, string $type, ?string $table = null) {
protected static function dataset_from_string(string $content, string $type, ?string $table = null) {
$dataset = new phpunit_dataset();
$dataset->from_string($content, $type, $table);
return $dataset;
@@ -222,7 +222,7 @@ abstract class advanced_testcase extends base_testcase {
* @param array $data array of tables, see {@see phpunit_dataset::from_array()} for supported formats.
* @return phpunit_dataset
*/
protected function dataset_from_array(array $data) {
protected static function dataset_from_array(array $data) {
$dataset = new phpunit_dataset();
$dataset->from_array($data);
return $dataset;
@@ -607,28 +607,31 @@ abstract class advanced_testcase extends base_testcase {
* @param bool $https true if https required
* @return string url
*/
public function getExternalTestFileUrl($path, $https = false) {
public static function getExternalTestFileUrl(
string $path,
bool $https = false,
): string {
$path = ltrim($path, '/');
if ($path) {
$path = '/' . $path;
$path = "/{$path}";
}
if ($https) {
if (defined('TEST_EXTERNAL_FILES_HTTPS_URL')) {
if (!TEST_EXTERNAL_FILES_HTTPS_URL) {
$this->markTestSkipped('Tests using external https test files are disabled');
self::markTestSkipped('Tests using external https test files are disabled');
}
return TEST_EXTERNAL_FILES_HTTPS_URL . $path;
}
return 'https://download.moodle.org/unittest' . $path;
return "https://download.moodle.org/unittest/{$path}";
}
if (defined('TEST_EXTERNAL_FILES_HTTP_URL')) {
if (!TEST_EXTERNAL_FILES_HTTP_URL) {
$this->markTestSkipped('Tests using external http test files are disabled');
self::markTestSkipped('Tests using external http test files are disabled');
}
return TEST_EXTERNAL_FILES_HTTP_URL . $path;
}
return 'http://download.moodle.org/unittest' . $path;
return "http://download.moodle.org/unittest/{$path}";
}
/**
@@ -845,17 +848,11 @@ abstract class advanced_testcase extends base_testcase {
string $component,
string $path,
): string {
$fullpath = sprintf(
return sprintf(
"%s/tests/fixtures/%s",
\core_component::get_component_directory($component),
$path,
);
if (!file_exists($fullpath)) {
throw new \coding_exception("Fixture file not found: $fullpath");
}
return $fullpath;
}
/**
@@ -880,7 +877,13 @@ abstract class advanced_testcase extends base_testcase {
global $COURSE;
global $SITE;
require_once(static::get_fixture_path($component, $path));
$fullpath = static::get_fixture_path($component, $path);
if (!file_exists($fullpath)) {
throw new \coding_exception("Fixture file not found: $fullpath");
}
require_once($fullpath);
}
/**
+7 -7
View File
@@ -80,7 +80,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_from_file() data provider.
*/
public function from_file_provider() {
public static function from_file_provider(): array {
// Create an unreadable file with vfsStream.
$vfsfile = vfsStream::newFile('unreadable', 0222);
vfsStream::setup('root')->addChild($vfsfile);
@@ -177,7 +177,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_from_string() data provider.
*/
public function from_string_provider() {
public static function from_string_provider(): array {
return [
'wrong type' => [
@@ -267,7 +267,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_from_array() data provider.
*/
public function from_array_provider() {
public static function from_array_provider(): array {
return [
'repeated array table many structures' => [
'structure' => [
@@ -413,7 +413,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_load_csv() data provider.
*/
public function load_csv_provider() {
public static function load_csv_provider(): array {
return [
'repeated csv table many files' => [
@@ -501,7 +501,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_load_xml() data provider.
*/
public function load_xml_provider() {
public static function load_xml_provider(): array {
return [
'repeated xml table multiple files' => [
@@ -669,7 +669,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_to_database() data provider.
*/
public function to_database_provider() {
public static function to_database_provider(): array {
return [
'wrong table requested' => [
@@ -824,7 +824,7 @@ class phpunit_dataset_test extends advanced_testcase {
/**
* test_get_rows() data provider.
*/
public function get_rows_provider() {
public static function get_rows_provider(): array {
return [
'wrong table requested' => [
+1 -1
View File
@@ -36,7 +36,7 @@ class util_test extends \advanced_testcase {
}
}
public function set_table_modified_by_sql_provider() {
public static function set_table_modified_by_sql_provider(): array {
global $DB;
$prefix = $DB->get_prefix();