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

/test/testsuite/runtime/parser/PropelXMLParserTest.php

https://github.com/1989gaurav/Propel
PHP | 198 lines | 154 code | 13 blank | 31 comment | 0 complexity | 8e207cdb2e17d84c3cd9b0de465657f6 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../runtime/lib/parser/PropelParser.php';
  10. require_once dirname(__FILE__) . '/../../../../runtime/lib/parser/PropelXMLParser.php';
  11. /**
  12. * Test for PropelXMLParser class
  13. *
  14. * @author Francois Zaninotto
  15. * @package runtime.parser
  16. */
  17. class PropelXMLParserTest extends PHPUnit_Framework_TestCase
  18. {
  19. public static function arrayXmlConversionDataProvider()
  20. {
  21. return array(
  22. array(array(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  23. <data/>
  24. ", 'empty array'),
  25. array(array('a' => 1, 'b' => 2), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  26. <data>
  27. <a>1</a>
  28. <b>2</b>
  29. </data>
  30. ", 'associative array'),
  31. array(array('a' => 0, 'b' => null, 'c' => ''), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  32. <data>
  33. <a>0</a>
  34. <b></b>
  35. <c><![CDATA[]]></c>
  36. </data>
  37. ", 'associative array with empty values'),
  38. array(array('a' => 1, 'b' => 'bar'), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  39. <data>
  40. <a>1</a>
  41. <b><![CDATA[bar]]></b>
  42. </data>
  43. ", 'associative array with strings'),
  44. array(array('a' => '<html><body><p style="width:30px;">Hello, World!</p></body></html>'), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  45. <data>
  46. <a><![CDATA[&lt;html&gt;&lt;body&gt;&lt;p style=&quot;width:30px;&quot;&gt;Hello, World!&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;]]></a>
  47. </data>
  48. ", 'associative array with code'),
  49. array(array('a' => 1, 'b' => array('foo' => 2)), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  50. <data>
  51. <a>1</a>
  52. <b>
  53. <foo>2</foo>
  54. </b>
  55. </data>
  56. ", 'nested associative arrays'),
  57. array(array('Id' => 123, 'Title' => 'Pride and Prejudice', 'AuthorId' => 456, 'ISBN' => '0553213105', 'Author' => array('Id' => 456, 'FirstName' => 'Jane', 'LastName' => 'Austen')), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  58. <data>
  59. <Id>123</Id>
  60. <Title><![CDATA[Pride and Prejudice]]></Title>
  61. <AuthorId>456</AuthorId>
  62. <ISBN><![CDATA[0553213105]]></ISBN>
  63. <Author>
  64. <Id>456</Id>
  65. <FirstName><![CDATA[Jane]]></FirstName>
  66. <LastName><![CDATA[Austen]]></LastName>
  67. </Author>
  68. </data>
  69. ", 'array resulting from an object conversion'),
  70. array(array('a1' => 1, 'b2' => 2), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  71. <data>
  72. <a1>1</a1>
  73. <b2>2</b2>
  74. </data>
  75. ", 'keys with numbers'),
  76. );
  77. }
  78. /**
  79. * @dataProvider arrayXmlConversionDataProvider
  80. */
  81. public function testFromArray($arrayData, $xmlData, $type)
  82. {
  83. $parser = new PropelXMLParser();
  84. $this->assertEquals($xmlData, $parser->fromArray($arrayData), 'PropelXMLParser::fromArray() converts from ' . $type . ' correctly');
  85. }
  86. /**
  87. * @dataProvider arrayXmlConversionDataProvider
  88. */
  89. public function testToXML($arrayData, $xmlData, $type)
  90. {
  91. $parser = new PropelXMLParser();
  92. $this->assertEquals($xmlData, $parser->toXML($arrayData), 'PropelXMLParser::toXML() converts from ' . $type . ' correctly');
  93. }
  94. /**
  95. * @dataProvider arrayXmlConversionDataProvider
  96. */
  97. public function testToArray($arrayData, $xmlData, $type)
  98. {
  99. $parser = new PropelXMLParser();
  100. $this->assertEquals($arrayData, $parser->toArray($xmlData), 'PropelXMLParser::toArray() converts to ' . $type . ' correctly');
  101. }
  102. /**
  103. * @dataProvider arrayXmlConversionDataProvider
  104. */
  105. public function testFromXML($arrayData, $xmlData, $type)
  106. {
  107. $parser = new PropelXMLParser();
  108. $this->assertEquals($arrayData, $parser->fromXML($xmlData), 'PropelXMLParser::fromXML() converts to ' . $type . ' correctly');
  109. }
  110. public function testToArrayRespectsNullValues()
  111. {
  112. $xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  113. <data>
  114. <Id></Id>
  115. <Title><![CDATA[]]></Title>
  116. </data>";
  117. $parser = new PropelXMLParser();
  118. $data = $parser->fromXML($xmlData);
  119. $this->assertNull($data['Id']);
  120. $this->assertSame('', $data['Title']);
  121. }
  122. public static function listToXMLDataProvider()
  123. {
  124. $list = array(
  125. 'book0' => array('Id' => 123, 'Title' => 'Pride and Prejudice', 'AuthorId' => 456, 'ISBN' => '0553213105', 'Author' => array('Id' => 456, 'FirstName' => 'Jane', 'LastName' => 'Austen')),
  126. 'book1' => array('Id' => 82, 'Title' => 'Anna Karenina', 'AuthorId' => 543, 'ISBN' => '0143035002', 'Author' => array('Id' => 543, 'FirstName' => 'Leo', 'LastName' => 'Tolstoi')),
  127. 'book2' => array('Id' => 567, 'Title' => 'War and Peace', 'AuthorId' => 543, 'ISBN' => '067003469X', 'Author' => array('Id' => 543, 'FirstName' => 'Leo', 'LastName' => 'Tolstoi')),
  128. );
  129. $xml = <<<EOF
  130. <?xml version="1.0" encoding="UTF-8"?>
  131. <data>
  132. <book>
  133. <Id>123</Id>
  134. <Title><![CDATA[Pride and Prejudice]]></Title>
  135. <AuthorId>456</AuthorId>
  136. <ISBN><![CDATA[0553213105]]></ISBN>
  137. <Author>
  138. <Id>456</Id>
  139. <FirstName><![CDATA[Jane]]></FirstName>
  140. <LastName><![CDATA[Austen]]></LastName>
  141. </Author>
  142. </book>
  143. <book>
  144. <Id>82</Id>
  145. <Title><![CDATA[Anna Karenina]]></Title>
  146. <AuthorId>543</AuthorId>
  147. <ISBN><![CDATA[0143035002]]></ISBN>
  148. <Author>
  149. <Id>543</Id>
  150. <FirstName><![CDATA[Leo]]></FirstName>
  151. <LastName><![CDATA[Tolstoi]]></LastName>
  152. </Author>
  153. </book>
  154. <book>
  155. <Id>567</Id>
  156. <Title><![CDATA[War and Peace]]></Title>
  157. <AuthorId>543</AuthorId>
  158. <ISBN><![CDATA[067003469X]]></ISBN>
  159. <Author>
  160. <Id>543</Id>
  161. <FirstName><![CDATA[Leo]]></FirstName>
  162. <LastName><![CDATA[Tolstoi]]></LastName>
  163. </Author>
  164. </book>
  165. </data>
  166. EOF;
  167. return array(array($list, $xml));
  168. }
  169. /**
  170. * @dataProvider listToXMLDataProvider
  171. */
  172. public function testListToXML($list, $xml)
  173. {
  174. $parser = new PropelXMLParser();
  175. $this->assertEquals($xml, $parser->listToXML($list));
  176. }
  177. /**
  178. * @dataProvider listToXMLDataProvider
  179. */
  180. public function testXMLToList($list, $xml)
  181. {
  182. $parser = new PropelXMLParser();
  183. $this->assertEquals($list, $parser->fromXML($xml));
  184. }
  185. }