ConfigTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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-2009 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-2009 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. public function testZF4107_ensureCloneDoesNotKeepNestedReferences()
  353. {
  354. $parent = new Zend_Config(array('key' => array('nested' => 'parent')), true);
  355. $newConfig = clone $parent;
  356. $newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
  357. $this->assertEquals('override', $newConfig->key->nested, '$newConfig is not overridden');
  358. $this->assertEquals('parent', $parent->key->nested, '$parent has been overridden');
  359. }
  360. /**
  361. * @group ZF-3575
  362. *
  363. */
  364. public function testMergeHonoursAllowModificationsFlagAtAllLevels()
  365. {
  366. $config = new Zend_Config(array('key' => array('nested' => 'yes'), 'key2'=>'yes'), false);
  367. $config2 = new Zend_Config(array(), true);
  368. $config2->merge($config);
  369. try {
  370. $config2->key2 = 'no';
  371. } catch (Zend_Config_Exception $e) {
  372. $this->fail('Unexpected exception at top level has been raised: ' . $e->getMessage());
  373. }
  374. $this->assertEquals('no', $config2->key2);
  375. try {
  376. $config2->key->nested = 'no';
  377. } catch (Zend_Config_Exception $e) {
  378. $this->fail('Unexpected exception on nested object has been raised: ' . $e->getMessage());
  379. }
  380. $this->assertEquals('no', $config2->key->nested);
  381. }
  382. /**
  383. * @group ZF-5771a
  384. *
  385. */
  386. public function testUnsettingFirstElementDuringForeachDoesNotSkipAnElement()
  387. {
  388. $config = new Zend_Config(array(
  389. 'first' => array(1),
  390. 'second' => array(2),
  391. 'third' => array(3)
  392. ), true);
  393. $keyList = array();
  394. foreach ($config as $key => $value)
  395. {
  396. $keyList[] = $key;
  397. if ($key == 'first') {
  398. unset($config->$key); // uses magic Zend_Config::__unset() method
  399. }
  400. }
  401. $this->assertEquals('first', $keyList[0]);
  402. $this->assertEquals('second', $keyList[1]);
  403. $this->assertEquals('third', $keyList[2]);
  404. }
  405. /**
  406. * @group ZF-5771
  407. *
  408. */
  409. public function testUnsettingAMiddleElementDuringForeachDoesNotSkipAnElement()
  410. {
  411. $config = new Zend_Config(array(
  412. 'first' => array(1),
  413. 'second' => array(2),
  414. 'third' => array(3)
  415. ), true);
  416. $keyList = array();
  417. foreach ($config as $key => $value)
  418. {
  419. $keyList[] = $key;
  420. if ($key == 'second') {
  421. unset($config->$key); // uses magic Zend_Config::__unset() method
  422. }
  423. }
  424. $this->assertEquals('first', $keyList[0]);
  425. $this->assertEquals('second', $keyList[1]);
  426. $this->assertEquals('third', $keyList[2]);
  427. }
  428. /**
  429. * @group ZF-5771
  430. *
  431. */
  432. public function testUnsettingLastElementDuringForeachDoesNotSkipAnElement()
  433. {
  434. $config = new Zend_Config(array(
  435. 'first' => array(1),
  436. 'second' => array(2),
  437. 'third' => array(3)
  438. ), true);
  439. $keyList = array();
  440. foreach ($config as $key => $value)
  441. {
  442. $keyList[] = $key;
  443. if ($key == 'third') {
  444. unset($config->$key); // uses magic Zend_Config::__unset() method
  445. }
  446. }
  447. $this->assertEquals('first', $keyList[0]);
  448. $this->assertEquals('second', $keyList[1]);
  449. $this->assertEquals('third', $keyList[2]);
  450. }
  451. /**
  452. * @group ZF-4728
  453. *
  454. */
  455. public function testSetReadOnlyAppliesToChildren()
  456. {
  457. $config = new Zend_Config($this->_all, true);
  458. $config->setReadOnly();
  459. $this->assertTrue($config->readOnly());
  460. $this->assertTrue($config->one->readOnly(), 'First level children are writable');
  461. $this->assertTrue($config->one->two->readOnly(), 'Second level children are writable');
  462. }
  463. public function testZF6995_toArrayDoesNotDisturbInternalIterator()
  464. {
  465. $config = new Zend_Config(range(1,10));
  466. $config->rewind();
  467. $this->assertEquals(1, $config->current());
  468. $config->toArray();
  469. $this->assertEquals(1, $config->current());
  470. }
  471. }