MDL-66903 core: Introduce \core\component::resetto replace hacks

Traditionally we have done this using reflection and it would be better
to do this in a 'proper' fashion in case we ever need to change how it
works.
This commit is contained in:
Andrew Nicols
2024-07-15 13:07:42 +08:00
parent aa55779ba0
commit 0270c90dc0
3 changed files with 17 additions and 7 deletions
+11 -1
View File
@@ -283,7 +283,6 @@ class component {
return $file;
}
/**
* Initialise caches, always call before accessing self:: caches.
*/
@@ -398,6 +397,17 @@ class component {
}
}
/**
* Reset the initialisation of the component utility.
*
* Note: It should not be necessary to call this in regular code.
* Please only use it where strictly required.
*/
public static function reset(): void {
// The autoloader will re-initialise if plugintypes is null.
self::$plugintypes = null;
}
/**
* Check whether the cache content in the supplied cache is valid.
*
+3 -2
View File
@@ -79,8 +79,9 @@ abstract class advanced_testcase extends base_testcase {
// Reset global state after test and test failure.
$CFG = phpunit_util::get_global_backup('CFG');
$DB = phpunit_util::get_global_backup('DB');
// This is _hacky_. We need to reset the autoloader, and this is the only way to do so right now.
(new ReflectionProperty(\core\component::class, 'plugintypes'))->setValue(null, null);
// We need to reset the autoloader.
\core_component::reset();
}
if (isset($e)) {
+3 -4
View File
@@ -33,10 +33,9 @@ use ReflectionProperty;
final class component_test extends \advanced_testcase {
#[\Override]
public function tearDown(): void {
$plugintypes = new ReflectionProperty(component::class, 'plugintypes');
$plugintypes->setValue(null, null);
parent::tearDown();
component::reset();
}
/**
@@ -963,7 +962,7 @@ final class component_test extends \advanced_testcase {
// We have to override the dirroot and libdir, and then reset the plugintypes property.
$CFG->dirroot = $vfileroot->url();
$CFG->libdir = $vfileroot->url() . '/lib';
(new ReflectionProperty(component::class, 'plugintypes'))->setValue(null, null);
component::reset();
// Existing classes do not break.
$this->assertTrue(