diff options
author | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-15 17:21:58 +0200 |
---|---|---|
committer | James D. Forrester <jforrester@wikimedia.org> | 2023-09-12 14:28:18 +0100 |
commit | 34acf667eb659b0b45c45c018885ba30ce238862 (patch) | |
tree | b85799cbcea56d90f5d1d5aee35f8f2554ffaf54 /includes/Settings | |
parent | e54fb98c301a3dfc93714f2f9d452b6d015f5ae0 (diff) | |
download | mediawikicore-34acf667eb659b0b45c45c018885ba30ce238862.tar.gz mediawikicore-34acf667eb659b0b45c45c018885ba30ce238862.zip |
SettingsBuilder: prevent access to the global instance in unit tests
Unit tests must use dependency injection, not global instances.
Change-Id: If7e35a67edc2337657bec4e35724d7f6bc034418
Diffstat (limited to 'includes/Settings')
-rw-r--r-- | includes/Settings/SettingsBuilder.php | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/includes/Settings/SettingsBuilder.php b/includes/Settings/SettingsBuilder.php index a229b2661cf5..9612bbb8cd62 100644 --- a/includes/Settings/SettingsBuilder.php +++ b/includes/Settings/SettingsBuilder.php @@ -21,6 +21,7 @@ use MediaWiki\Settings\Source\FileSource; use MediaWiki\Settings\Source\SettingsFileUtils; use MediaWiki\Settings\Source\SettingsIncludeLocator; use MediaWiki\Settings\Source\SettingsSource; +use RuntimeException; use StatusValue; use function array_key_exists; @@ -131,6 +132,8 @@ class SettingsBuilder { /** @var string[] */ private $warnings = []; + private static bool $accessDisabledForUnitTests = false; + /** * Accessor for the global SettingsBuilder instance. * @@ -143,6 +146,10 @@ class SettingsBuilder { public static function getInstance(): self { static $instance = null; + if ( self::$accessDisabledForUnitTests ) { + throw new RuntimeException( 'Access is disabled in unit tests' ); + } + if ( !$instance ) { // NOTE: SettingsBuilder is used during bootstrap, before MediaWikiServices // is available. It has to be, because it is used to construct the @@ -161,6 +168,26 @@ class SettingsBuilder { } /** + * @internal + */ + public static function disableAccessForUnitTests(): void { + if ( !defined( 'MW_PHPUNIT_TEST' ) ) { + throw new RuntimeException( 'Can only be called in tests' ); + } + self::$accessDisabledForUnitTests = true; + } + + /** + * @internal + */ + public static function enableAccessAfterUnitTests(): void { + if ( !defined( 'MW_PHPUNIT_TEST' ) ) { + throw new RuntimeException( 'Can only be called in tests' ); + } + self::$accessDisabledForUnitTests = false; + } + + /** * @param string $baseDir * @param ExtensionRegistry $extensionRegistry * @param ConfigBuilder $configSink |