2
0

ConfigTest.php 16 KB

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