IniTest.php 11 KB

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