/cake/tests/cases/libs/view/helpers/xml.test.php

https://github.com/hardsshah/bookmarks · PHP · 252 lines · 129 code · 13 blank · 110 comment · 2 complexity · d9a07815628a10592dbf74e763b9293f MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * XmlHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.view.helpers
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  28. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  29. }
  30. App::import('Helper', 'Xml');
  31. /**
  32. * TestXml class
  33. *
  34. * @package cake
  35. * @subpackage cake.tests.cases.libs.view.helpers
  36. */
  37. class TestXml extends Object {
  38. /**
  39. * content property
  40. *
  41. * @var string ''
  42. * @access public
  43. */
  44. var $content = '';
  45. /**
  46. * construct method
  47. *
  48. * @param mixed $content
  49. * @access private
  50. * @return void
  51. */
  52. function __construct($content) {
  53. $this->content = $content;
  54. }
  55. /**
  56. * toString method
  57. *
  58. * @access public
  59. * @return void
  60. */
  61. function toString() {
  62. return $this->content;
  63. }
  64. }
  65. /**
  66. * XmlHelperTest class
  67. *
  68. * @package cake
  69. * @subpackage cake.tests.cases.libs.view.helpers
  70. */
  71. class XmlHelperTest extends CakeTestCase {
  72. /**
  73. * setUp method
  74. *
  75. * @access public
  76. * @return void
  77. */
  78. function setUp() {
  79. $this->Xml =& new XmlHelper();
  80. $this->Xml->beforeRender();
  81. $manager =& XmlManager::getInstance();
  82. $manager->namespaces = array();
  83. }
  84. /**
  85. * tearDown method
  86. *
  87. * @access public
  88. * @return void
  89. */
  90. function tearDown() {
  91. unset($this->Xml);
  92. }
  93. /**
  94. * testAddNamespace method
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. function testAddNamespace() {
  100. $this->Xml->addNs('custom', 'http://example.com/dtd.xml');
  101. $manager =& XmlManager::getInstance();
  102. $expected = array('custom' => 'http://example.com/dtd.xml');
  103. $this->assertEqual($manager->namespaces, $expected);
  104. }
  105. /**
  106. * testRemoveNamespace method
  107. *
  108. * @access public
  109. * @return void
  110. */
  111. function testRemoveNamespace() {
  112. $this->Xml->addNs('custom', 'http://example.com/dtd.xml');
  113. $this->Xml->addNs('custom2', 'http://example.com/dtd2.xml');
  114. $manager =& XmlManager::getInstance();
  115. $expected = array('custom' => 'http://example.com/dtd.xml', 'custom2' => 'http://example.com/dtd2.xml');
  116. $this->assertEqual($manager->namespaces, $expected);
  117. $this->Xml->removeNs('custom');
  118. $expected = array('custom2' => 'http://example.com/dtd2.xml');
  119. $this->assertEqual($manager->namespaces, $expected);
  120. }
  121. /**
  122. * testRenderZeroElement method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function testRenderZeroElement() {
  128. $result = $this->Xml->elem('count', null, 0);
  129. $expected = '<count>0</count>';
  130. $this->assertEqual($result, $expected);
  131. }
  132. /**
  133. * testRenderElementWithNamespace method
  134. *
  135. * @access public
  136. * @return void
  137. */
  138. function testRenderElementWithNamespace() {
  139. $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content');
  140. $expected = '<myNameSpace:count>content</myNameSpace:count>';
  141. $this->assertEqual($result, $expected);
  142. $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content', false);
  143. $expected = '<myNameSpace:count>content';
  144. $this->assertEqual($result, $expected);
  145. $expected .= '</myNameSpace:count>';
  146. $result .= $this->Xml->closeElem();
  147. $this->assertEqual($result, $expected);
  148. }
  149. /**
  150. * testRenderElementWithComplexContent method
  151. *
  152. * @access public
  153. * @return void
  154. */
  155. function testRenderElementWithComplexContent() {
  156. $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), array('contrived' => 'content'));
  157. $expected = '<myNameSpace:count><content /></myNameSpace:count>';
  158. $this->assertEqual($result, $expected);
  159. $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), array('cdata' => true, 'value' => 'content'));
  160. $expected = '<myNameSpace:count><![CDATA[content]]></myNameSpace:count>';
  161. $this->assertEqual($result, $expected);
  162. }
  163. /**
  164. * testSerialize method
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. function testSerialize() {
  170. $data = array(
  171. 'test1' => 'test with no quotes',
  172. 'test2' => 'test with "double quotes"'
  173. );
  174. $result = $this->Xml->serialize($data);
  175. $expected = '<std_class test1="test with no quotes" test2="test with &quot;double quotes&quot;" />';
  176. $this->assertIdentical($result, $expected);
  177. $data = array(
  178. 'test1' => 'test with no quotes',
  179. 'test2' => 'test without double quotes'
  180. );
  181. $result = $this->Xml->serialize($data);
  182. $expected = '<std_class test1="test with no quotes" test2="test without double quotes" />';
  183. $this->assertIdentical($result, $expected);
  184. $data = array(
  185. 'ServiceDay' => array('ServiceTime' => array('ServiceTimePrice' => array('dollar' => 1, 'cents' => '2')))
  186. );
  187. $result = $this->Xml->serialize($data);
  188. $expected = '<service_day><service_time><service_time_price dollar="1" cents="2" /></service_time></service_day>';
  189. $this->assertIdentical($result, $expected);
  190. $data = array(
  191. 'ServiceDay' => array('ServiceTime' => array('ServiceTimePrice' => array('dollar' => 1, 'cents' => '2')))
  192. );
  193. $result = $this->Xml->serialize($data, array('format' => 'tags'));
  194. $expected = '<service_day><service_time><service_time_price><dollar>1</dollar><cents>2</cents></service_time_price></service_time></service_day>';
  195. $this->assertIdentical($result, $expected);
  196. $data = array(
  197. 'Pages' => array('id' => 2, 'url' => 'http://www.url.com/rb/153/?id=bbbb&t=access')
  198. );
  199. $result = $this->Xml->serialize($data);
  200. $expected = '<pages id="2" url="http://www.url.com/rb/153/?id=bbbb&amp;t=access" />';
  201. $this->assertIdentical($result, $expected);
  202. }
  203. /**
  204. * testHeader method
  205. *
  206. * @access public
  207. * @return void
  208. */
  209. function testHeader() {
  210. $expectedDefaultEncoding = Configure::read('App.encoding');
  211. if (empty($expectedDefaultEncoding)) {
  212. $expectedDefaultEncoding = 'UTF-8';
  213. }
  214. $attrib = array();
  215. $result = $this->Xml->header($attrib);
  216. $expected = '<?xml version="1.0" encoding="'.$expectedDefaultEncoding.'" ?>';
  217. $this->assertIdentical($result, $expected);
  218. $attrib = array(
  219. 'encoding' => 'UTF-8',
  220. 'version' => '1.1'
  221. );
  222. $result = $this->Xml->header($attrib);
  223. $expected = '<?xml version="1.1" encoding="UTF-8" ?>';
  224. $this->assertIdentical($result, $expected);
  225. $attrib = array(
  226. 'encoding' => 'UTF-8',
  227. 'version' => '1.2',
  228. 'additional' => 'attribute'
  229. );
  230. $result = $this->Xml->header($attrib);
  231. $expected = '<?xml version="1.2" encoding="UTF-8" additional="attribute" ?>';
  232. $this->assertIdentical($result, $expected);
  233. $attrib = 'encoding="UTF-8" someOther="value"';
  234. $result = $this->Xml->header($attrib);
  235. $expected = '<?xml encoding="UTF-8" someOther="value" ?>';
  236. $this->assertIdentical($result, $expected);
  237. }
  238. }
  239. ?>