PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/ConfigTest.php

https://bitbucket.org/ksekar/campus
PHP | 538 lines | 398 code | 71 blank | 69 comment | 10 complexity | 57cf0485b2a49666af374cacbabb820c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ConfigTest.php 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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_clean();
  175. $this->assertContains('Array', $contents);
  176. $this->assertContains('[hostname] => all', $contents);
  177. $this->assertContains('[user] => username', $contents);
  178. }
  179. public function testErrorWriteToReadOnly()
  180. {
  181. $config = new Zend_Config($this->_all);
  182. try {
  183. $config->test = '32';
  184. } catch (Zend_Config_Exception $expected) {
  185. $this->assertContains('read only', $expected->getMessage());
  186. return;
  187. }
  188. $this->fail('An expected Zend_Config_Exception has not been raised');
  189. }
  190. public function testZF343()
  191. {
  192. $config_array = array(
  193. 'controls' => array(
  194. 'visible' => array(
  195. 'name' => 'visible',
  196. 'type' => 'checkbox',
  197. 'attribs' => array(), // empty array
  198. ),
  199. ),
  200. );
  201. $form_config = new Zend_Config($config_array, true);
  202. $this->assertSame(array(), $form_config->controls->visible->attribs->toArray());
  203. }
  204. public function testZF402()
  205. {
  206. $configArray = array(
  207. 'data1' => 'someValue',
  208. 'data2' => 'someValue',
  209. 'false1' => false,
  210. 'data3' => 'someValue'
  211. );
  212. $config = new Zend_Config($configArray);
  213. $this->assertTrue(count($config) === count($configArray));
  214. $count = 0;
  215. foreach ($config as $key => $value) {
  216. if ($key === 'false1') {
  217. $this->assertTrue($value === false);
  218. } else {
  219. $this->assertTrue($value === 'someValue');
  220. }
  221. $count++;
  222. }
  223. $this->assertTrue($count === 4);
  224. }
  225. public function testZf1019_HandlingInvalidKeyNames()
  226. {
  227. $config = new Zend_Config($this->_leadingdot);
  228. $array = $config->toArray();
  229. $this->assertContains('dot-test', $array['.test']);
  230. }
  231. public function testZF1019_EmptyKeys()
  232. {
  233. $config = new Zend_Config($this->_invalidkey);
  234. $array = $config->toArray();
  235. $this->assertContains('test', $array[' ']);
  236. $this->assertContains('test', $array['']);
  237. }
  238. public function testZF1417_DefaultValues()
  239. {
  240. $config = new Zend_Config($this->_all);
  241. $value = $config->get('notthere', 'default');
  242. $this->assertTrue($value === 'default');
  243. $this->assertTrue($config->notThere === null);
  244. }
  245. public function testUnsetException()
  246. {
  247. // allow modifications is off - expect an exception
  248. $config = new Zend_Config($this->_all, false);
  249. $this->assertTrue(isset($config->hostname)); // top level
  250. try {
  251. unset($config->hostname);
  252. } catch (Zend_Config_Exception $expected) {
  253. $this->assertContains('is read only', $expected->getMessage());
  254. return;
  255. }
  256. $this->fail('Expected read only exception has not been raised.');
  257. }
  258. public function testUnset()
  259. {
  260. // allow modifications is on
  261. $config = new Zend_Config($this->_all, true);
  262. $this->assertTrue(isset($config->hostname));
  263. $this->assertTrue(isset($config->db->name));
  264. unset($config->hostname);
  265. unset($config->db->name);
  266. $this->assertFalse(isset($config->hostname));
  267. $this->assertFalse(isset($config->db->name));
  268. }
  269. public function testMerge()
  270. {
  271. $stdArray = array(
  272. 'test_feature' => false,
  273. 'some_files' => array(
  274. 'foo'=>'dir/foo.xml',
  275. 'bar'=>'dir/bar.xml',
  276. ),
  277. 2 => 123,
  278. );
  279. $stdConfig = new Zend_Config($stdArray, true);
  280. $devArray = array(
  281. 'test_feature'=>true,
  282. 'some_files' => array(
  283. 'bar' => 'myDir/bar.xml',
  284. 'baz' => 'myDir/baz.xml',
  285. ),
  286. 2 => 456,
  287. );
  288. $devConfig = new Zend_Config($devArray);
  289. $stdConfig->merge($devConfig);
  290. $this->assertTrue($stdConfig->test_feature);
  291. $this->assertEquals('myDir/bar.xml', $stdConfig->some_files->bar);
  292. $this->assertEquals('myDir/baz.xml', $stdConfig->some_files->baz);
  293. $this->assertEquals('dir/foo.xml', $stdConfig->some_files->foo);
  294. $this->assertEquals(456, $stdConfig->{2});
  295. }
  296. /**
  297. * Ensures that toArray() supports objects of types other than Zend_Config
  298. *
  299. * @return void
  300. */
  301. public function testToArraySupportsObjects()
  302. {
  303. $configData = array(
  304. 'a' => new stdClass(),
  305. 'b' => array(
  306. 'c' => new stdClass(),
  307. 'd' => new stdClass()
  308. )
  309. );
  310. $config = new Zend_Config($configData);
  311. $this->assertEquals($config->toArray(), $configData);
  312. $this->assertType('stdClass', $config->a);
  313. $this->assertType('stdClass', $config->b->c);
  314. $this->assertType('stdClass', $config->b->d);
  315. }
  316. /**
  317. * ensure that modification is not allowed after calling setReadOnly()
  318. *
  319. */
  320. public function testSetReadOnly()
  321. {
  322. $configData = array(
  323. 'a' => 'a'
  324. );
  325. $config = new Zend_Config($configData, true);
  326. $config->b = 'b';
  327. $config->setReadOnly();
  328. try {
  329. $config->c = 'c';
  330. } catch (Zend_Config_Exception $expected) {
  331. $this->assertContains('is read only', $expected->getMessage());
  332. return;
  333. }
  334. $this->fail('Expected read only exception has not been raised.');
  335. }
  336. public function testZF3408_countNotDecreasingOnUnset()
  337. {
  338. $configData = array(
  339. 'a' => 'a',
  340. 'b' => 'b',
  341. 'c' => 'c',
  342. );
  343. $config = new Zend_Config($configData, true);
  344. $this->assertEquals(count($config), 3);
  345. unset($config->b);
  346. $this->assertEquals(count($config), 2);
  347. }
  348. public function testZF4107_ensureCloneDoesNotKeepNestedReferences()
  349. {
  350. $parent = new Zend_Config(array('key' => array('nested' => 'parent')), true);
  351. $newConfig = clone $parent;
  352. $newConfig->merge(new Zend_Config(array('key' => array('nested' => 'override')), true));
  353. $this->assertEquals('override', $newConfig->key->nested, '$newConfig is not overridden');
  354. $this->assertEquals('parent', $parent->key->nested, '$parent has been overridden');
  355. }
  356. /**
  357. * @group ZF-3575
  358. *
  359. */
  360. public function testMergeHonoursAllowModificationsFlagAtAllLevels()
  361. {
  362. $config = new Zend_Config(array('key' => array('nested' => 'yes'), 'key2'=>'yes'), false);
  363. $config2 = new Zend_Config(array(), true);
  364. $config2->merge($config);
  365. try {
  366. $config2->key2 = 'no';
  367. } catch (Zend_Config_Exception $e) {
  368. $this->fail('Unexpected exception at top level has been raised: ' . $e->getMessage());
  369. }
  370. $this->assertEquals('no', $config2->key2);
  371. try {
  372. $config2->key->nested = 'no';
  373. } catch (Zend_Config_Exception $e) {
  374. $this->fail('Unexpected exception on nested object has been raised: ' . $e->getMessage());
  375. }
  376. $this->assertEquals('no', $config2->key->nested);
  377. }
  378. /**
  379. * @group ZF-5771a
  380. *
  381. */
  382. public function testUnsettingFirstElementDuringForeachDoesNotSkipAnElement()
  383. {
  384. $config = new Zend_Config(array(
  385. 'first' => array(1),
  386. 'second' => array(2),
  387. 'third' => array(3)
  388. ), true);
  389. $keyList = array();
  390. foreach ($config as $key => $value)
  391. {
  392. $keyList[] = $key;
  393. if ($key == 'first') {
  394. unset($config->$key); // uses magic Zend_Config::__unset() method
  395. }
  396. }
  397. $this->assertEquals('first', $keyList[0]);
  398. $this->assertEquals('second', $keyList[1]);
  399. $this->assertEquals('third', $keyList[2]);
  400. }
  401. /**
  402. * @group ZF-5771
  403. *
  404. */
  405. public function testUnsettingAMiddleElementDuringForeachDoesNotSkipAnElement()
  406. {
  407. $config = new Zend_Config(array(
  408. 'first' => array(1),
  409. 'second' => array(2),
  410. 'third' => array(3)
  411. ), true);
  412. $keyList = array();
  413. foreach ($config as $key => $value)
  414. {
  415. $keyList[] = $key;
  416. if ($key == 'second') {
  417. unset($config->$key); // uses magic Zend_Config::__unset() method
  418. }
  419. }
  420. $this->assertEquals('first', $keyList[0]);
  421. $this->assertEquals('second', $keyList[1]);
  422. $this->assertEquals('third', $keyList[2]);
  423. }
  424. /**
  425. * @group ZF-5771
  426. *
  427. */
  428. public function testUnsettingLastElementDuringForeachDoesNotSkipAnElement()
  429. {
  430. $config = new Zend_Config(array(
  431. 'first' => array(1),
  432. 'second' => array(2),
  433. 'third' => array(3)
  434. ), true);
  435. $keyList = array();
  436. foreach ($config as $key => $value)
  437. {
  438. $keyList[] = $key;
  439. if ($key == 'third') {
  440. unset($config->$key); // uses magic Zend_Config::__unset() method
  441. }
  442. }
  443. $this->assertEquals('first', $keyList[0]);
  444. $this->assertEquals('second', $keyList[1]);
  445. $this->assertEquals('third', $keyList[2]);
  446. }
  447. /**
  448. * @group ZF-4728
  449. *
  450. */
  451. public function testSetReadOnlyAppliesToChildren()
  452. {
  453. $config = new Zend_Config($this->_all, true);
  454. $config->setReadOnly();
  455. $this->assertTrue($config->readOnly());
  456. $this->assertTrue($config->one->readOnly(), 'First level children are writable');
  457. $this->assertTrue($config->one->two->readOnly(), 'Second level children are writable');
  458. }
  459. public function testZF6995_toArrayDoesNotDisturbInternalIterator()
  460. {
  461. $config = new Zend_Config(range(1,10));
  462. $config->rewind();
  463. $this->assertEquals(1, $config->current());
  464. $config->toArray();
  465. $this->assertEquals(1, $config->current());
  466. }
  467. }