XMLTest.php 4.6 KB

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