XmlTest.php 13 KB

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