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-04-13 14:40:07 +08:00
parent eaa25cf51b
commit 3d51aed3e2
387 changed files with 1442 additions and 1404 deletions
+36 -18
View File
@@ -228,7 +228,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);
@@ -245,7 +245,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;
@@ -259,7 +259,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;
@@ -622,28 +622,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}";
}
/**
@@ -747,6 +750,24 @@ abstract class advanced_testcase extends base_testcase {
$tasks->close();
}
/**
* Convenience method to get the path to a fixture.
*
* @param string $component
* @param string $path
* @throws coding_exception
*/
protected static function get_fixture_path(
string $component,
string $path
): string {
return sprintf(
"%s/tests/fixtures/%s",
\core_component::get_component_directory($component),
$path,
);
}
/**
* Convenience method to load a fixture from a component's fixture directory.
*
@@ -758,15 +779,6 @@ abstract class advanced_testcase extends base_testcase {
string $component,
string $path
): void {
$fullpath = 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");
}
global $ADMIN;
global $CFG;
global $DB;
@@ -778,6 +790,12 @@ abstract class advanced_testcase extends base_testcase {
global $COURSE;
global $SITE;
$fullpath = static::get_fixture_path($component, $path);
if (!file_exists($fullpath)) {
throw new \coding_exception("Fixture file not found: $fullpath");
}
require_once($fullpath);
}
}