/** * Creates a new configuration object for the specified group. When caching * is enabled, Kohana_Config will attempt to load the group from the cache. * * @param string group name * @param boolean cache the group array * @return void */ public function __construct($group, $cache = TRUE) { // Set the configuration group name $this->_configuration_group = $group; if ($cache === FALSE) { // Load the configuration $config = Kohana_Config::load($group); } elseif (($config = Kohana::cache(self::CACHE_PREFIX . $group)) === NULL) { // Load the configuration, it has not been cached $config = Kohana_Config::load($group); // Create a cache of the configuration group Kohana::cache(self::CACHE_PREFIX . $group, $config); } // Load the array using the values as properties ArrayObject::__construct($config, ArrayObject::ARRAY_AS_PROPS); }
/** * Calling load() with a group that doesn't exist, should get it to use the last reader * to create a new config group * * @test * @covers Kohana_Config::load */ public function test_load_returns_new_config_group_if_one_dnx() { $config = new Kohana_Config(); $group = 'my_group'; $reader1 = $this->getMock('Kohana_Config_Reader'); $reader2 = $this->getMock('Kohana_Config_Reader', array('load'), array(), 'Kohana_Config_Waffles'); // This is a slightly hacky way of doing it, but it works $reader2->expects($this->exactly(2))->method('load')->with($group)->will($this->onConsecutiveCalls($this->returnValue(FALSE), $this->returnValue(clone $reader2))); $config->attach($reader1)->attach($reader2); $new_config = $config->load('my_group'); $this->assertInstanceOf('Kohana_Config_Waffles', $new_config); // Slightly taboo, testing a different api!! $this->assertSame(array(), $new_config->as_array()); }
/** * load() should keep a record of what config groups have been requested and if * a group is requested more than once the first instance should be returned * * @test * @covers Config::load */ public function test_load_reuses_config_groups() { $reader = $this->getMock('Kohana_Config_Reader', array('load')); $reader->expects($this->once())->method('load')->with('something')->will($this->returnValue(array())); $config = new Kohana_Config(); $config->attach($reader); $group = $config->load('something'); $this->assertSame($group, $config->load('something')); }