XMLTest.php 4.1 KB

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