ConfigTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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-2010 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. * Zend_Config
  24. */
  25. require_once 'Zend/Config.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Config
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Config
  33. */
  34. class Zend_ConfigTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $_iniFileConfig;
  37. protected $_iniFileNested;
  38. public function setUp()
  39. {
  40. // Arrays representing common config configurations
  41. $this->_all = array(
  42. 'hostname' => 'all',
  43. 'name' => 'thisname',
  44. 'db' => array(
  45. 'host' => '127.0.0.1',
  46. 'user' => 'username',
  47. 'pass' => 'password',
  48. 'name' => 'live'
  49. ),
  50. 'one' => array(
  51. 'two' => array(
  52. 'three' => 'multi'
  53. )
  54. )
  55. );
  56. $this->_numericData = array(
  57. 0 => 34,
  58. 1 => 'test',
  59. );
  60. $this->_menuData1 = array(
  61. 'button' => array(
  62. 'b0' => array(
  63. 'L1' => 'button0-1',
  64. 'L2' => 'button0-2',
  65. 'L3' => 'button0-3'
  66. ),
  67. 'b1' => array(
  68. 'L1' => 'button1-1',
  69. 'L2' => 'button1-2'
  70. ),
  71. 'b2' => array(
  72. 'L1' => 'button2-1'
  73. )
  74. )
  75. );
  76. $this->_leadingdot = array('.test' => 'dot-test');
  77. $this->_invalidkey = array(' ' => 'test', ''=>'test2');
  78. }
  79. public function testLoadSingleSection()
  80. {
  81. $config = new Zend_Config($this->_all, false);
  82. $this->assertEquals('all', $config->hostname);
  83. $this->assertEquals('live', $config->db->name);
  84. $this->assertEquals('multi', $config->one->two->three);
  85. $this->assertNull($config->nonexistent); // property doesn't exist
  86. }
  87. public function testIsset()
  88. {
  89. if (version_compare(PHP_VERSION, '5.1', '>=')) {
  90. $config = new Zend_Config($this->_all, false);
  91. $this->assertFalse(isset($config->notarealkey));
  92. $this->assertTrue(isset($config->hostname)); // top level
  93. $this->assertTrue(isset($config->db->name)); // one level down
  94. }
  95. }
  96. public function testModification()
  97. {
  98. $config = new Zend_Config($this->_all, true);
  99. // overwrite an existing key
  100. $this->assertEquals('thisname', $config->name);
  101. $config->name = 'anothername';
  102. $this->assertEquals('anothername', $config->name);
  103. // overwrite an existing multi-level key
  104. $this->assertEquals('multi', $config->one->two->three);
  105. $config->one->two->three = 'anothername';
  106. $this->assertEquals('anothername', $config->one->two->three);
  107. // create a new multi-level key
  108. $config->does = array('not'=> array('exist' => 'yet'));
  109. $this->assertEquals('yet', $config->does->not->exist);
  110. }
  111. public function testNoModifications()
  112. {
  113. $config = new Zend_Config($this->_all);
  114. try {
  115. $config->hostname = 'test';
  116. } catch (Zend_Config_Exception $expected) {
  117. $this->assertContains('is read only', $expected->getMessage());
  118. return;
  119. }
  120. $this->fail('An expected Zend_Config_Exception has not been raised');
  121. }
  122. public function testNoNestedModifications()
  123. {
  124. $config = new Zend_Config($this->_all);
  125. try {
  126. $config->db->host = 'test';
  127. } catch (Zend_Config_Exception $expected) {
  128. $this->assertContains('is read only', $expected->getMessage());
  129. return;
  130. }
  131. $this->fail('An expected Zend_Config_Exception has not been raised');
  132. }
  133. public function testNumericKeys()
  134. {
  135. $data = new Zend_Config($this->_numericData);
  136. $this->assertEquals('test', $data->{1});
  137. $this->assertEquals(34, $data->{0});
  138. }
  139. public function testCount()
  140. {
  141. $data = new Zend_Config($this->_menuData1);
  142. $this->assertEquals(3, count($data->button));
  143. }
  144. public function testIterator()
  145. {
  146. // top level
  147. $config = new Zend_Config($this->_all);
  148. $var = '';
  149. foreach ($config as $key=>$value) {
  150. if (is_string($value)) {
  151. $var .= "\nkey = $key, value = $value";
  152. }
  153. }
  154. $this->assertContains('key = name, value = thisname', $var);
  155. // 1 nest
  156. $var = '';
  157. foreach ($config->db as $key=>$value) {
  158. $var .= "\nkey = $key, value = $value";
  159. }
  160. $this->assertContains('key = host, value = 127.0.0.1', $var);
  161. // 2 nests
  162. $config = new Zend_Config($this->_menuData1);
  163. $var = '';
  164. foreach ($config->button->b1 as $key=>$value) {
  165. $var .= "\nkey = $key, value = $value";
  166. }
  167. $this->assertContains('key = L1, value = button1-1', $var);
  168. }
  169. public function testArray()
  170. {
  171. $config = new Zend_Config($this->_all);
  172. ob_start();
  173. print_r($config->toArray());
  174. $contents = ob_get_contents();
  175. ob_end_clean();
  176. $this->assertContains('Array', $contents);
  177. $this->assertContains('[hostname] => all', $contents);
  178. $this->assertContains('[user] => username', $contents);
  179. }
  180. public function testErrorWriteToReadOnly()
  181. {
  182. $config = new Zend_Config($this->_all);
  183. try {
  184. $config->test = '32';
  185. } catch (Zend_Config_Exception $expected) {
  186. $this->assertContains('read only', $expected->getMessage());
  187. return;
  188. }
  189. $this->fail('An expected Zend_Config_Exception has not been raised');
  190. }
  191. public function testZF343()
  192. {
  193. $config_array = array(
  194. 'controls' => array(
  195. 'visible' => array(
  196. 'name' => 'visible',
  197. 'type' => 'checkbox',
  198. 'attribs' => array(), // empty array
  199. ),
  200. ),
  201. );
  202. $form_config = new Zend_Config($config_array, true);
  203. $this->assertSame(array(), $form_config->controls->visible->attribs->toArray());
  204. }
  205. public function testZF402()
  206. {
  207. $configArray = array(
  208. 'data1' => 'someValue',
  209. 'data2' => 'someValue',
  210. 'false1' => false,
  211. 'data3' => 'someValue'
  212. );
  213. $config = new Zend_Config($configArray);
  214. $this->assertTrue(count($config) === count($configArray));
  215. $count = 0;
  216. foreach ($config as $key => $value) {
  217. if ($key === 'false1') {
  218. $this->assertTrue($value === false);
  219. } else {
  220. $this->assertTrue($value === 'someValue');
  221. }
  222. $count++;
  223. }
  224. $this->assertTrue($count === 4);
  225. }
  226. public function testZf1019_HandlingInvalidKeyNames()
  227. {
  228. $config = new Zend_Config($this->_leadingdot);
  229. $array = $config->toArray();
  230. $this->assertContains('dot-test', $array['.test']);
  231. }
  232. public function testZF1019_EmptyKeys()
  233. {
  234. $config = new Zend_Config($this->_invalidkey);
  235. $array = $config->toArray();
  236. $this->assertContains('test', $array[' ']);
  237. $this->assertContains('test', $array['']);
  238. }
  239. public function testZF1417_DefaultValues()
  240. {
  241. $config = new Zend_Config($this->_all);
  242. $value = $config->get('notthere', 'default');
  243. $this->assertTrue($value === 'default');
  244. $this->assertTrue($config->notThere === null);
  245. }
  246. public function testUnsetException()
  247. {
  248. // allow modifications is off - expect an exception
  249. $config = new Zend_Config($this->_all, false);
  250. $this->assertTrue(isset($config->hostname)); // top level
  251. try {
  252. unset($config->hostname);
  253. } catch (Zend_Config_Exception $expected) {
  254. $this->assertContains('is read only', $expected->getMessage());
  255. return;
  256. }
  257. $this->fail('Expected read only exception has not been raised.');
  258. }
  259. public function testUnset()
  260. {
  261. // allow modifications is on
  262. $config = new Zend_Config($this->_all, true);
  263. $this->assertTrue(isset($config->hostname));
  264. $this->assertTrue(isset($config->db->name));
  265. unset($config->hostname);
  266. unset($config->db->name);
  267. $this->assertFalse(isset($config->hostname));
  268. $this->assertFalse(isset($config->db->name));
  269. }
  270. public function testMerge()
  271. {
  272. $stdArray = array(
  273. 'test_feature' => false,
  274. 'some_files' => array(
  275. 'foo'=>'dir/foo.xml',
  276. 'bar'=>'dir/bar.xml',
  277. ),
  278. 2 => 123,
  279. );
  280. $stdConfig = new Zend_Config($stdArray, true);
  281. $devArray = array(
  282. 'test_feature'=>true,
  283. 'some_files' => array(
  284. 'bar' => 'myDir/bar.xml',
  285. 'baz' => 'myDir/baz.xml',
  286. ),
  287. 2 => 456,
  288. );
  289. $devConfig = new Zend_Config($devArray);
  290. $stdConfig->merge($devConfig);
  291. $this->assertTrue($stdConfig->test_feature);
  292. $this->assertEquals('myDir/bar.xml', $stdConfig->some_files->bar);
  293. $this->assertEquals('myDir/baz.xml', $stdConfig->some_files->baz);
  294. $this->assertEquals('dir/foo.xml', $stdConfig->some_files->foo);
  295. $this->assertEquals(456, $stdConfig->{2});
  296. }
  297. /**
  298. * Ensures that toArray() supports objects of types other than Zend_Config
  299. *
  300. * @return void
  301. */
  302. public function testToArraySupportsObjects()
  303. {
  304. $configData = array(
  305. 'a' => new stdClass(),
  306. 'b' => array(
  307. 'c' => new stdClass(),
  308. 'd' => new stdClass()
  309. )
  310. );
  311. $config = new Zend_Config($configData);
  312. $this->assertEquals($config->toArray(), $configData);
  313. $this->assertType('stdClass', $config->a);
  314. $this->assertType('stdClass', $config->b->c);
  315. $this->assertType('stdClass', $config->b->d);
  316. }
  317. /**
  318. * ensure that modification is not allowed after calling setReadOnly()
  319. *
  320. */
  321. public function testSetReadOnly()
  322. {
  323. $configData = array(
  324. 'a' => 'a'
  325. );
  326. $config = new Zend_Config($configData, true);
  327. $config->b = 'b';
  328. $config->setReadOnly();
  329. try {
  330. $config->c = 'c';
  331. } catch (Zend_Config_Exception $expected) {
  332. $this->assertContains('is read only', $expected->getMessage());
  333. return;
  334. }
  335. $this->fail('Expected read only exception has not been raised.');
  336. }
  337. public function testZF3408_countNotDecreasingOnUnset()
  338. {
  339. $configData = array(
  340. 'a' => 'a',
  341. 'b' => 'b',
  342. 'c' => 'c',
  343. );
  344. $config = new Zend_Config($configData, true);
  345. $this->assertEquals(count($config), 3);
  346. unset($config->b);
  347. $this->assertEquals(count($config), 2);
  348. }
  349. public function testZF4107_ensureCloneDoesNotKeepNestedReferences()
  350. {
  351. $parent = new Zend_Config(array('key' => array('nested' => 'parent')), true);
  352. $newConfig = clone $parent;
  353. $newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
  354. $this->assertEquals('override', $newConfig->key->nested, '$newConfig is not overridden');
  355. $this->assertEquals('parent', $parent->key->nested, '$parent has been overridden');
  356. }
  357. /**
  358. * @group ZF-3575
  359. *
  360. */
  361. public function testMergeHonoursAllowModificationsFlagAtAllLevels()
  362. {
  363. $config = new Zend_Config(array('key' => array('nested' => 'yes'), 'key2'=>'yes'), false);
  364. $config2 = new Zend_Config(array(), true);
  365. $config2->merge($config);
  366. try {
  367. $config2->key2 = 'no';
  368. } catch (Zend_Config_Exception $e) {
  369. $this->fail('Unexpected exception at top level has been raised: ' . $e->getMessage());
  370. }
  371. $this->assertEquals('no', $config2->key2);
  372. try {
  373. $config2->key->nested = 'no';
  374. } catch (Zend_Config_Exception $e) {
  375. $this->fail('Unexpected exception on nested object has been raised: ' . $e->getMessage());
  376. }
  377. $this->assertEquals('no', $config2->key->nested);
  378. }
  379. /**
  380. * @group ZF-5771a
  381. *
  382. */
  383. public function testUnsettingFirstElementDuringForeachDoesNotSkipAnElement()
  384. {
  385. $config = new Zend_Config(array(
  386. 'first' => array(1),
  387. 'second' => array(2),
  388. 'third' => array(3)
  389. ), true);
  390. $keyList = array();
  391. foreach ($config as $key => $value)
  392. {
  393. $keyList[] = $key;
  394. if ($key == 'first') {
  395. unset($config->$key); // uses magic Zend_Config::__unset() method
  396. }
  397. }
  398. $this->assertEquals('first', $keyList[0]);
  399. $this->assertEquals('second', $keyList[1]);
  400. $this->assertEquals('third', $keyList[2]);
  401. }
  402. /**
  403. * @group ZF-5771
  404. *
  405. */
  406. public function testUnsettingAMiddleElementDuringForeachDoesNotSkipAnElement()
  407. {
  408. $config = new Zend_Config(array(
  409. 'first' => array(1),
  410. 'second' => array(2),
  411. 'third' => array(3)
  412. ), true);
  413. $keyList = array();
  414. foreach ($config as $key => $value)
  415. {
  416. $keyList[] = $key;
  417. if ($key == 'second') {
  418. unset($config->$key); // uses magic Zend_Config::__unset() method
  419. }
  420. }
  421. $this->assertEquals('first', $keyList[0]);
  422. $this->assertEquals('second', $keyList[1]);
  423. $this->assertEquals('third', $keyList[2]);
  424. }
  425. /**
  426. * @group ZF-5771
  427. *
  428. */
  429. public function testUnsettingLastElementDuringForeachDoesNotSkipAnElement()
  430. {
  431. $config = new Zend_Config(array(
  432. 'first' => array(1),
  433. 'second' => array(2),
  434. 'third' => array(3)
  435. ), true);
  436. $keyList = array();
  437. foreach ($config as $key => $value)
  438. {
  439. $keyList[] = $key;
  440. if ($key == 'third') {
  441. unset($config->$key); // uses magic Zend_Config::__unset() method
  442. }
  443. }
  444. $this->assertEquals('first', $keyList[0]);
  445. $this->assertEquals('second', $keyList[1]);
  446. $this->assertEquals('third', $keyList[2]);
  447. }
  448. /**
  449. * @group ZF-4728
  450. *
  451. */
  452. public function testSetReadOnlyAppliesToChildren()
  453. {
  454. $config = new Zend_Config($this->_all, true);
  455. $config->setReadOnly();
  456. $this->assertTrue($config->readOnly());
  457. $this->assertTrue($config->one->readOnly(), 'First level children are writable');
  458. $this->assertTrue($config->one->two->readOnly(), 'Second level children are writable');
  459. }
  460. public function testZF6995_toArrayDoesNotDisturbInternalIterator()
  461. {
  462. $config = new Zend_Config(range(1,10));
  463. $config->rewind();
  464. $this->assertEquals(1, $config->current());
  465. $config->toArray();
  466. $this->assertEquals(1, $config->current());
  467. }
  468. }