diff --git a/.upgradenotes/MDL-66903-2024070502035383.yml b/.upgradenotes/MDL-66903-2024070502035383.yml index 255dd885601..0c749fbef70 100644 --- a/.upgradenotes/MDL-66903-2024070502035383.yml +++ b/.upgradenotes/MDL-66903-2024070502035383.yml @@ -7,4 +7,14 @@ notes: namespace from the `[path/to/component]/tests/classes` directory. type: improved + - message: > + Added a helper to load fixtures from a components `tests/fixtures/` + folder: + + ```php + + advanced_testcase::load_fixture(string $component, string $fixture): void; + + ``` + type: improved diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index fed9d37ba26..e87be8c0565 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -828,4 +828,38 @@ abstract class advanced_testcase extends base_testcase { $mockedcomponent->setStaticPropertyValue('plugins', $plugins); $this->resetDebugging(); } + + /** + * 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); + } }