IniTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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-2015 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_Ini
  24. */
  25. require_once 'Zend/Config/Ini.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Config
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 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_Config_IniTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $_iniFileConfig;
  37. protected $_iniFileAllSectionsConfig;
  38. protected $_iniFileCircularConfig;
  39. public function setUp()
  40. {
  41. $this->_iniFileConfig = dirname(__FILE__) . '/_files/config.ini';
  42. $this->_iniFileAllSectionsConfig = dirname(__FILE__) . '/_files/allsections.ini';
  43. $this->_iniFileCircularConfig = dirname(__FILE__) . '/_files/circular.ini';
  44. $this->_iniFileMultipleInheritanceConfig = dirname(__FILE__) . '/_files/multipleinheritance.ini';
  45. $this->_iniFileSeparatorConfig = dirname(__FILE__) . '/_files/separator.ini';
  46. $this->_nonReadableConfig = dirname(__FILE__) . '/_files/nonreadable.ini';
  47. $this->_iniFileNoSectionsConfig = dirname(__FILE__) . '/_files/nosections.ini';
  48. $this->_iniFileInvalid = dirname(__FILE__) . '/_files/invalid.ini';
  49. }
  50. public function testLoadSingleSection()
  51. {
  52. $config = new Zend_Config_Ini($this->_iniFileConfig, 'all');
  53. $this->assertEquals('all', $config->hostname);
  54. $this->assertEquals('live', $config->db->name);
  55. $this->assertEquals('multi', $config->one->two->three);
  56. $this->assertNull(@$config->nonexistent); // property doesn't exist
  57. }
  58. public function testSectionInclude()
  59. {
  60. $config = new Zend_Config_Ini($this->_iniFileConfig, 'staging');
  61. $this->assertEquals('', $config->debug); // only in staging
  62. $this->assertEquals('thisname', $config->name); // only in all
  63. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  64. $this->assertEquals('staging', $config->hostname); // inherited and overridden
  65. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  66. }
  67. public function testTrueValues()
  68. {
  69. $config = new Zend_Config_Ini($this->_iniFileConfig, 'debug');
  70. $this->assertTrue(is_string($config->debug));
  71. $this->assertEquals('1', $config->debug);
  72. $this->assertTrue(is_string($config->values->changed));
  73. $this->assertEquals('1', $config->values->changed);
  74. }
  75. public function testEmptyValues()
  76. {
  77. $config = new Zend_Config_Ini($this->_iniFileConfig, 'debug');
  78. $this->assertTrue(is_string($config->special->no));
  79. $this->assertEquals('', $config->special->no);
  80. $this->assertTrue(is_string($config->special->null));
  81. $this->assertEquals('', $config->special->null);
  82. $this->assertTrue(is_string($config->special->false));
  83. $this->assertEquals('', $config->special->false);
  84. }
  85. public function testMultiDepthExtends()
  86. {
  87. $config = new Zend_Config_Ini($this->_iniFileConfig, 'other_staging');
  88. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  89. $this->assertEquals('', $config->debug); // 1 level down: only in staging
  90. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  91. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  92. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  93. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  94. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  95. }
  96. public function testErrorNoExtendsSection()
  97. {
  98. try {
  99. $config = new Zend_Config_Ini($this->_iniFileConfig, 'extendserror');
  100. $this->fail('An expected Zend_Config_Exception has not been raised');
  101. } catch (Zend_Config_Exception $expected) {
  102. $this->assertContains('cannot be found', $expected->getMessage());
  103. }
  104. }
  105. public function testInvalidKeys()
  106. {
  107. $sections = array('leadingdot', 'onedot', 'twodots', 'threedots', 'trailingdot');
  108. foreach ($sections as $section) {
  109. try {
  110. $config = new Zend_Config_Ini($this->_iniFileConfig, $section);
  111. $this->fail('An expected Zend_Config_Exception has not been raised');
  112. } catch (Zend_Config_Exception $expected) {
  113. $this->assertContains('Invalid key', $expected->getMessage());
  114. }
  115. }
  116. }
  117. public function testZF426()
  118. {
  119. try {
  120. $config = new Zend_Config_Ini($this->_iniFileConfig, 'zf426');
  121. $this->fail('An expected Zend_Config_Exception has not been raised');
  122. } catch (Zend_Config_Exception $expected) {
  123. $this->assertContains('Cannot create sub-key for', $expected->getMessage());
  124. }
  125. }
  126. public function testZF413_MultiSections()
  127. {
  128. $config = new Zend_Config_Ini($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  129. $this->assertEquals('otherStaging', $config->only_in);
  130. $this->assertEquals('staging', $config->hostname);
  131. }
  132. public function testZF413_AllSections()
  133. {
  134. $config = new Zend_Config_Ini($this->_iniFileAllSectionsConfig, null);
  135. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  136. $this->assertEquals('staging', $config->staging->hostname);
  137. }
  138. public function testZF414()
  139. {
  140. $config = new Zend_Config_Ini($this->_iniFileAllSectionsConfig, null);
  141. $this->assertEquals(null, $config->getSectionName());
  142. $this->assertEquals(true, $config->areAllSectionsLoaded());
  143. $config = new Zend_Config_Ini($this->_iniFileAllSectionsConfig, 'all');
  144. $this->assertEquals('all', $config->getSectionName());
  145. $this->assertEquals(false, $config->areAllSectionsLoaded());
  146. $config = new Zend_Config_Ini($this->_iniFileAllSectionsConfig, array('staging','other_staging'));
  147. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  148. $this->assertEquals(false, $config->areAllSectionsLoaded());
  149. }
  150. public function testZF415()
  151. {
  152. try {
  153. $config = new Zend_Config_Ini($this->_iniFileCircularConfig, null);
  154. $this->fail('An expected Zend_Config_Exception has not been raised');
  155. } catch (Zend_Config_Exception $expected) {
  156. $this->assertContains('circular inheritance', $expected->getMessage());
  157. }
  158. }
  159. public function testErrorNoFile()
  160. {
  161. try {
  162. $config = new Zend_Config_Ini('','');
  163. $this->fail('An expected Zend_Config_Exception has not been raised');
  164. } catch (Zend_Config_Exception $expected) {
  165. $this->assertContains('Filename is not set', $expected->getMessage());
  166. }
  167. }
  168. public function testErrorMultipleExensions()
  169. {
  170. try {
  171. $config = new Zend_Config_Ini($this->_iniFileMultipleInheritanceConfig, 'three');
  172. zend::dump($config);
  173. $this->fail('An expected Zend_Config_Exception has not been raised');
  174. } catch (Zend_Config_Exception $expected) {
  175. $this->assertContains('may not extend multiple sections', $expected->getMessage());
  176. }
  177. }
  178. public function testErrorNoSectionFound()
  179. {
  180. try {
  181. $config = new Zend_Config_Ini($this->_iniFileConfig,array('all', 'notthere'));
  182. $this->fail('An expected Zend_Config_Exception has not been raised');
  183. } catch (Zend_Config_Exception $expected) {
  184. $this->assertContains('cannot be found', $expected->getMessage());
  185. }
  186. try {
  187. $config = new Zend_Config_Ini($this->_iniFileConfig,'notthere');
  188. $this->fail('An expected Zend_Config_Exception has not been raised');
  189. } catch (Zend_Config_Exception $expected) {
  190. $this->assertContains('cannot be found', $expected->getMessage());
  191. }
  192. }
  193. public function testZF739()
  194. {
  195. if (version_compare('5.3.0', PHP_VERSION) < 1) {
  196. $this->markTestSkipped('PHP >= 5.3.0 does not allow : as a nest separator');
  197. return;
  198. }
  199. $config = new Zend_Config_Ini($this->_iniFileSeparatorConfig, 'all', array('nestSeparator'=>':'));
  200. $this->assertEquals('all', $config->hostname);
  201. $this->assertEquals('live', $config->db->name);
  202. $this->assertEquals('multi', $config->one->two->three);
  203. }
  204. public function testZF2508NoSections()
  205. {
  206. $config = new Zend_Config_Ini($this->_iniFileNoSectionsConfig);
  207. $this->assertEquals('all', $config->hostname);
  208. $this->assertEquals('two', $config->one->two);
  209. $this->assertEquals('4', $config->one->three->four);
  210. $this->assertEquals('5', $config->one->three->five);
  211. }
  212. public function testZF2843NoSectionNoTree()
  213. {
  214. $filename = dirname(__FILE__) . '/_files/zf2843.ini';
  215. $config = new Zend_Config_Ini($filename, null, array('nestSeparator' => '.'));
  216. $this->assertEquals('123', $config->abc);
  217. $this->assertEquals('jkl', $config->ghi);
  218. }
  219. public function testZF3196_InvalidIniFile()
  220. {
  221. try {
  222. $config = new Zend_Config_Ini($this->_iniFileInvalid);
  223. $this->fail('An expected Zend_Config_Exception has not been raised');
  224. } catch (Zend_Config_Exception $expected) {
  225. $this->assertRegexp('/(Error parsing|parse error|syntax error, unexpected)/', $expected->getMessage());
  226. }
  227. }
  228. /**
  229. * @group ZF-8159
  230. */
  231. public function testZF8159()
  232. {
  233. $config = new Zend_Config_Ini(
  234. dirname(__FILE__) . '/_files/zf8159.ini',
  235. array('first', 'second')
  236. );
  237. $this->assertTrue(isset(
  238. $config->user->login->elements->password
  239. ));
  240. $this->assertEquals(
  241. 'password',
  242. $config->user->login->elements->password->type
  243. );
  244. }
  245. /*
  246. * @group ZF-5800
  247. *
  248. */
  249. public function testArraysOfKeysCreatedUsingAttributesAndKeys()
  250. {
  251. $filename = dirname(__FILE__) . '/_files/zf5800.ini';
  252. $config = new Zend_Config_Ini($filename, 'dev');
  253. $this->assertEquals('nice.guy@company.com', $config->receiver->{0}->mail);
  254. $this->assertEquals('1', $config->receiver->{0}->html);
  255. $this->assertNull($config->receiver->mail);
  256. }
  257. /*
  258. * @group ZF-6508
  259. */
  260. public function testPreservationOfIntegerKeys()
  261. {
  262. $filename = dirname(__FILE__) . '/_files/zf6508.ini';
  263. $config = new Zend_Config_Ini($filename, 'all');
  264. $this->assertEquals(true, isset($config->{1002}));
  265. }
  266. }