7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Config
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once dirname(__FILE__
) . '/../TestHelper.php';
31 require_once 'Zend/Config.php';
35 * @package Zend_Config
36 * @subpackage UnitTests
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_ConfigTest
extends PHPUnit_Framework_TestCase
43 protected $_iniFileConfig;
44 protected $_iniFileNested;
46 public function setUp()
48 // Arrays representing common config configurations
53 'host' => '127.0.0.1',
65 $this->_numericData
= array(
70 $this->_menuData1
= array(
87 $this->_leadingdot
= array('.test' => 'dot-test');
88 $this->_invalidkey
= array(' ' => 'test', ''=>'test2');
92 public function testLoadSingleSection()
94 $config = new Zend_Config($this->_all
, false);
96 $this->assertEquals('all', $config->hostname
);
97 $this->assertEquals('live', $config->db
->name
);
98 $this->assertEquals('multi', $config->one
->two
->three
);
99 $this->assertNull($config->nonexistent
); // property doesn't exist
102 public function testIsset()
104 if (version_compare(PHP_VERSION
, '5.1', '>=')) {
105 $config = new Zend_Config($this->_all
, false);
107 $this->assertFalse(isset($config->notarealkey
));
108 $this->assertTrue(isset($config->hostname
)); // top level
109 $this->assertTrue(isset($config->db
->name
)); // one level down
113 public function testModification()
115 $config = new Zend_Config($this->_all
, true);
117 // overwrite an existing key
118 $this->assertEquals('thisname', $config->name
);
119 $config->name
= 'anothername';
120 $this->assertEquals('anothername', $config->name
);
122 // overwrite an existing multi-level key
123 $this->assertEquals('multi', $config->one
->two
->three
);
124 $config->one
->two
->three
= 'anothername';
125 $this->assertEquals('anothername', $config->one
->two
->three
);
127 // create a new multi-level key
128 $config->does
= array('not'=> array('exist' => 'yet'));
129 $this->assertEquals('yet', $config->does
->not
->exist
);
133 public function testNoModifications()
135 $config = new Zend_Config($this->_all
);
137 $config->hostname
= 'test';
138 } catch (Zend_Config_Exception
$expected) {
139 $this->assertContains('is read only', $expected->getMessage());
142 $this->fail('An expected Zend_Config_Exception has not been raised');
145 public function testNoNestedModifications()
147 $config = new Zend_Config($this->_all
);
149 $config->db
->host
= 'test';
150 } catch (Zend_Config_Exception
$expected) {
151 $this->assertContains('is read only', $expected->getMessage());
154 $this->fail('An expected Zend_Config_Exception has not been raised');
157 public function testNumericKeys()
159 $data = new Zend_Config($this->_numericData
);
160 $this->assertEquals('test', $data->{1});
161 $this->assertEquals(34, $data->{0});
164 public function testCount()
166 $data = new Zend_Config($this->_menuData1
);
167 $this->assertEquals(3, count($data->button
));
170 public function testIterator()
173 $config = new Zend_Config($this->_all
);
175 foreach ($config as $key=>$value) {
176 if (is_string($value)) {
177 $var .= "\nkey = $key, value = $value";
180 $this->assertContains('key = name, value = thisname', $var);
184 foreach ($config->db
as $key=>$value) {
185 $var .= "\nkey = $key, value = $value";
187 $this->assertContains('key = host, value = 127.0.0.1', $var);
190 $config = new Zend_Config($this->_menuData1
);
192 foreach ($config->button
->b1
as $key=>$value) {
193 $var .= "\nkey = $key, value = $value";
195 $this->assertContains('key = L1, value = button1-1', $var);
198 public function testArray()
200 $config = new Zend_Config($this->_all
);
203 print_r($config->toArray());
204 $contents = ob_get_contents();
207 $this->assertContains('Array', $contents);
208 $this->assertContains('[hostname] => all', $contents);
209 $this->assertContains('[user] => username', $contents);
212 public function testErrorWriteToReadOnly()
214 $config = new Zend_Config($this->_all
);
216 $config->test
= '32';
217 } catch (Zend_Config_Exception
$expected) {
218 $this->assertContains('read only', $expected->getMessage());
222 $this->fail('An expected Zend_Config_Exception has not been raised');
225 public function testZF343()
227 $config_array = array(
231 'type' => 'checkbox',
232 'attribs' => array(), // empty array
236 $form_config = new Zend_Config($config_array, true);
237 $this->assertSame(array(), $form_config->controls
->visible
->attribs
->toArray());
240 public function testZF402()
242 $configArray = array(
243 'data1' => 'someValue',
244 'data2' => 'someValue',
246 'data3' => 'someValue'
248 $config = new Zend_Config($configArray);
249 $this->assertTrue(count($config) === count($configArray));
251 foreach ($config as $key => $value) {
252 if ($key === 'false1') {
253 $this->assertTrue($value === false);
255 $this->assertTrue($value === 'someValue');
259 $this->assertTrue($count === 4);
262 public function testZf1019_HandlingInvalidKeyNames()
264 $config = new Zend_Config($this->_leadingdot
);
265 $array = $config->toArray();
266 $this->assertContains('dot-test', $array['.test']);
269 public function testZF1019_EmptyKeys()
271 $config = new Zend_Config($this->_invalidkey
);
272 $array = $config->toArray();
273 $this->assertContains('test', $array[' ']);
274 $this->assertContains('test', $array['']);
277 public function testZF1417_DefaultValues()
279 $config = new Zend_Config($this->_all
);
280 $value = $config->get('notthere', 'default');
281 $this->assertTrue($value === 'default');
282 $this->assertTrue($config->notThere
=== null);
286 public function testUnsetException()
288 // allow modifications is off - expect an exception
289 $config = new Zend_Config($this->_all
, false);
291 $this->assertTrue(isset($config->hostname
)); // top level
294 unset($config->hostname
);
295 } catch (Zend_Config_Exception
$expected) {
296 $this->assertContains('is read only', $expected->getMessage());
299 $this->fail('Expected read only exception has not been raised.');
301 public function testUnset()
303 // allow modifications is on
304 $config = new Zend_Config($this->_all
, true);
306 $this->assertTrue(isset($config->hostname
));
307 $this->assertTrue(isset($config->db
->name
));
309 unset($config->hostname
);
310 unset($config->db
->name
);
312 $this->assertFalse(isset($config->hostname
));
313 $this->assertFalse(isset($config->db
->name
));
317 public function testMerge()
320 'test_feature' => false,
321 'some_files' => array(
322 'foo'=>'dir/foo.xml',
323 'bar'=>'dir/bar.xml',
327 $stdConfig = new Zend_Config($stdArray, true);
330 'test_feature'=>true,
331 'some_files' => array(
332 'bar' => 'myDir/bar.xml',
333 'baz' => 'myDir/baz.xml',
337 $devConfig = new Zend_Config($devArray);
339 $stdConfig->merge($devConfig);
341 $this->assertTrue($stdConfig->test_feature
);
342 $this->assertEquals('myDir/bar.xml', $stdConfig->some_files
->bar
);
343 $this->assertEquals('myDir/baz.xml', $stdConfig->some_files
->baz
);
344 $this->assertEquals('dir/foo.xml', $stdConfig->some_files
->foo
);
345 $this->assertEquals(456, $stdConfig->{2});
350 * Ensures that toArray() supports objects of types other than Zend_Config
354 public function testToArraySupportsObjects()
357 'a' => new stdClass(),
359 'c' => new stdClass(),
360 'd' => new stdClass()
363 $config = new Zend_Config($configData);
364 $this->assertEquals($config->toArray(), $configData);
365 $this->assertType('stdClass', $config->a
);
366 $this->assertType('stdClass', $config->b
->c
);
367 $this->assertType('stdClass', $config->b
->d
);
371 * ensure that modification is not allowed after calling setReadOnly()
374 public function testSetReadOnly()
379 $config = new Zend_Config($configData, true);
382 $config->setReadOnly();
385 } catch (Zend_Config_Exception
$expected) {
386 $this->assertContains('is read only', $expected->getMessage());
389 $this->fail('Expected read only exception has not been raised.');
392 public function testZF3408_countNotDecreasingOnUnset()
399 $config = new Zend_Config($configData, true);
400 $this->assertEquals(count($config), 3);
402 $this->assertEquals(count($config), 2);
405 public function testZF4107_ensureCloneDoesNotKeepNestedReferences()
407 $parent = new Zend_Config(array('key' => array('nested' => 'parent')), true);
408 $newConfig = clone $parent;
409 $newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
411 $this->assertEquals('override', $newConfig->key
->nested
, '$newConfig is not overridden');
412 $this->assertEquals('parent', $parent->key
->nested
, '$parent has been overridden');
420 public function testMergeHonoursAllowModificationsFlagAtAllLevels()
422 $config = new Zend_Config(array('key' => array('nested' => 'yes'), 'key2'=>'yes'), false);
423 $config2 = new Zend_Config(array(), true);
425 $config2->merge($config);
427 $config2->key2
= 'no';
428 } catch (Zend_Config_Exception
$e) {
429 $this->fail('Unexpected exception at top level has been raised: ' . $e->getMessage());
431 $this->assertEquals('no', $config2->key2
);
434 $config2->key
->nested
= 'no';
435 } catch (Zend_Config_Exception
$e) {
436 $this->fail('Unexpected exception on nested object has been raised: ' . $e->getMessage());
438 $this->assertEquals('no', $config2->key
->nested
);
446 public function testUnsettingFirstElementDuringForeachDoesNotSkipAnElement()
448 $config = new Zend_Config(array(
450 'second' => array(2),
455 foreach ($config as $key => $value)
458 if ($key == 'first') {
459 unset($config->$key); // uses magic Zend_Config::__unset() method
463 $this->assertEquals('first', $keyList[0]);
464 $this->assertEquals('second', $keyList[1]);
465 $this->assertEquals('third', $keyList[2]);
472 public function testUnsettingAMiddleElementDuringForeachDoesNotSkipAnElement()
474 $config = new Zend_Config(array(
476 'second' => array(2),
481 foreach ($config as $key => $value)
484 if ($key == 'second') {
485 unset($config->$key); // uses magic Zend_Config::__unset() method
489 $this->assertEquals('first', $keyList[0]);
490 $this->assertEquals('second', $keyList[1]);
491 $this->assertEquals('third', $keyList[2]);
498 public function testUnsettingLastElementDuringForeachDoesNotSkipAnElement()
500 $config = new Zend_Config(array(
502 'second' => array(2),
507 foreach ($config as $key => $value)
510 if ($key == 'third') {
511 unset($config->$key); // uses magic Zend_Config::__unset() method
515 $this->assertEquals('first', $keyList[0]);
516 $this->assertEquals('second', $keyList[1]);
517 $this->assertEquals('third', $keyList[2]);
524 public function testSetReadOnlyAppliesToChildren()
526 $config = new Zend_Config($this->_all
, true);
528 $config->setReadOnly();
529 $this->assertTrue($config->readOnly());
530 $this->assertTrue($config->one
->readOnly(), 'First level children are writable');
531 $this->assertTrue($config->one
->two
->readOnly(), 'Second level children are writable');
534 public function testZF6995_toArrayDoesNotDisturbInternalIterator()
536 $config = new Zend_Config(range(1,10));
538 $this->assertEquals(1, $config->current());
541 $this->assertEquals(1, $config->current());