/standard/tags/release-0.9.0/tests/Zend/ConfigTest.php

https://github.com/bhaumik25/zend-framework · PHP · 262 lines · 202 code · 37 blank · 23 comment · 4 complexity · 9517225edcac4fd818680a2b1974ea8d MD5 · raw file

  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Config
  5. * @subpackage UnitTests
  6. */
  7. /**
  8. * Zend_Config
  9. */
  10. require_once 'Zend/Config.php';
  11. /**
  12. * PHPUnit test case
  13. */
  14. require_once 'PHPUnit/Framework/TestCase.php';
  15. /**
  16. * @category Zend
  17. * @package Zend_Config
  18. * @subpackage UnitTests
  19. */
  20. class Zend_ConfigTest extends PHPUnit_Framework_TestCase
  21. {
  22. protected $_iniFileConfig;
  23. protected $_iniFileNested;
  24. public function setUp()
  25. {
  26. // Arrays representing common config configurations
  27. $this->_all = array(
  28. 'hostname' => 'all',
  29. 'name' => 'thisname',
  30. 'db' => array(
  31. 'host' => '127.0.0.1',
  32. 'user' => 'username',
  33. 'pass' => 'password',
  34. 'name' => 'live'
  35. ),
  36. 'one' => array(
  37. 'two' => array(
  38. 'three' => 'multi'
  39. )
  40. )
  41. );
  42. $this->_numericData = array(
  43. 0 => 34,
  44. 1 => 'test',
  45. );
  46. $this->_menuData1 = array(
  47. 'button' => array(
  48. 'b0' => array(
  49. 'L1' => 'button0-1',
  50. 'L2' => 'button0-2',
  51. 'L3' => 'button0-3'
  52. ),
  53. 'b1' => array(
  54. 'L1' => 'button1-1',
  55. 'L2' => 'button1-2'
  56. ),
  57. 'b2' => array(
  58. 'L1' => 'button2-1'
  59. )
  60. )
  61. );
  62. $this->_leadingdot = array('.test' => 'dot-test');
  63. $this->_invalidkey = array(' ' => 'test', ''=>'test2');
  64. }
  65. public function testLoadSingleSection()
  66. {
  67. $config = new Zend_Config($this->_all, false);
  68. $this->assertEquals('all', $config->hostname);
  69. $this->assertEquals('live', $config->db->name);
  70. $this->assertEquals('multi', $config->one->two->three);
  71. $this->assertNull($config->nonexistent); // property doesn't exist
  72. }
  73. public function testIsset()
  74. {
  75. if (version_compare(PHP_VERSION, '5.1', '>=')) {
  76. $config = new Zend_Config($this->_all, false);
  77. $this->assertFalse(isset($config->notarealkey));
  78. $this->assertTrue(isset($config->hostname)); // top level
  79. $this->assertTrue(isset($config->db->name)); // one level down
  80. }
  81. }
  82. public function testModification()
  83. {
  84. $config = new Zend_Config($this->_all, true);
  85. // overwrite an existing key
  86. $this->assertEquals('thisname', $config->name);
  87. $config->name = 'anothername';
  88. $this->assertEquals('anothername', $config->name);
  89. // overwrite an existing multi-level key
  90. $this->assertEquals('multi', $config->one->two->three);
  91. $config->one->two->three = 'anothername';
  92. $this->assertEquals('anothername', $config->one->two->three);
  93. // create a new multi-level key
  94. $config->does = array('not'=> array('exist' => 'yet'));
  95. $this->assertEquals('yet', $config->does->not->exist);
  96. }
  97. public function testNoModifications()
  98. {
  99. $config = new Zend_Config($this->_all);
  100. try {
  101. $config->hostname = 'test';
  102. } catch (Zend_Config_Exception $expected) {
  103. $this->assertContains('is read only', $expected->getMessage());
  104. return;
  105. }
  106. $this->fail('An expected Zend_Config_Exception has not been raised');
  107. }
  108. public function testNoNestedModifications()
  109. {
  110. $config = new Zend_Config($this->_all);
  111. try {
  112. $config->db->host = 'test';
  113. } catch (Zend_Config_Exception $expected) {
  114. $this->assertContains('is read only', $expected->getMessage());
  115. return;
  116. }
  117. $this->fail('An expected Zend_Config_Exception has not been raised');
  118. }
  119. public function testNumericKeys()
  120. {
  121. $data = new Zend_Config($this->_numericData);
  122. $this->assertEquals('test', $data->{1});
  123. $this->assertEquals(34, $data->{0});
  124. }
  125. public function testCount()
  126. {
  127. $data = new Zend_Config($this->_menuData1);
  128. $this->assertEquals(3, count($data->button));
  129. }
  130. public function testIterator()
  131. {
  132. // top level
  133. $config = new Zend_Config($this->_all);
  134. $var = '';
  135. foreach ($config as $key=>$value) {
  136. if (is_string($value)) {
  137. $var .= "\nkey = $key, value = $value";
  138. }
  139. }
  140. $this->assertContains('key = name, value = thisname', $var);
  141. // 1 nest
  142. $var = '';
  143. foreach ($config->db as $key=>$value) {
  144. $var .= "\nkey = $key, value = $value";
  145. }
  146. $this->assertContains('key = host, value = 127.0.0.1', $var);
  147. // 2 nests
  148. $config = new Zend_Config($this->_menuData1);
  149. $var = '';
  150. foreach ($config->button->b1 as $key=>$value) {
  151. $var .= "\nkey = $key, value = $value";
  152. }
  153. $this->assertContains('key = L1, value = button1-1', $var);
  154. }
  155. public function testArray()
  156. {
  157. $config = new Zend_Config($this->_all);
  158. ob_start();
  159. print_r($config->asArray());
  160. $contents = ob_get_contents();
  161. ob_end_clean();
  162. $this->assertContains('Array', $contents);
  163. $this->assertContains('[hostname] => all', $contents);
  164. $this->assertContains('[user] => username', $contents);
  165. }
  166. public function testErrorWriteToReadOnly()
  167. {
  168. $config = new Zend_Config($this->_all);
  169. try {
  170. $config->test = '32';
  171. } catch (Zend_Config_Exception $expected) {
  172. $this->assertContains('read only', $expected->getMessage());
  173. return;
  174. }
  175. $this->fail('An expected Zend_Config_Exception has not been raised');
  176. }
  177. public function testZF343()
  178. {
  179. $config_array = array(
  180. 'controls' => array(
  181. 'visible' => array(
  182. 'name' => 'visible',
  183. 'type' => 'checkbox',
  184. 'attribs' => array(), // empty array
  185. ),
  186. ),
  187. );
  188. $form_config = new Zend_Config($config_array, true);
  189. $this->assertSame(array(), $form_config->controls->visible->attribs->asArray());
  190. }
  191. public function testZF402()
  192. {
  193. $configArray = array(
  194. 'data1' => 'someValue',
  195. 'data2' => 'someValue',
  196. 'false1' => false,
  197. 'data3' => 'someValue'
  198. );
  199. $config = new Zend_Config($configArray);
  200. $this->assertTrue(count($config) === count($configArray));
  201. $count = 0;
  202. foreach ($config as $key => $value) {
  203. if ($key === 'false1') {
  204. $this->assertTrue($value === false);
  205. } else {
  206. $this->assertTrue($value === 'someValue');
  207. }
  208. $count++;
  209. }
  210. $this->assertTrue($count === 4);
  211. }
  212. public function testZf1019_HandlingInvalidKeyNames()
  213. {
  214. $config = new Zend_Config($this->_leadingdot);
  215. $array = $config->asArray();
  216. $this->assertContains('dot-test', $array['.test']);
  217. }
  218. public function testZF1019_EmptyKeys()
  219. {
  220. $config = new Zend_Config($this->_invalidkey);
  221. $array = $config->asArray();
  222. $this->assertContains('test', $array[' ']);
  223. $this->assertContains('test', $array['']);
  224. }
  225. }