XmlTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_Xml
  28. */
  29. require_once 'Zend/Config/Xml.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_XmlTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_xmlFileConfig;
  40. protected $_xmlFileAllSectionsConfig;
  41. protected $_xmlFileCircularConfig;
  42. protected $_xmlFileInvalid;
  43. public function setUp()
  44. {
  45. $this->_xmlFileConfig = dirname(__FILE__) . '/_files/config.xml';
  46. $this->_xmlFileAllSectionsConfig = dirname(__FILE__) . '/_files/allsections.xml';
  47. $this->_xmlFileCircularConfig = dirname(__FILE__) . '/_files/circular.xml';
  48. $this->_xmlFileTopLevelStringConfig = dirname(__FILE__) . '/_files/toplevelstring.xml';
  49. $this->_xmlFileOneTopLevelStringConfig = dirname(__FILE__) . '/_files/onetoplevelstring.xml';
  50. $this->_nonReadableConfig = dirname(__FILE__) . '/_files/nonreadable.xml';
  51. $this->_xmlFileSameNameKeysConfig = dirname(__FILE__) . '/_files/array.xml';
  52. $this->_xmlFileShortParamsOneConfig = dirname(__FILE__) . '/_files/shortparamsone.xml';
  53. $this->_xmlFileShortParamsTwoConfig = dirname(__FILE__) . '/_files/shortparamstwo.xml';
  54. $this->_xmlFileInvalid = dirname(__FILE__) . '/_files/invalid.xml';
  55. }
  56. public function testLoadSingleSection()
  57. {
  58. $config = new Zend_Config_Xml($this->_xmlFileConfig, 'all');
  59. $this->assertEquals('all', $config->hostname);
  60. $this->assertEquals('live', $config->db->name);
  61. $this->assertEquals('multi', $config->one->two->three);
  62. $this->assertNull(@$config->nonexistent); // property doesn't exist
  63. }
  64. public function testSectionInclude()
  65. {
  66. $config = new Zend_Config_Xml($this->_xmlFileConfig, 'staging');
  67. $this->assertEquals('false', $config->debug); // only in staging
  68. $this->assertEquals('thisname', $config->name); // only in all
  69. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  70. $this->assertEquals('staging', $config->hostname); // inherited and overridden
  71. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  72. }
  73. public function testMultiDepthExtends()
  74. {
  75. $config = new Zend_Config_Xml($this->_xmlFileConfig, 'other_staging');
  76. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  77. $this->assertEquals('false', $config->debug); // 1 level down: only in staging
  78. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  79. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  80. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  81. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  82. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  83. }
  84. public function testErrorNoInitialSection()
  85. {
  86. try {
  87. $config = @new Zend_Config_Xml($this->_xmlFileConfig, 'notthere');
  88. $this->fail('An expected Zend_Config_Exception has not been raised');
  89. } catch (Zend_Config_Exception $expected) {
  90. $this->assertContains('cannot be found in', $expected->getMessage());
  91. }
  92. try {
  93. $config = @new Zend_Config_Xml($this->_xmlFileConfig, array('notthere', 'all'));
  94. $this->fail('An expected Zend_Config_Exception has not been raised');
  95. } catch (Zend_Config_Exception $expected) {
  96. $this->assertContains('cannot be found in', $expected->getMessage());
  97. }
  98. }
  99. public function testErrorNoExtendsSection()
  100. {
  101. try {
  102. $config = new Zend_Config_Xml($this->_xmlFileConfig, '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 testZF413_MultiSections()
  109. {
  110. $config = new Zend_Config_Xml($this->_xmlFileAllSectionsConfig, array('staging','other_staging'));
  111. $this->assertEquals('otherStaging', $config->only_in);
  112. $this->assertEquals('staging', $config->hostname);
  113. }
  114. public function testZF413_AllSections()
  115. {
  116. $config = new Zend_Config_Xml($this->_xmlFileAllSectionsConfig, null);
  117. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  118. $this->assertEquals('staging', $config->staging->hostname);
  119. }
  120. public function testZF414()
  121. {
  122. $config = new Zend_Config_Xml($this->_xmlFileAllSectionsConfig, null);
  123. $this->assertEquals(null, $config->getSectionName());
  124. $this->assertEquals(true, $config->areAllSectionsLoaded());
  125. $config = new Zend_Config_Xml($this->_xmlFileAllSectionsConfig, 'all');
  126. $this->assertEquals('all', $config->getSectionName());
  127. $this->assertEquals(false, $config->areAllSectionsLoaded());
  128. $config = new Zend_Config_Xml($this->_xmlFileAllSectionsConfig, array('staging','other_staging'));
  129. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  130. $this->assertEquals(false, $config->areAllSectionsLoaded());
  131. }
  132. public function testZF415()
  133. {
  134. try {
  135. $config = new Zend_Config_Xml($this->_xmlFileCircularConfig, null);
  136. $this->fail('An expected Zend_Config_Exception has not been raised');
  137. } catch (Zend_Config_Exception $expected) {
  138. $this->assertContains('circular inheritance', $expected->getMessage());
  139. }
  140. }
  141. public function testErrorNoFile()
  142. {
  143. try {
  144. $config = new Zend_Config_Xml('',null);
  145. $this->fail('An expected Zend_Config_Exception has not been raised');
  146. } catch (Zend_Config_Exception $expected) {
  147. $this->assertContains('Filename is not set', $expected->getMessage());
  148. }
  149. }
  150. public function testZF2162_TopLevelString()
  151. {
  152. $config = new Zend_Config_Xml($this->_xmlFileTopLevelStringConfig, null);
  153. $this->assertEquals('one', $config->one);
  154. $this->assertEquals('three', $config->two->three);
  155. $this->assertEquals('five', $config->two->four->five);
  156. $this->assertEquals('three', $config->six->three);
  157. $config = new Zend_Config_Xml($this->_xmlFileOneTopLevelStringConfig);
  158. $this->assertEquals('one', $config->one);
  159. $config = new Zend_Config_Xml($this->_xmlFileOneTopLevelStringConfig, 'one');
  160. $this->assertEquals('one', $config->one);
  161. }
  162. public function testZF2285_MultipleKeysOfTheSameName()
  163. {
  164. $config = new Zend_Config_Xml($this->_xmlFileSameNameKeysConfig, null);
  165. $this->assertEquals('2a', $config->one->two->{0});
  166. $this->assertEquals('2b', $config->one->two->{1});
  167. $this->assertEquals('4', $config->three->four->{1});
  168. $this->assertEquals('5', $config->three->four->{0}->five);
  169. }
  170. public function testZF2437_ArraysWithMultipleChildren()
  171. {
  172. $config = new Zend_Config_Xml($this->_xmlFileSameNameKeysConfig, null);
  173. $this->assertEquals('1', $config->six->seven->{0}->eight);
  174. $this->assertEquals('2', $config->six->seven->{1}->eight);
  175. $this->assertEquals('3', $config->six->seven->{2}->eight);
  176. $this->assertEquals('1', $config->six->seven->{0}->nine);
  177. $this->assertEquals('2', $config->six->seven->{1}->nine);
  178. $this->assertEquals('3', $config->six->seven->{2}->nine);
  179. }
  180. public function testZF3578_InvalidOrMissingfXmlFile()
  181. {
  182. try {
  183. $config = new Zend_Config_Xml($this->_xmlFileInvalid);
  184. $this->fail('An expected Zend_Config_Exception has not been raised');
  185. } catch (Zend_Config_Exception $expected) {
  186. $this->assertContains('parser error', $expected->getMessage());
  187. }
  188. try {
  189. $config = new Zend_Config_Xml('I/dont/exist');
  190. $this->fail('An expected Zend_Config_Exception has not been raised');
  191. } catch (Zend_Config_Exception $expected) {
  192. $this->assertContains('failed to load', $expected->getMessage());
  193. }
  194. }
  195. public function testShortParamsOne()
  196. {
  197. $config = new Zend_Config_Xml($this->_xmlFileShortParamsOneConfig, 'all');
  198. $this->assertEquals('all', $config->hostname);
  199. $this->assertEquals('thisname', $config->name);
  200. $this->assertEquals('username', $config->db->user);
  201. $this->assertEquals('live', $config->db->name);
  202. $this->assertEquals('multi', $config->one->two->three);
  203. }
  204. public function testShortParamsTwo()
  205. {
  206. $config = new Zend_Config_Xml($this->_xmlFileShortParamsTwoConfig, 'all');
  207. $this->assertEquals('all', $config->hostname);
  208. $this->assertEquals('thisname', $config->name);
  209. $this->assertEquals('username', $config->db->user);
  210. $this->assertEquals('live', $config->db->name);
  211. $this->assertEquals('multi', $config->one->two->three);
  212. }
  213. /*
  214. * @group 3702
  215. *
  216. */
  217. public function testLoadAnXMLString()
  218. {
  219. $string = <<<EOT
  220. <?xml version="1.0"?>
  221. <config>
  222. <all>
  223. <hostname>all</hostname>
  224. <db>
  225. <host>127.0.0.1</host>
  226. <name>live</name>
  227. </db>
  228. </all>
  229. <staging extends="all">
  230. <hostname>staging</hostname>
  231. <db>
  232. <name>dbstaging</name>
  233. </db>
  234. <debug>false</debug>
  235. </staging>
  236. </config>
  237. EOT;
  238. $config = new Zend_Config_Xml($string, 'staging');
  239. $this->assertEquals('staging', $config->hostname);
  240. }
  241. /*
  242. * @group ZF-5800
  243. *
  244. */
  245. public function testArraysOfKeysCreatedUsingAttributesAndKeys()
  246. {
  247. $string = <<<EOT
  248. <?xml version="1.0"?>
  249. <config>
  250. <rec>
  251. <receiver>
  252. <mail>user1@company.com</mail>
  253. <name>User Name</name>
  254. <html>1</html>
  255. </receiver>
  256. </rec>
  257. <dev extends="rec">
  258. <receiver mail="nice.guy@company.com" name="Nice Guy" />
  259. <receiver mail="fred@company.com" html="2" />
  260. </dev>
  261. </config>
  262. EOT;
  263. $config = new Zend_Config_Xml($string, 'dev');
  264. $this->assertEquals('nice.guy@company.com', $config->receiver->{0}->mail);
  265. $this->assertEquals('1', $config->receiver->{0}->html);
  266. $this->assertNull($config->receiver->mail);
  267. }
  268. }