diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index aed50f64227..2d4cea8d670 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -710,4 +710,38 @@ abstract class advanced_testcase extends base_testcase { } $tasks->close(); } + + /** + * Convenience method to load a fixture from a component's fixture directory. + * + * @param string $component + * @param string $path + * @throws coding_exception + */ + protected static function load_fixture( + 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; + global $SITE; + global $USER; + global $OUTPUT; + global $PAGE; + global $SESSION; + global $COURSE; + global $SITE; + + require_once($fullpath); + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index acb52e2369a..c35f76add92 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -4,6 +4,7 @@ information provided here is intended especially for developers. === 4.2.9 === * Added the ability for unit tests to autoload classes in the `\[component]\tests\` namespace from the `[path/to/component]/tests/classes` directory. +* Added the `advanced_testcase::load_fixture()` helper to load fixtures from a components `tests/fixtures/` folder. === 4.2.7 ===