XMLTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace PhpParser\Serializer;
  3. use PhpParser;
  4. class XMLTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @covers PhpParser\Serializer\XML<extended>
  8. */
  9. public function testSerialize() {
  10. $code = <<<CODE
  11. <?php
  12. // comment
  13. /** doc comment */
  14. function functionName(&\$a = 0, \$b = 1.0) {
  15. echo 'Foo';
  16. }
  17. CODE;
  18. $xml = <<<XML
  19. <?xml version="1.0" encoding="UTF-8"?>
  20. <AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
  21. <scalar:array>
  22. <node:Stmt_Function>
  23. <attribute:startLine>
  24. <scalar:int>4</scalar:int>
  25. </attribute:startLine>
  26. <attribute:comments>
  27. <scalar:array>
  28. <comment isDocComment="false" line="2">// comment
  29. </comment>
  30. <comment isDocComment="true" line="3">/** doc comment */</comment>
  31. </scalar:array>
  32. </attribute:comments>
  33. <attribute:endLine>
  34. <scalar:int>6</scalar:int>
  35. </attribute:endLine>
  36. <subNode:byRef>
  37. <scalar:false/>
  38. </subNode:byRef>
  39. <subNode:name>
  40. <scalar:string>functionName</scalar:string>
  41. </subNode:name>
  42. <subNode:params>
  43. <scalar:array>
  44. <node:Param>
  45. <attribute:startLine>
  46. <scalar:int>4</scalar:int>
  47. </attribute:startLine>
  48. <attribute:endLine>
  49. <scalar:int>4</scalar:int>
  50. </attribute:endLine>
  51. <subNode:type>
  52. <scalar:null/>
  53. </subNode:type>
  54. <subNode:byRef>
  55. <scalar:true/>
  56. </subNode:byRef>
  57. <subNode:variadic>
  58. <scalar:false/>
  59. </subNode:variadic>
  60. <subNode:name>
  61. <scalar:string>a</scalar:string>
  62. </subNode:name>
  63. <subNode:default>
  64. <node:Scalar_LNumber>
  65. <attribute:startLine>
  66. <scalar:int>4</scalar:int>
  67. </attribute:startLine>
  68. <attribute:endLine>
  69. <scalar:int>4</scalar:int>
  70. </attribute:endLine>
  71. <attribute:kind>
  72. <scalar:int>10</scalar:int>
  73. </attribute:kind>
  74. <subNode:value>
  75. <scalar:int>0</scalar:int>
  76. </subNode:value>
  77. </node:Scalar_LNumber>
  78. </subNode:default>
  79. </node:Param>
  80. <node:Param>
  81. <attribute:startLine>
  82. <scalar:int>4</scalar:int>
  83. </attribute:startLine>
  84. <attribute:endLine>
  85. <scalar:int>4</scalar:int>
  86. </attribute:endLine>
  87. <subNode:type>
  88. <scalar:null/>
  89. </subNode:type>
  90. <subNode:byRef>
  91. <scalar:false/>
  92. </subNode:byRef>
  93. <subNode:variadic>
  94. <scalar:false/>
  95. </subNode:variadic>
  96. <subNode:name>
  97. <scalar:string>b</scalar:string>
  98. </subNode:name>
  99. <subNode:default>
  100. <node:Scalar_DNumber>
  101. <attribute:startLine>
  102. <scalar:int>4</scalar:int>
  103. </attribute:startLine>
  104. <attribute:endLine>
  105. <scalar:int>4</scalar:int>
  106. </attribute:endLine>
  107. <subNode:value>
  108. <scalar:float>1</scalar:float>
  109. </subNode:value>
  110. </node:Scalar_DNumber>
  111. </subNode:default>
  112. </node:Param>
  113. </scalar:array>
  114. </subNode:params>
  115. <subNode:returnType>
  116. <scalar:null/>
  117. </subNode:returnType>
  118. <subNode:stmts>
  119. <scalar:array>
  120. <node:Stmt_Echo>
  121. <attribute:startLine>
  122. <scalar:int>5</scalar:int>
  123. </attribute:startLine>
  124. <attribute:endLine>
  125. <scalar:int>5</scalar:int>
  126. </attribute:endLine>
  127. <subNode:exprs>
  128. <scalar:array>
  129. <node:Scalar_String>
  130. <attribute:startLine>
  131. <scalar:int>5</scalar:int>
  132. </attribute:startLine>
  133. <attribute:endLine>
  134. <scalar:int>5</scalar:int>
  135. </attribute:endLine>
  136. <attribute:kind>
  137. <scalar:int>1</scalar:int>
  138. </attribute:kind>
  139. <subNode:value>
  140. <scalar:string>Foo</scalar:string>
  141. </subNode:value>
  142. </node:Scalar_String>
  143. </scalar:array>
  144. </subNode:exprs>
  145. </node:Stmt_Echo>
  146. </scalar:array>
  147. </subNode:stmts>
  148. </node:Stmt_Function>
  149. </scalar:array>
  150. </AST>
  151. XML;
  152. $parser = new PhpParser\Parser\Php7(new PhpParser\Lexer);
  153. $serializer = new XML;
  154. $code = str_replace("\r\n", "\n", $code);
  155. $stmts = $parser->parse($code);
  156. $this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts));
  157. }
  158. /**
  159. * @expectedException \InvalidArgumentException
  160. * @expectedExceptionMessage Unexpected node type
  161. */
  162. public function testError() {
  163. $serializer = new XML;
  164. $serializer->serialize(array(new \stdClass));
  165. }
  166. }