PageRenderTime 141ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 1ms

/standard/branches/release-1.5/tests/Zend/ConfigTest.php

https://github.com/bhaumik25/zend-framework
PHP | 403 lines | 301 code | 51 blank | 51 comment | 4 complexity | ebb13d5482eb8f0caa7422a6d810a619 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * Zend_Config
  28. */
  29. require_once 'Zend/Config.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Config
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_ConfigTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_iniFileConfig;
  40. protected $_iniFileNested;
  41. public function setUp()
  42. {
  43. // Arrays representing common config configurations
  44. $this->_all = array(
  45. 'hostname' => 'all',
  46. 'name' => 'thisname',
  47. 'db' => array(
  48. 'host' => '127.0.0.1',
  49. 'user' => 'username',
  50. 'pass' => 'password',
  51. 'name' => 'live'
  52. ),
  53. 'one' => array(
  54. 'two' => array(
  55. 'three' => 'multi'
  56. )
  57. )
  58. );
  59. $this->_numericData = array(
  60. 0 => 34,
  61. 1 => 'test',
  62. );
  63. $this->_menuData1 = array(
  64. 'button' => array(
  65. 'b0' => array(
  66. 'L1' => 'button0-1',
  67. 'L2' => 'button0-2',
  68. 'L3' => 'button0-3'
  69. ),
  70. 'b1' => array(
  71. 'L1' => 'button1-1',
  72. 'L2' => 'button1-2'
  73. ),
  74. 'b2' => array(
  75. 'L1' => 'button2-1'
  76. )
  77. )
  78. );
  79. $this->_leadingdot = array('.test' => 'dot-test');
  80. $this->_invalidkey = array(' ' => 'test', ''=>'test2');
  81. }
  82. public function testLoadSingleSection()
  83. {
  84. $config = new Zend_Config($this->_all, false);
  85. $this->assertEquals('all', $config->hostname);
  86. $this->assertEquals('live', $config->db->name);
  87. $this->assertEquals('multi', $config->one->two->three);
  88. $this->assertNull($config->nonexistent); // property doesn't exist
  89. }
  90. public function testIsset()
  91. {
  92. if (version_compare(PHP_VERSION, '5.1', '>=')) {
  93. $config = new Zend_Config($this->_all, false);
  94. $this->assertFalse(isset($config->notarealkey));
  95. $this->assertTrue(isset($config->hostname)); // top level
  96. $this->assertTrue(isset($config->db->name)); // one level down
  97. }
  98. }
  99. public function testModification()
  100. {
  101. $config = new Zend_Config($this->_all, true);
  102. // overwrite an existing key
  103. $this->assertEquals('thisname', $config->name);
  104. $config->name = 'anothername';
  105. $this->assertEquals('anothername', $config->name);
  106. // overwrite an existing multi-level key
  107. $this->assertEquals('multi', $config->one->two->three);
  108. $config->one->two->three = 'anothername';
  109. $this->assertEquals('anothername', $config->one->two->three);
  110. // create a new multi-level key
  111. $config->does = array('not'=> array('exist' => 'yet'));
  112. $this->assertEquals('yet', $config->does->not->exist);
  113. }
  114. public function testNoModifications()
  115. {
  116. $config = new Zend_Config($this->_all);
  117. try {
  118. $config->hostname = 'test';
  119. } catch (Zend_Config_Exception $expected) {
  120. $this->assertContains('is read only', $expected->getMessage());
  121. return;
  122. }
  123. $this->fail('An expected Zend_Config_Exception has not been raised');
  124. }
  125. public function testNoNestedModifications()
  126. {
  127. $config = new Zend_Config($this->_all);
  128. try {
  129. $config->db->host = 'test';
  130. } catch (Zend_Config_Exception $expected) {
  131. $this->assertContains('is read only', $expected->getMessage());
  132. return;
  133. }
  134. $this->fail('An expected Zend_Config_Exception has not been raised');
  135. }
  136. public function testNumericKeys()
  137. {
  138. $data = new Zend_Config($this->_numericData);
  139. $this->assertEquals('test', $data->{1});
  140. $this->assertEquals(34, $data->{0});
  141. }
  142. public function testCount()
  143. {
  144. $data = new Zend_Config($this->_menuData1);
  145. $this->assertEquals(3, count($data->button));
  146. }
  147. public function testIterator()
  148. {
  149. // top level
  150. $config = new Zend_Config($this->_all);
  151. $var = '';
  152. foreach ($config as $key=>$value) {
  153. if (is_string($value)) {
  154. $var .= "\nkey = $key, value = $value";
  155. }
  156. }
  157. $this->assertContains('key = name, value = thisname', $var);
  158. // 1 nest
  159. $var = '';
  160. foreach ($config->db as $key=>$value) {
  161. $var .= "\nkey = $key, value = $value";
  162. }
  163. $this->assertContains('key = host, value = 127.0.0.1', $var);
  164. // 2 nests
  165. $config = new Zend_Config($this->_menuData1);
  166. $var = '';
  167. foreach ($config->button->b1 as $key=>$value) {
  168. $var .= "\nkey = $key, value = $value";
  169. }
  170. $this->assertContains('key = L1, value = button1-1', $var);
  171. }
  172. public function testArray()
  173. {
  174. $config = new Zend_Config($this->_all);
  175. ob_start();
  176. print_r($config->toArray());
  177. $contents = ob_get_contents();
  178. ob_end_clean();
  179. $this->assertContains('Array', $contents);
  180. $this->assertContains('[hostname] => all', $contents);
  181. $this->assertContains('[user] => username', $contents);
  182. }
  183. public function testErrorWriteToReadOnly()
  184. {
  185. $config = new Zend_Config($this->_all);
  186. try {
  187. $config->test = '32';
  188. } catch (Zend_Config_Exception $expected) {
  189. $this->assertContains('read only', $expected->getMessage());
  190. return;
  191. }
  192. $this->fail('An expected Zend_Config_Exception has not been raised');
  193. }
  194. public function testZF343()
  195. {
  196. $config_array = array(
  197. 'controls' => array(
  198. 'visible' => array(
  199. 'name' => 'visible',
  200. 'type' => 'checkbox',
  201. 'attribs' => array(), // empty array
  202. ),
  203. ),
  204. );
  205. $form_config = new Zend_Config($config_array, true);
  206. $this->assertSame(array(), $form_config->controls->visible->attribs->toArray());
  207. }
  208. public function testZF402()
  209. {
  210. $configArray = array(
  211. 'data1' => 'someValue',
  212. 'data2' => 'someValue',
  213. 'false1' => false,
  214. 'data3' => 'someValue'
  215. );
  216. $config = new Zend_Config($configArray);
  217. $this->assertTrue(count($config) === count($configArray));
  218. $count = 0;
  219. foreach ($config as $key => $value) {
  220. if ($key === 'false1') {
  221. $this->assertTrue($value === false);
  222. } else {
  223. $this->assertTrue($value === 'someValue');
  224. }
  225. $count++;
  226. }
  227. $this->assertTrue($count === 4);
  228. }
  229. public function testZf1019_HandlingInvalidKeyNames()
  230. {
  231. $config = new Zend_Config($this->_leadingdot);
  232. $array = $config->toArray();
  233. $this->assertContains('dot-test', $array['.test']);
  234. }
  235. public function testZF1019_EmptyKeys()
  236. {
  237. $config = new Zend_Config($this->_invalidkey);
  238. $array = $config->toArray();
  239. $this->assertContains('test', $array[' ']);
  240. $this->assertContains('test', $array['']);
  241. }
  242. public function testZF1417_DefaultValues()
  243. {
  244. $config = new Zend_Config($this->_all);
  245. $value = $config->get('notthere', 'default');
  246. $this->assertTrue($value === 'default');
  247. $this->assertTrue($config->notThere === null);
  248. }
  249. public function testUnsetException()
  250. {
  251. // allow modifications is off - expect an exception
  252. $config = new Zend_Config($this->_all, false);
  253. $this->assertTrue(isset($config->hostname)); // top level
  254. try {
  255. unset($config->hostname);
  256. } catch (Zend_Config_Exception $expected) {
  257. $this->assertContains('is read only', $expected->getMessage());
  258. return;
  259. }
  260. $this->fail('Expected read only exception has not been raised.');
  261. }
  262. public function testUnset()
  263. {
  264. // allow modifications is on
  265. $config = new Zend_Config($this->_all, true);
  266. $this->assertTrue(isset($config->hostname));
  267. $this->assertTrue(isset($config->db->name));
  268. unset($config->hostname);
  269. unset($config->db->name);
  270. $this->assertFalse(isset($config->hostname));
  271. $this->assertFalse(isset($config->db->name));
  272. }
  273. public function testMerge()
  274. {
  275. $stdArray = array(
  276. 'test_feature' => false,
  277. 'some_files' => array(
  278. 'foo'=>'dir/foo.xml',
  279. 'bar'=>'dir/bar.xml',
  280. ),
  281. 2 => 123,
  282. );
  283. $stdConfig = new Zend_Config($stdArray, true);
  284. $devArray = array(
  285. 'test_feature'=>true,
  286. 'some_files' => array(
  287. 'bar' => 'myDir/bar.xml',
  288. 'baz' => 'myDir/baz.xml',
  289. ),
  290. 2 => 456,
  291. );
  292. $devConfig = new Zend_Config($devArray);
  293. $stdConfig->merge($devConfig);
  294. $this->assertTrue($stdConfig->test_feature);
  295. $this->assertEquals('myDir/bar.xml', $stdConfig->some_files->bar);
  296. $this->assertEquals('myDir/baz.xml', $stdConfig->some_files->baz);
  297. $this->assertEquals('dir/foo.xml', $stdConfig->some_files->foo);
  298. $this->assertEquals(456, $stdConfig->{2});
  299. }
  300. /**
  301. * Ensures that toArray() supports objects of types other than Zend_Config
  302. *
  303. * @return void
  304. */
  305. public function testToArraySupportsObjects()
  306. {
  307. $configData = array(
  308. 'a' => new stdClass(),
  309. 'b' => array(
  310. 'c' => new stdClass(),
  311. 'd' => new stdClass()
  312. )
  313. );
  314. $config = new Zend_Config($configData);
  315. $this->assertEquals($config->toArray(), $configData);
  316. $this->assertType('stdClass', $config->a);
  317. $this->assertType('stdClass', $config->b->c);
  318. $this->assertType('stdClass', $config->b->d);
  319. }
  320. /**
  321. * ensure that modification is not allowed after calling setReadOnly()
  322. *
  323. */
  324. public function testSetReadOnly()
  325. {
  326. $configData = array(
  327. 'a' => 'a'
  328. );
  329. $config = new Zend_Config($configData, true);
  330. $config->b = 'b';
  331. $config->setReadOnly();
  332. try {
  333. $config->c = 'c';
  334. } catch (Zend_Config_Exception $expected) {
  335. $this->assertContains('is read only', $expected->getMessage());
  336. return;
  337. }
  338. $this->fail('Expected read only exception has not been raised.');
  339. }
  340. public function testZF3408_countNotDecreasingOnUnset()
  341. {
  342. $configData = array(
  343. 'a' => 'a',
  344. 'b' => 'b',
  345. 'c' => 'c',
  346. );
  347. $config = new Zend_Config($configData, true);
  348. $this->assertEquals(count($config), 3);
  349. unset($config->b);
  350. $this->assertEquals(count($config), 2);
  351. }
  352. }