Skip to content
Snippets Groups Projects
Commit 85ff5bf3 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Make it possible to pre-load a configuration array into...

Make it possible to pre-load a configuration array into SimpleSAML_Configuration for subsequent calls to getInstance() to use it. This opens up for unit-testing code that calls SimpleSAML_Configuration::getInstance().
parent 5d1b1421
No related branches found
No related tags found
No related merge requests found
......@@ -214,15 +214,22 @@ class SimpleSAML_Configuration
*
* @param array $config The configuration array.
* @param string $location The location which will be given when an error occurs. Optional.
* @param string|null $instance The name of this instance. If specified, the configuration will be loaded and an
* instance with that name will be kept for it to be retrieved later with getInstance($instance). If null, the
* configuration will not be kept for later use. Defaults to null.
*
* @return SimpleSAML_Configuration The configuration object.
*/
public static function loadFromArray($config, $location = '[ARRAY]')
public static function loadFromArray($config, $location = '[ARRAY]', $instance = null)
{
assert('is_array($config)');
assert('is_string($location)');
return new SimpleSAML_Configuration($config, $location);
$c = new SimpleSAML_Configuration($config, $location);
if ($instance !== null) {
self::$instance[$instance] = $c;
}
return $c;
}
......@@ -245,14 +252,16 @@ class SimpleSAML_Configuration
{
assert('is_string($instancename)');
// check if the instance exists already
if (array_key_exists($instancename, self::$instance)) {
return self::$instance[$instancename];
}
if ($instancename === 'simplesaml') {
return self::getConfig();
}
if (!array_key_exists($instancename, self::$instance)) {
throw new Exception('Configuration with name '.$instancename.' is not initialized.');
}
return self::$instance[$instancename];
throw new Exception('Configuration with name '.$instancename.' is not initialized.');
}
......
......@@ -522,4 +522,35 @@ class Test_SimpleSAML_Configuration extends PHPUnit_Framework_TestCase
$c->getLocalizedString('opt');
}
/**
* Test that the default instance fails to load even if we previously loaded another instance.
* @expectedException Exception
*/
public function testLoadDefaultInstance()
{
SimpleSAML_Configuration::loadFromArray(array('key' => 'value'), '', 'dummy');
$c = SimpleSAML_Configuration::getInstance();
var_dump($c);
}
/**
* Test that Configuration objects can be initialized from an array.
*
* ATTENTION: this test must be kept the last.
*/
public function testLoadInstanceFromArray()
{
$c = array(
'key' => 'value'
);
// test loading a custom instance
SimpleSAML_Configuration::loadFromArray($c, '', 'dummy');
$this->assertEquals('value', SimpleSAML_Configuration::getInstance('dummy')->getValue('key', null));
// test loading the default instance
SimpleSAML_Configuration::loadFromArray($c, '', 'simplesaml');
$this->assertEquals('value', SimpleSAML_Configuration::getInstance()->getValue('key', null));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment