XMLTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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:comments>
  24. <scalar:array>
  25. <comment isDocComment="false" line="2">// comment
  26. </comment>
  27. <comment isDocComment="true" line="3">/** doc comment */</comment>
  28. </scalar:array>
  29. </attribute:comments>
  30. <attribute:startLine>
  31. <scalar:int>4</scalar:int>
  32. </attribute:startLine>
  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. <subNode:value>
  72. <scalar:int>0</scalar:int>
  73. </subNode:value>
  74. </node:Scalar_LNumber>
  75. </subNode:default>
  76. </node:Param>
  77. <node:Param>
  78. <attribute:startLine>
  79. <scalar:int>4</scalar:int>
  80. </attribute:startLine>
  81. <attribute:endLine>
  82. <scalar:int>4</scalar:int>
  83. </attribute:endLine>
  84. <subNode:type>
  85. <scalar:null/>
  86. </subNode:type>
  87. <subNode:byRef>
  88. <scalar:false/>
  89. </subNode:byRef>
  90. <subNode:variadic>
  91. <scalar:false/>
  92. </subNode:variadic>
  93. <subNode:name>
  94. <scalar:string>b</scalar:string>
  95. </subNode:name>
  96. <subNode:default>
  97. <node:Scalar_DNumber>
  98. <attribute:startLine>
  99. <scalar:int>4</scalar:int>
  100. </attribute:startLine>
  101. <attribute:endLine>
  102. <scalar:int>4</scalar:int>
  103. </attribute:endLine>
  104. <subNode:value>
  105. <scalar:float>1</scalar:float>
  106. </subNode:value>
  107. </node:Scalar_DNumber>
  108. </subNode:default>
  109. </node:Param>
  110. </scalar:array>
  111. </subNode:params>
  112. <subNode:returnType>
  113. <scalar:null/>
  114. </subNode:returnType>
  115. <subNode:stmts>
  116. <scalar:array>
  117. <node:Stmt_Echo>
  118. <attribute:startLine>
  119. <scalar:int>5</scalar:int>
  120. </attribute:startLine>
  121. <attribute:endLine>
  122. <scalar:int>5</scalar:int>
  123. </attribute:endLine>
  124. <subNode:exprs>
  125. <scalar:array>
  126. <node:Scalar_String>
  127. <attribute:startLine>
  128. <scalar:int>5</scalar:int>
  129. </attribute:startLine>
  130. <attribute:endLine>
  131. <scalar:int>5</scalar:int>
  132. </attribute:endLine>
  133. <subNode:value>
  134. <scalar:string>Foo</scalar:string>
  135. </subNode:value>
  136. </node:Scalar_String>
  137. </scalar:array>
  138. </subNode:exprs>
  139. </node:Stmt_Echo>
  140. </scalar:array>
  141. </subNode:stmts>
  142. </node:Stmt_Function>
  143. </scalar:array>
  144. </AST>
  145. XML;
  146. $parser = new PhpParser\Parser(new PhpParser\Lexer);
  147. $serializer = new XML;
  148. $stmts = $parser->parse($code);
  149. $this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts));
  150. }
  151. /**
  152. * @expectedException \InvalidArgumentException
  153. * @expectedExceptionMessage Unexpected node type
  154. */
  155. public function testError() {
  156. $serializer = new XML;
  157. $serializer->serialize(array(new \stdClass));
  158. }
  159. }