/tests/unit/suite/libraries/joomla/utilities/JSimpleXMLTest.php

https://bitbucket.org/talueses/joomla-cms · PHP · 238 lines · 129 code · 12 blank · 97 comment · 1 complexity · f5b0ec07be4c5323ea263529d86bf911 MD5 · raw file

  1. <?php
  2. /**
  3. * JSimpleXMLTest.php -- unit testing file for JSimpleXML
  4. *
  5. * @package Joomla.UnitTest
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. require_once JPATH_BASE.'/tests/unit/JoomlaTestCase.php';
  10. /**
  11. * Test class for JSimpleXML.
  12. * Generated by PHPUnit on 2009-10-26 at 22:30:14.
  13. *
  14. * @package Joomla.UnitTest
  15. * @subpackage Utilities
  16. *
  17. */
  18. class JSimpleXMLTest extends JoomlaTestCase
  19. {
  20. /**
  21. * @var JSimpleXML
  22. */
  23. protected $object;
  24. /**
  25. * Receives the callback from JError and logs the required error information for the test.
  26. *
  27. * @param JException The JException object from JError
  28. *
  29. * @return bool To not continue with JError processing
  30. */
  31. static function errorCallback( $error )
  32. {
  33. JSimpleXMLTest::$actualError['code'] = $error->get('code');
  34. JSimpleXMLTest::$actualError['msg'] = $error->get('message');
  35. JSimpleXMLTest::$actualError['info'] = $error->get('info');
  36. return false;
  37. }
  38. /**
  39. * Sets up the fixture, for example, opens a network connection.
  40. * This method is called before a test is executed.
  41. *
  42. * @return void
  43. */
  44. protected function setUp()
  45. {
  46. include_once JPATH_BASE . '/libraries/joomla/utilities/simplexml.php';
  47. //parent::setUp();
  48. $this->saveErrorHandlers();
  49. $this->setErrorCallback('JSimpleXMLTest');
  50. JSimpleXMLTest::$actualError = array();
  51. $this->object = new JSimpleXML( array(XML_OPTION_SKIP_WHITE => true) );
  52. }
  53. /**
  54. * Tears down the fixture, for example, closes a network connection.
  55. * This method is called after a test is executed.
  56. *
  57. * @return void
  58. */
  59. protected function tearDown()
  60. {
  61. $this->setErrorhandlers($this->savedErrorState);
  62. }
  63. /**
  64. * Test cases for loadString
  65. *
  66. * @return array
  67. *
  68. */
  69. function casesLoadString()
  70. {
  71. return array(
  72. 'simple' => array(
  73. '<one><two>fred</two></one>',
  74. true,
  75. array(),
  76. ),
  77. 'bad' => array(
  78. '<one><two>fred</one>',
  79. true,
  80. array(
  81. 'code' => 'SOME_ERROR_CODE',
  82. 'msg' => 'XML Parsing Error at 1:21. Error 76: Mismatched tag',
  83. 'info' => '',
  84. ),
  85. ),
  86. 'larger' => array(
  87. '<one><two>fred</two><three>fred<ten>Next</ten></three><four>fred</four></one>',
  88. true,
  89. array(),
  90. ),
  91. );
  92. }
  93. /**
  94. * Testing loadString().
  95. *
  96. * @param string The XML document structure.
  97. * @param boolean The expected return from the function.
  98. * @param array The error information array generated by the JError stub.
  99. *
  100. * @return void
  101. * @dataProvider casesLoadString
  102. */
  103. public function testLoadString( $xml, $expected, $error )
  104. {
  105. $this->assertThat(
  106. $this->object->loadString($xml),
  107. $this->equalTo($expected)
  108. );
  109. $this->assertThat(
  110. JSimpleXMLTest::$actualError,
  111. $this->equalTo($error)
  112. );
  113. }
  114. /**
  115. * Test case for loadFile
  116. *
  117. * @return array
  118. */
  119. function casesLoadFile()
  120. {
  121. return array(
  122. 'good' => array(
  123. JPATH_BASE. '/tests/unit/stubs/xmlFile.xml',
  124. true,
  125. 'JSimpleXMLElement',
  126. ),
  127. 'bad' => array(
  128. JPATH_BASE. '/tests/unit/stubs/fred.xml',
  129. false,
  130. null,
  131. ),
  132. 'empty' => array(
  133. JPATH_BASE. '/tests/unit/stubs/empty.xml',
  134. false,
  135. null,
  136. ),
  137. );
  138. }
  139. /**
  140. * Testing testLoadFile().
  141. *
  142. * @param string Path to xml
  143. * @param bool Result of load
  144. * @param string Class of result
  145. *
  146. * @return void
  147. * @dataProvider casesLoadFile
  148. */
  149. public function testLoadFile( $path, $expected, $class )
  150. {
  151. $this->assertThat(
  152. is_null($this->object->document),
  153. $this->isTrue()
  154. );
  155. $this->assertThat(
  156. $this->object->loadFile($path),
  157. $this->equalTo($expected)
  158. );
  159. if (!is_null($class))
  160. {
  161. $this->assertThat(
  162. $this->object->document,
  163. $this->isInstanceOf($class)
  164. );
  165. }
  166. else
  167. {
  168. $this->assertThat(
  169. $this->object->document,
  170. $this->isNull()
  171. );
  172. }
  173. }
  174. /**
  175. * Testing testImportDOM().
  176. *
  177. * This function was a puzzle to test, because it doesn't seem to have
  178. * any reason at all for existing. In the end, I decided that since the
  179. * actions of it were allegedly similar to loadString, we should use the same
  180. * sort of testing approach should be used. But, since the code itself does
  181. * nothing at the moment but return false, that would be all we test for
  182. * and that way of the function is ever activated in the future, the test
  183. * would flag it and new tests could be written.
  184. *
  185. * @param string DOM string to parse
  186. * @param boolean Function always seems to return false
  187. * @param array Error structure
  188. *
  189. * @return void
  190. * @dataProvider casesLoadString
  191. */
  192. public function testImportDOM( $xml, $expected, $error )
  193. {
  194. $this->assertThat(
  195. $this->object->importDOM($xml),
  196. $this->isFalse()
  197. );
  198. }
  199. /**
  200. * Testing testGetParser().
  201. *
  202. * @return void
  203. */
  204. public function testGetParser()
  205. {
  206. $this->assertThat(
  207. get_resource_type($this->object->getparser()),
  208. $this->equalTo('xml')
  209. );
  210. }
  211. /**
  212. * Testing testSetParser().
  213. *
  214. * @return void
  215. */
  216. public function testSetParser()
  217. {
  218. $parser = xml_parser_create('');
  219. $this->object->setParser($parser);
  220. $this->assertThat(
  221. $this->object->getparser(),
  222. $this->equalTo($parser)
  223. );
  224. }
  225. }
  226. ?>