2
0

IniTest.php 11 KB

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