XMLTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class PHPParser_Tests_Unserializer_XMLTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testNode() {
  5. $xml = <<<XML
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <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">
  8. <node:Scalar_String line="1" docComment="/** doc comment */">
  9. <attribute:startLine>
  10. <scalar:int>1</scalar:int>
  11. </attribute:startLine>
  12. <attribute:comments>
  13. <scalar:array>
  14. <comment isDocComment="false" line="2">// comment
  15. </comment>
  16. <comment isDocComment="true" line="3">/** doc comment */</comment>
  17. </scalar:array>
  18. </attribute:comments>
  19. <subNode:value>
  20. <scalar:string>Test</scalar:string>
  21. </subNode:value>
  22. </node:Scalar_String>
  23. </AST>
  24. XML;
  25. $unserializer = new PHPParser_Unserializer_XML;
  26. $this->assertEquals(
  27. new PHPParser_Node_Scalar_String('Test', array(
  28. 'startLine' => 1,
  29. 'comments' => array(
  30. new PHPParser_Comment('// comment' . "\n", 2),
  31. new PHPParser_Comment_Doc('/** doc comment */', 3),
  32. ),
  33. )),
  34. $unserializer->unserialize($xml)
  35. );
  36. }
  37. public function testEmptyNode() {
  38. $xml = <<<XML
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <AST xmlns:node="http://nikic.github.com/PHPParser/XML/node">
  41. <node:Scalar_ClassConst />
  42. </AST>
  43. XML;
  44. $unserializer = new PHPParser_Unserializer_XML;
  45. $this->assertEquals(
  46. new PHPParser_Node_Scalar_ClassConst,
  47. $unserializer->unserialize($xml)
  48. );
  49. }
  50. public function testScalars() {
  51. $xml = <<<XML
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
  54. <scalar:array>
  55. <scalar:array></scalar:array>
  56. <scalar:array/>
  57. <scalar:string>test</scalar:string>
  58. <scalar:string></scalar:string>
  59. <scalar:string/>
  60. <scalar:int>1</scalar:int>
  61. <scalar:float>1</scalar:float>
  62. <scalar:float>1.5</scalar:float>
  63. <scalar:true/>
  64. <scalar:false/>
  65. <scalar:null/>
  66. </scalar:array>
  67. </AST>
  68. XML;
  69. $result = array(
  70. array(), array(),
  71. 'test', '', '',
  72. 1,
  73. 1, 1.5,
  74. true, false, null
  75. );
  76. $unserializer = new PHPParser_Unserializer_XML;
  77. $this->assertEquals($result, $unserializer->unserialize($xml));
  78. }
  79. /**
  80. * @expectedException DomainException
  81. * @expectedExceptionMessage AST root element not found
  82. */
  83. public function testWrongRootElementError() {
  84. $xml = <<<XML
  85. <?xml version="1.0" encoding="UTF-8"?>
  86. <notAST/>
  87. XML;
  88. $unserializer = new PHPParser_Unserializer_XML;
  89. $unserializer->unserialize($xml);
  90. }
  91. /**
  92. * @dataProvider provideTestErrors
  93. */
  94. public function testErrors($xml, $errorMsg) {
  95. $this->setExpectedException('DomainException', $errorMsg);
  96. $xml = <<<XML
  97. <?xml version="1.0" encoding="UTF-8"?>
  98. <AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar"
  99. xmlns:node="http://nikic.github.com/PHPParser/XML/node"
  100. xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode"
  101. xmlns:foo="http://nikic.github.com/PHPParser/XML/foo">
  102. $xml
  103. </AST>
  104. XML;
  105. $unserializer = new PHPParser_Unserializer_XML;
  106. $unserializer->unserialize($xml);
  107. }
  108. public function provideTestErrors() {
  109. return array(
  110. array('<scalar:true>test</scalar:true>', '"true" scalar must be empty'),
  111. array('<scalar:false>test</scalar:false>', '"false" scalar must be empty'),
  112. array('<scalar:null>test</scalar:null>', '"null" scalar must be empty'),
  113. array('<scalar:foo>bar</scalar:foo>', 'Unknown scalar type "foo"'),
  114. array('<scalar:int>x</scalar:int>', '"x" is not a valid int'),
  115. array('<scalar:float>x</scalar:float>', '"x" is not a valid float'),
  116. array('', 'Expected node or scalar'),
  117. array('<foo:bar>test</foo:bar>', 'Unexpected node of type "foo:bar"'),
  118. array(
  119. '<node:Scalar_String><foo:bar>test</foo:bar></node:Scalar_String>',
  120. 'Expected sub node or attribute, got node of type "foo:bar"'
  121. ),
  122. array(
  123. '<node:Scalar_String><subNode:value/></node:Scalar_String>',
  124. 'Expected node or scalar'
  125. ),
  126. );
  127. }
  128. }