Adding enclosures now treats type and length parameters as optional for Atom, but...
[zend.git] / tests / Zend / ConfigTest.php
blob1ff0884ba41d5cee1155302d9baab9b96885a5fb
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
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
20 * @version $Id$
23 /**
24 * Test helper
26 require_once dirname(__FILE__) . '/../TestHelper.php';
28 /**
29 * Zend_Config
31 require_once 'Zend/Config.php';
33 /**
34 * @category Zend
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
39 * @group Zend_Config
41 class Zend_ConfigTest extends PHPUnit_Framework_TestCase
43 protected $_iniFileConfig;
44 protected $_iniFileNested;
46 public function setUp()
48 // Arrays representing common config configurations
49 $this->_all = array(
50 'hostname' => 'all',
51 'name' => 'thisname',
52 'db' => array(
53 'host' => '127.0.0.1',
54 'user' => 'username',
55 'pass' => 'password',
56 'name' => 'live'
58 'one' => array(
59 'two' => array(
60 'three' => 'multi'
65 $this->_numericData = array(
66 0 => 34,
67 1 => 'test',
70 $this->_menuData1 = array(
71 'button' => array(
72 'b0' => array(
73 'L1' => 'button0-1',
74 'L2' => 'button0-2',
75 'L3' => 'button0-3'
77 'b1' => array(
78 'L1' => 'button1-1',
79 'L2' => 'button1-2'
81 'b2' => array(
82 'L1' => 'button2-1'
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);
136 try {
137 $config->hostname = 'test';
138 } catch (Zend_Config_Exception $expected) {
139 $this->assertContains('is read only', $expected->getMessage());
140 return;
142 $this->fail('An expected Zend_Config_Exception has not been raised');
145 public function testNoNestedModifications()
147 $config = new Zend_Config($this->_all);
148 try {
149 $config->db->host = 'test';
150 } catch (Zend_Config_Exception $expected) {
151 $this->assertContains('is read only', $expected->getMessage());
152 return;
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()
172 // top level
173 $config = new Zend_Config($this->_all);
174 $var = '';
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);
182 // 1 nest
183 $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);
189 // 2 nests
190 $config = new Zend_Config($this->_menuData1);
191 $var = '';
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);
202 ob_start();
203 print_r($config->toArray());
204 $contents = ob_get_contents();
205 ob_end_clean();
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);
215 try {
216 $config->test = '32';
217 } catch (Zend_Config_Exception $expected) {
218 $this->assertContains('read only', $expected->getMessage());
219 return;
222 $this->fail('An expected Zend_Config_Exception has not been raised');
225 public function testZF343()
227 $config_array = array(
228 'controls' => array(
229 'visible' => array(
230 'name' => 'visible',
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',
245 'false1' => false,
246 'data3' => 'someValue'
248 $config = new Zend_Config($configArray);
249 $this->assertTrue(count($config) === count($configArray));
250 $count = 0;
251 foreach ($config as $key => $value) {
252 if ($key === 'false1') {
253 $this->assertTrue($value === false);
254 } else {
255 $this->assertTrue($value === 'someValue');
257 $count++;
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
293 try {
294 unset($config->hostname);
295 } catch (Zend_Config_Exception $expected) {
296 $this->assertContains('is read only', $expected->getMessage());
297 return;
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()
319 $stdArray = array(
320 'test_feature' => false,
321 'some_files' => array(
322 'foo'=>'dir/foo.xml',
323 'bar'=>'dir/bar.xml',
325 2 => 123,
327 $stdConfig = new Zend_Config($stdArray, true);
329 $devArray = array(
330 'test_feature'=>true,
331 'some_files' => array(
332 'bar' => 'myDir/bar.xml',
333 'baz' => 'myDir/baz.xml',
335 2 => 456,
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
352 * @return void
354 public function testToArraySupportsObjects()
356 $configData = array(
357 'a' => new stdClass(),
358 'b' => array(
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()
376 $configData = array(
377 'a' => 'a'
379 $config = new Zend_Config($configData, true);
380 $config->b = 'b';
382 $config->setReadOnly();
383 try {
384 $config->c = 'c';
385 } catch (Zend_Config_Exception $expected) {
386 $this->assertContains('is read only', $expected->getMessage());
387 return;
389 $this->fail('Expected read only exception has not been raised.');
392 public function testZF3408_countNotDecreasingOnUnset()
394 $configData = array(
395 'a' => 'a',
396 'b' => 'b',
397 'c' => 'c',
399 $config = new Zend_Config($configData, true);
400 $this->assertEquals(count($config), 3);
401 unset($config->b);
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');
417 * @group ZF-3575
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);
426 try {
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);
433 try {
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);
443 * @group ZF-5771a
446 public function testUnsettingFirstElementDuringForeachDoesNotSkipAnElement()
448 $config = new Zend_Config(array(
449 'first' => array(1),
450 'second' => array(2),
451 'third' => array(3)
452 ), true);
454 $keyList = array();
455 foreach ($config as $key => $value)
457 $keyList[] = $key;
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]);
469 * @group ZF-5771
472 public function testUnsettingAMiddleElementDuringForeachDoesNotSkipAnElement()
474 $config = new Zend_Config(array(
475 'first' => array(1),
476 'second' => array(2),
477 'third' => array(3)
478 ), true);
480 $keyList = array();
481 foreach ($config as $key => $value)
483 $keyList[] = $key;
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]);
495 * @group ZF-5771
498 public function testUnsettingLastElementDuringForeachDoesNotSkipAnElement()
500 $config = new Zend_Config(array(
501 'first' => array(1),
502 'second' => array(2),
503 'third' => array(3)
504 ), true);
506 $keyList = array();
507 foreach ($config as $key => $value)
509 $keyList[] = $key;
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]);
521 * @group ZF-4728
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));
537 $config->rewind();
538 $this->assertEquals(1, $config->current());
540 $config->toArray();
541 $this->assertEquals(1, $config->current());