PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Config/XmlTest.php

https://github.com/sidealice/zf2
PHP | 368 lines | 259 code | 46 blank | 63 comment | 2 complexity | cd28cad5ccf3eb05c18178615fb6ac95 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace ZendTest\Config;
  22. use \Zend\Config\Xml;
  23. /**
  24. * @category Zend
  25. * @package Zend_Config
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Config
  30. */
  31. class XmlTest extends \PHPUnit_Framework_TestCase
  32. {
  33. protected $_xmlFileConfig;
  34. protected $_xmlFileAllSectionsConfig;
  35. protected $_xmlFileCircularConfig;
  36. protected $_xmlFileInvalid;
  37. public function setUp()
  38. {
  39. $this->_xmlFileConfig = __DIR__ . '/_files/config.xml';
  40. $this->_xmlFileAllSectionsConfig = __DIR__ . '/_files/allsections.xml';
  41. $this->_xmlFileCircularConfig = __DIR__ . '/_files/circular.xml';
  42. $this->_xmlFileTopLevelStringConfig = __DIR__ . '/_files/toplevelstring.xml';
  43. $this->_xmlFileOneTopLevelStringConfig = __DIR__ . '/_files/onetoplevelstring.xml';
  44. $this->_nonReadableConfig = __DIR__ . '/_files/nonreadable.xml';
  45. $this->_xmlFileSameNameKeysConfig = __DIR__ . '/_files/array.xml';
  46. $this->_xmlFileShortParamsOneConfig = __DIR__ . '/_files/shortparamsone.xml';
  47. $this->_xmlFileShortParamsTwoConfig = __DIR__ . '/_files/shortparamstwo.xml';
  48. $this->_xmlFileInvalid = __DIR__ . '/_files/invalid.xml';
  49. }
  50. public function testLoadSingleSection()
  51. {
  52. $config = new Xml($this->_xmlFileConfig, 'all');
  53. $this->assertEquals('all', $config->hostname);
  54. $this->assertEquals('live', $config->db->name);
  55. $this->assertEquals('multi', $config->one->two->three);
  56. $this->assertNull(@$config->nonexistent); // property doesn't exist
  57. }
  58. public function testSectionInclude()
  59. {
  60. $config = new Xml($this->_xmlFileConfig, 'staging');
  61. $this->assertEquals('false', $config->debug); // only in staging
  62. $this->assertEquals('thisname', $config->name); // only in all
  63. $this->assertEquals('username', $config->db->user); // only in all (nested version)
  64. $this->assertEquals('staging', $config->hostname); // inherited and overridden
  65. $this->assertEquals('dbstaging', $config->db->name); // inherited and overridden
  66. }
  67. public function testMultiDepthExtends()
  68. {
  69. $config = new Xml($this->_xmlFileConfig, 'other_staging');
  70. $this->assertEquals('otherStaging', $config->only_in); // only in other_staging
  71. $this->assertEquals('false', $config->debug); // 1 level down: only in staging
  72. $this->assertEquals('thisname', $config->name); // 2 levels down: only in all
  73. $this->assertEquals('username', $config->db->user); // 2 levels down: only in all (nested version)
  74. $this->assertEquals('staging', $config->hostname); // inherited from two to one and overridden
  75. $this->assertEquals('dbstaging', $config->db->name); // inherited from two to one and overridden
  76. $this->assertEquals('anotherpwd', $config->db->pass); // inherited from two to other_staging and overridden
  77. }
  78. public function testErrorNoInitialSection()
  79. {
  80. $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'cannot be found in');
  81. $config = @new Xml($this->_xmlFileConfig, 'notthere');
  82. }
  83. public function testErrorNoInitialSectionWhenArrayOfSectionsSpecified()
  84. {
  85. $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'cannot be found in');
  86. $config = @new Xml($this->_xmlFileConfig, array('notthere', 'all'));
  87. }
  88. public function testErrorNoExtendsSection()
  89. {
  90. $this->setExpectedException('Zend\Config\Exception\RuntimeException', 'cannot be found');
  91. $config = new Xml($this->_xmlFileConfig, 'extendserror');
  92. }
  93. /**
  94. * @group ZF-413
  95. */
  96. public function testMultiSections()
  97. {
  98. $config = new Xml($this->_xmlFileAllSectionsConfig, array('staging','other_staging'));
  99. $this->assertEquals('otherStaging', $config->only_in);
  100. $this->assertEquals('staging', $config->hostname);
  101. }
  102. /**
  103. * @group ZF-413
  104. */
  105. public function testAllSections()
  106. {
  107. $config = new Xml($this->_xmlFileAllSectionsConfig, null);
  108. $this->assertEquals('otherStaging', $config->other_staging->only_in);
  109. $this->assertEquals('staging', $config->staging->hostname);
  110. }
  111. /**
  112. * @group ZF-414
  113. */
  114. public function testGetSectionNameAndAreAllSectionsLoaded()
  115. {
  116. $config = new Xml($this->_xmlFileAllSectionsConfig, null);
  117. $this->assertEquals(null, $config->getSectionName());
  118. $this->assertEquals(true, $config->areAllSectionsLoaded());
  119. $config = new Xml($this->_xmlFileAllSectionsConfig, 'all');
  120. $this->assertEquals('all', $config->getSectionName());
  121. $this->assertEquals(false, $config->areAllSectionsLoaded());
  122. $config = new Xml($this->_xmlFileAllSectionsConfig, array('staging','other_staging'));
  123. $this->assertEquals(array('staging','other_staging'), $config->getSectionName());
  124. $this->assertEquals(false, $config->areAllSectionsLoaded());
  125. }
  126. /**
  127. * @group ZF-415
  128. */
  129. public function testErrorCircularInheritance()
  130. {
  131. $this->setExpectedException('Zend\Config\Exception\RuntimeException', 'circular inheritance');
  132. $config = new Xml($this->_xmlFileCircularConfig, null);
  133. }
  134. public function testErrorNoFile()
  135. {
  136. $this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Filename is not set');
  137. $config = new Xml('',null);
  138. }
  139. /**
  140. * @group ZF-2162
  141. */
  142. public function testTopLevelString()
  143. {
  144. $config = new Xml($this->_xmlFileTopLevelStringConfig, null);
  145. $this->assertEquals('one', $config->one);
  146. $this->assertEquals('three', $config->two->three);
  147. $this->assertEquals('five', $config->two->four->five);
  148. $this->assertEquals('three', $config->six->three);
  149. $config = new Xml($this->_xmlFileOneTopLevelStringConfig);
  150. $this->assertEquals('one', $config->one);
  151. $config = new Xml($this->_xmlFileOneTopLevelStringConfig, 'one');
  152. $this->assertEquals('one', $config->one);
  153. }
  154. /**
  155. * @group ZF-2285
  156. */
  157. public function testMultipleKeysOfTheSameName()
  158. {
  159. $config = new Xml($this->_xmlFileSameNameKeysConfig, null);
  160. $this->assertEquals('2a', $config->one->two->{0});
  161. $this->assertEquals('2b', $config->one->two->{1});
  162. $this->assertEquals('4', $config->three->four->{1});
  163. $this->assertEquals('5', $config->three->four->{0}->five);
  164. }
  165. /**
  166. * @group ZF-2437
  167. */
  168. public function testArraysWithMultipleChildren()
  169. {
  170. $config = new Xml($this->_xmlFileSameNameKeysConfig, null);
  171. $this->assertEquals('1', $config->six->seven->{0}->eight);
  172. $this->assertEquals('2', $config->six->seven->{1}->eight);
  173. $this->assertEquals('3', $config->six->seven->{2}->eight);
  174. $this->assertEquals('1', $config->six->seven->{0}->nine);
  175. $this->assertEquals('2', $config->six->seven->{1}->nine);
  176. $this->assertEquals('3', $config->six->seven->{2}->nine);
  177. }
  178. /**
  179. * @group ZF-3578
  180. * @group ZF2-13
  181. */
  182. public function testInvalidXmlFile()
  183. {
  184. try {
  185. $config = new Xml($this->_xmlFileInvalid);
  186. $this->fail('Missing expected exception');
  187. } catch (\Zend\Config\Exception\RuntimeException $e) {
  188. // read exception stack
  189. do {
  190. $stack[] = $e;
  191. } while ( ($e = $e->getPrevious()) );
  192. // test two thrown xml errors
  193. $this->assertEquals(2, count($stack));
  194. $this->assertContains('tag mismatch', $stack[0]->getMessage());
  195. $this->assertContains('undefined', $stack[1]->getMessage());
  196. }
  197. }
  198. /**
  199. * @group ZF-3578
  200. */
  201. public function testMissingXmlFile()
  202. {
  203. $this->setExpectedException('Zend\Config\Exception\RuntimeException', 'failed to load');
  204. $config = new Xml('I/dont/exist');
  205. }
  206. public function testShortParamsOne()
  207. {
  208. $config = new Xml($this->_xmlFileShortParamsOneConfig, 'all');
  209. $this->assertEquals('all', $config->hostname);
  210. $this->assertEquals('thisname', $config->name);
  211. $this->assertEquals('username', $config->db->user);
  212. $this->assertEquals('live', $config->db->name);
  213. $this->assertEquals('multi', $config->one->two->three);
  214. }
  215. public function testShortParamsTwo()
  216. {
  217. $config = new Xml($this->_xmlFileShortParamsTwoConfig, 'all');
  218. $this->assertEquals('all', $config->hostname);
  219. $this->assertEquals('thisname', $config->name);
  220. $this->assertEquals('username', $config->db->user);
  221. $this->assertEquals('live', $config->db->name);
  222. $this->assertEquals('multi', $config->one->two->three);
  223. }
  224. public function testConstants()
  225. {
  226. if (!defined('ZEND_CONFIG_XML_TEST_CONSTANT')) {
  227. define('ZEND_CONFIG_XML_TEST_CONSTANT', 'test');
  228. }
  229. $string = <<<EOT
  230. <?xml version="1.0"?>
  231. <config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  232. <all>
  233. <foo>foo-<zf:const zf:name="ZEND_CONFIG_XML_TEST_CONSTANT"/>-bar-<zf:const zf:name="ZEND_CONFIG_XML_TEST_CONSTANT"/></foo>
  234. <bar><const name="ZEND_CONFIG_XML_TEST_CONSTANT"/></bar>
  235. </all>
  236. </config>
  237. EOT;
  238. $config = new Xml($string, 'all');
  239. $this->assertEquals('foo-test-bar-test', $config->foo);
  240. $this->assertEquals('ZEND_CONFIG_XML_TEST_CONSTANT', $config->bar->const->name);
  241. }
  242. public function testNonExistentConstant()
  243. {
  244. $string = <<<EOT
  245. <?xml version="1.0"?>
  246. <config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  247. <all>
  248. <foo>foo-<zf:const zf:name="ZEND_CONFIG_XML_TEST_NON_EXISTENT_CONSTANT"/></foo>
  249. </all>
  250. </config>
  251. EOT;
  252. $this->setExpectedException('Zend\Config\Exception\RuntimeException', "Constant with name 'ZEND_CONFIG_XML_TEST_NON_EXISTENT_CONSTANT' was not defined");
  253. $config = new Xml($string, 'all');
  254. }
  255. public function testNamespacedExtends()
  256. {
  257. $string = <<<EOT
  258. <?xml version="1.0"?>
  259. <config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  260. <all>
  261. <foo>bar</foo>
  262. </all>
  263. <staging zf:extends="all"/>
  264. </config>
  265. EOT;
  266. $config = new Xml($string);
  267. $this->assertEquals('bar', $config->staging->foo);
  268. }
  269. /**
  270. * @group ZF-3702
  271. */
  272. public function testLoadAnXMLString()
  273. {
  274. $string = <<<EOT
  275. <?xml version="1.0"?>
  276. <config>
  277. <all>
  278. <hostname>all</hostname>
  279. <db>
  280. <host>127.0.0.1</host>
  281. <name>live</name>
  282. </db>
  283. </all>
  284. <staging extends="all">
  285. <hostname>staging</hostname>
  286. <db>
  287. <name>dbstaging</name>
  288. </db>
  289. <debug>false</debug>
  290. </staging>
  291. </config>
  292. EOT;
  293. $config = new Xml($string, 'staging');
  294. $this->assertEquals('staging', $config->hostname);
  295. }
  296. /**
  297. * @group ZF-5800
  298. */
  299. public function testArraysOfKeysCreatedUsingAttributesAndKeys()
  300. {
  301. $string = <<<EOT
  302. <?xml version="1.0"?>
  303. <config>
  304. <rec>
  305. <receiver>
  306. <mail>user1@company.com</mail>
  307. <name>User Name</name>
  308. <html>1</html>
  309. </receiver>
  310. </rec>
  311. <dev extends="rec">
  312. <receiver mail="nice.guy@company.com" name="Nice Guy" />
  313. <receiver mail="fred@company.com" html="2" />
  314. </dev>
  315. </config>
  316. EOT;
  317. $config = new Xml($string, 'dev');
  318. $this->assertEquals('nice.guy@company.com', $config->receiver->{0}->mail);
  319. $this->assertEquals('1', $config->receiver->{0}->html);
  320. $this->assertNull($config->receiver->mail);
  321. }
  322. }