From 39d46917bb5ab8122bd8cf34406009f10c72dd49 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 8 Feb 2024 10:24:17 +0800 Subject: [PATCH] MDL-66903 testing: Helper to load fixture files --- lib/phpunit/classes/advanced_testcase.php | 34 +++++++++++++++++++++++ lib/upgrade.txt | 1 + 2 files changed, 35 insertions(+) diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index 000e07a4c31..2357072cb44 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); + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index fb0f2996c71..4ad47cbd7dd 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -6,6 +6,7 @@ information provided here is intended especially for developers. * The `\core\dataformat::get_format_instance` method is now public, and can be used to retrieve a writer instance for a given dataformat * 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.4.1 ===