PageRenderTime 85ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suite/joomla/utilities/JSimpleXMLElementTest.php

http://github.com/joomla/joomla-platform
PHP | 388 lines | 226 code | 28 blank | 134 comment | 1 complexity | e2d58afd2a61a9694c351e07bbcf21bc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Utilities
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. require_once JPATH_PLATFORM. '/joomla/utilities/simplexml.php';
  10. /**
  11. * mapCallback is the function called by the map routine. It simply
  12. * records where it's been in the report variable for the testMap
  13. * method to check after the run.
  14. *
  15. * @param JSimpleXMLElement The object that is calling
  16. * @param array An array of passed arguments
  17. *
  18. * @return void
  19. */
  20. function mapCallback( $object, $arguments )
  21. {
  22. JSimpleXMLElementTest::$report[] = $arguments['msg'] . $object->name();
  23. }
  24. /**
  25. * Test class for JSimpleXMLElement.
  26. * Generated by PHPUnit on 2009-11-05 at 13:01:47.
  27. *
  28. * @package Joomla.UnitTest
  29. * @subpackage Utilities
  30. */
  31. class JSimpleXMLElementTest extends PHPUnit_Framework_TestCase
  32. {
  33. /**
  34. * @var JSimpleXMLElement
  35. */
  36. protected $object;
  37. /**
  38. * @var array
  39. */
  40. protected $child;
  41. /**
  42. * @var array
  43. */
  44. static $report;
  45. /**
  46. * Sets up the fixture, for example, opens a network connection.
  47. * This method is called before a test is executed.
  48. *
  49. * @return void
  50. */
  51. protected function setUp()
  52. {
  53. $this->object = new JSimpleXMLElement('Test');
  54. $this->report = array();
  55. $this->child = array();
  56. }
  57. /**
  58. * Tears down the fixture, for example, closes a network connection.
  59. * This method is called after a test is executed.
  60. *
  61. * @return void
  62. */
  63. protected function tearDown()
  64. {
  65. }
  66. /**
  67. * Builds a non-trivial XML document tree
  68. *
  69. * @return void
  70. */
  71. protected function buildXMLTree()
  72. {
  73. $this->child[0] = null;
  74. $this->child[1] = $this->object->addChild('Child1');
  75. $this->child[2] = $this->child[1]->addChild('Child2');
  76. $this->child[2]->setData('height');
  77. $this->child[2]->addAttribute('height', 300);
  78. $this->child[3] = $this->child[2]->addChild('Child3');
  79. $this->child[4] = $this->object->addChild('Child4');
  80. $this->child[4]->setData('Fred');
  81. }
  82. /**
  83. * Test cases for building XML elements
  84. *
  85. * @return array
  86. */
  87. function casesBuild()
  88. {
  89. return array(
  90. 'basic' => array(
  91. 'basic',
  92. array(XML_OPTION_SKIP_WHITE => true),
  93. 0,
  94. 'basic',
  95. array(XML_OPTION_SKIP_WHITE => true),
  96. true,
  97. 0,
  98. ),
  99. 'defaults' => array(
  100. 'default',
  101. null,
  102. null,
  103. 'default',
  104. array(),
  105. false,
  106. 0,
  107. ),
  108. );
  109. }
  110. /**
  111. * Testing testBuildingXMLElements().
  112. *
  113. * @param string Name of XML element to build
  114. * @param array Array of attributes to add to element
  115. * @param int Level of added node
  116. * @param string Expected name to retrieve from Element
  117. * @param array Expected array of attributes and values assigned to element
  118. * @param bool Expected value for the "Skip White" attribute
  119. * @param int Expected value for level of node
  120. *
  121. * @return void
  122. * @dataProvider casesBuild
  123. */
  124. public function testBuildingXMLElements( $name, $options, $level, $expName,
  125. $expAttr, $expSkip, $expLevel ) {
  126. if ( is_null($options) )
  127. {
  128. $this->object = new JSimpleXMLElement($name);
  129. }
  130. elseif ( is_null($level) )
  131. {
  132. $this->object = new JSimpleXMLElement($name, $options);
  133. }
  134. else
  135. {
  136. $this->object = new JSimpleXMLElement( $name, $options, $level );
  137. }
  138. $this->assertThat(
  139. $this->object->name(),
  140. $this->equalTo($expName)
  141. );
  142. $this->assertThat(
  143. $this->object->attributes(),
  144. $this->equalTo($expAttr)
  145. );
  146. $this->assertThat(
  147. $this->object->attributes(XML_OPTION_SKIP_WHITE),
  148. $this->equalTo($expSkip)
  149. );
  150. $this->assertThat(
  151. $this->object->level(),
  152. $this->equalTo($expLevel)
  153. );
  154. }
  155. /**
  156. * Test cases for manipulating attributes
  157. *
  158. * @return array
  159. */
  160. function casesAttributes()
  161. {
  162. return array(
  163. 'basic' => array(
  164. 'height',
  165. 300,
  166. ),
  167. );
  168. }
  169. /**
  170. * Testing Add/Remove Attribute
  171. *
  172. * @param string Name of attribute to add to node.
  173. * @param mixed Value to assign to added attribute.
  174. *
  175. * @return void
  176. * @dataProvider casesAttributes
  177. */
  178. function testAddAndRemoveAttributes( $attName, $attValue )
  179. {
  180. $this->assertThat(
  181. $this->object->attributes($attName),
  182. $this->equalTo(null)
  183. );
  184. $this->object->addAttribute($attName, $attValue);
  185. $this->assertThat(
  186. $this->object->attributes($attName),
  187. $this->equalTo($attValue)
  188. );
  189. $this->object->removeAttribute($attName);
  190. $this->assertThat(
  191. $this->object->attributes($attName),
  192. $this->equalTo(null)
  193. );
  194. }
  195. /**
  196. * Test cases for manipulating data
  197. *
  198. * @return array
  199. */
  200. function casesData()
  201. {
  202. return array(
  203. 'basic' => array(
  204. 'height',
  205. ),
  206. );
  207. }
  208. /**
  209. * Testing Add/Remove Data
  210. *
  211. * @param mixed Data to add to node and check for.
  212. *
  213. * @return void
  214. * @dataProvider casesData
  215. */
  216. function testAddAndRemoveData( $data )
  217. {
  218. $this->assertThat(
  219. $this->object->data(),
  220. $this->equalTo(null)
  221. );
  222. $this->object->setData($data);
  223. $this->assertThat(
  224. $this->object->data(),
  225. $this->equalTo($data)
  226. );
  227. }
  228. /**
  229. * Test cases for manipulating children
  230. *
  231. * @return array
  232. */
  233. function casesChildren()
  234. {
  235. return array(
  236. 'Just1' => array(
  237. 'first',
  238. array(
  239. 'Att1',
  240. 300
  241. ),
  242. ),
  243. );
  244. }
  245. /**
  246. * Test Add/Remove Children
  247. *
  248. * @param string Name of child node to add and check
  249. * @param array Key=>Value array of attributes for child node.
  250. *
  251. * @return void
  252. * @dataProvider casesChildren
  253. */
  254. function testAddAndRemoveChildren( $name, $options )
  255. {
  256. $this->assertThat(
  257. $this->object->children(),
  258. $this->equalTo(array())
  259. );
  260. $child = $this->object->addChild($name, $options);
  261. $this->assertThat(
  262. $child,
  263. $this->isInstanceOf('JSimpleXMLElement')
  264. );
  265. $this->assertThat(
  266. $child->attributes(),
  267. $this->equalTo($options)
  268. );
  269. $this->object->removeChild($child);
  270. $this->assertThat(
  271. $this->object->children(),
  272. $this->equalTo(array())
  273. );
  274. }
  275. /**
  276. * Test cases for getting element by path
  277. *
  278. * @return array
  279. */
  280. function casesPath()
  281. {
  282. return array(
  283. 'SuccessLC' => array(
  284. 'child1/child2/child3',
  285. 3,
  286. ),
  287. 'SuccessUC' => array(
  288. 'Child1/Child2/Child3',
  289. 3,
  290. ),
  291. 'Failure' => array(
  292. 'Child1/Child2/Child4',
  293. 0,
  294. ),
  295. );
  296. }
  297. /**
  298. * Testing getElementByPath().
  299. *
  300. * @param string The path ('/' as separator) to the XML node name
  301. * @param JSimpleXMLElement The index into the XMLTree ($child) of the sought node.
  302. *
  303. * @return void
  304. * @dataProvider casesPath
  305. */
  306. public function testGetElementByPath($path, $expected)
  307. {
  308. $this->buildXMLTree();
  309. $this->assertThat(
  310. $this->object->getElementByPath($path),
  311. $this->equalTo($this->child[$expected])
  312. );
  313. }
  314. /**
  315. * Testing testMap().
  316. *
  317. * @return void
  318. */
  319. public function testMap()
  320. {
  321. $this->buildXMLTree();
  322. $this->object->map('mapCallback', array( 'msg' => "Here " ));
  323. $this->assertThat(
  324. JSimpleXMLElementTest::$report,
  325. $this->equalTo(
  326. array(
  327. 'Here test',
  328. 'Here child1',
  329. 'Here child2',
  330. 'Here child3',
  331. 'Here child4'
  332. )
  333. )
  334. );
  335. }
  336. /**
  337. * Testing toString().
  338. *
  339. * @return void
  340. */
  341. public function testToString()
  342. {
  343. $this->buildXMLTree();
  344. $this->assertThat(
  345. $this->object->toString(),
  346. $this->equalTo(
  347. "\n<test>\n\t<child1>\n\t\t<child2 height=\"300\">\n\t\t\t<child3 />\n\t\t</child2>\n".
  348. "\t</child1>\n\t<child4>Fred</child4>\n</test>"
  349. ),
  350. "Test with whitespace turned on"
  351. );
  352. $this->assertThat(
  353. $this->object->toString(false),
  354. $this->equalTo(
  355. "<test><child1><child2 height=\"300\"><child3 /></child2></child1><child4>Fred</child4></test>"
  356. ),
  357. "Test without whitespace turned on"
  358. );
  359. }
  360. }