Issue #2210521 by mitrpaka, vlad.n, Ema.jar, donquixote, dawehner, klausi, alexpott: Settings::getInstance() should throw an exception if called before being initialized
parent
3e58b4981c
commit
e4893d4b1b
|
@ -24,7 +24,7 @@ final class Settings {
|
|||
*
|
||||
* @var \Drupal\Core\Site\Settings
|
||||
*/
|
||||
private static $instance;
|
||||
private static $instance = NULL;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
@ -44,8 +44,14 @@ final class Settings {
|
|||
* available.
|
||||
*
|
||||
* @return \Drupal\Core\Site\Settings
|
||||
*
|
||||
* @throws \BadMethodCallException
|
||||
* Thrown when the settings instance has not been initialized yet.
|
||||
*/
|
||||
public static function getInstance() {
|
||||
if (self::$instance === NULL) {
|
||||
throw new \BadMethodCallException('Settings::$instance is not initialized yet. Whatever you are trying to do, it might be too early for that. You could call Settings::initialize(), but it is probably better to wait until it is called in the regular way. Also check for recursions.');
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
|
|
|
@ -126,4 +126,21 @@ class SettingsTest extends UnitTestCase {
|
|||
$this->assertNotEquals($settings::getApcuPrefix('cache_test', '/test/a'), $settings::getApcuPrefix('cache_test', '/test/b'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that an exception is thrown when settings are not initialized yet.
|
||||
*
|
||||
* @covers ::getInstance
|
||||
*/
|
||||
public function testGetInstanceReflection() {
|
||||
$settings = new Settings(array());
|
||||
|
||||
$class = new \ReflectionClass(Settings::class);
|
||||
$instace_property = $class->getProperty("instance");
|
||||
$instace_property->setAccessible(TRUE);
|
||||
$instace_property->setValue(NULL);
|
||||
|
||||
$this->setExpectedException(\BadMethodCallException::class);
|
||||
$settings->getInstance();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue