MDL-79843 core: Fully reset the component cache for some tests

This commit is contained in:
Andrew Nicols
2025-01-29 14:39:50 +08:00
committed by Jake Dallimore
parent 54ed733f98
commit 2d9c877f58
3 changed files with 61 additions and 4 deletions
+25 -1
View File
@@ -486,9 +486,33 @@ class component {
* Note: It should not be necessary to call this in regular code.
* Please only use it where strictly required.
*/
public static function reset(): void {
public static function reset(
bool $fullreset = false,
): void {
// The autoloader will re-initialise if plugintypes is null.
self::$plugintypes = null;
// Reset all caches to ensure they are reloaded.
self::$plugins = null;
self::$subsystems = null;
self::$parents = null;
self::$subplugins = null;
self::$deprecatedplugins = null;
self::$deletedplugins = null;
self::$deprecatedplugintypes = null;
self::$deletedplugintypes = null;
self::$deprecatedsubplugins = null;
self::$deletedsubplugins = null;
self::$apis = null;
self::$classmap = null;
self::$classmaprenames = null;
self::$filemap = null;
if ($fullreset) {
self::$componentsource = null;
self::$version = null;
self::$supportsubplugins = ['mod', 'editor', 'tool', 'local'];
}
}
/**
+36 -1
View File
@@ -31,6 +31,37 @@ namespace core\tests;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait fake_plugins_test_trait {
/** @var bool Whether to fully reset the component cache after each test */
protected bool $fullcomponentreset = false;
/**
* Reset the component cache.
*
* This is an After test method that supplements the test tearDown to ensure the component cache is reset.
*
* Please note that the component cache is always partially reset in the main tearDown method.
* This method will extend the reset to also cause re-reads off disk.
*/
#[\PHPUnit\Framework\Attributes\After]
public function reset_component_cache(): void {
if ($this->fullcomponentreset === true) {
\core\component::reset(true);
}
$this->fullcomponentreset = false;
}
/**
* Call this method to force the component cache to be fully reset after each test.
*
* Normally the component cache is reset, but only partially, to speed up the tests.
* This method will cause the reset to also cause re-reads off disk.
*
* @param bool $reset Whether to fully reset the component cache after each test
*/
protected function fully_reset_component_after_test(bool $reset = true): void {
$this->fullcomponentreset = $reset;
}
/**
* Add a mocked plugintype at the component sources level, allowing \core\component to pick up the type and its plugins.
*
@@ -50,8 +81,8 @@ trait fake_plugins_test_trait {
string $path,
bool $subpluginsupport = false
): void {
require_phpunit_isolation();
$this->fully_reset_component_after_test(true);
// Inject the plugintype into the mock component sources. This will be picked up during \core\component::init().
$mockedcomponent = new \ReflectionClass(\core\component::class);
@@ -118,6 +149,8 @@ trait fake_plugins_test_trait {
protected function deprecate_full_mocked_plugintype(
string $plugintype,
): void {
$this->fully_reset_component_after_test(true);
$mockedcomponent = new \ReflectionClass(\core\component::class);
$componentsource = $mockedcomponent->getStaticPropertyValue('componentsource');
$deprecatedkey = 'deprecatedplugintypes';
@@ -151,6 +184,8 @@ trait fake_plugins_test_trait {
protected function delete_full_mocked_plugintype(
string $plugintype,
): void {
$this->fully_reset_component_after_test(true);
\core\component::classloader(\core\component::class);
$mockedcomponent = new \ReflectionClass(\core\component::class);
$componentsource = $mockedcomponent->getStaticPropertyValue('componentsource');
-2
View File
@@ -33,14 +33,12 @@ use ReflectionProperty;
* @covers \core\component
*/
final class component_test extends \advanced_testcase {
use fake_plugins_test_trait;
#[\Override]
public function tearDown(): void {
parent::tearDown();
component::reset();
ini_set('error_log', null);
}