XmlTest.php 13 KB

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