CodeParsingTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Stmt;
  5. class CodeParsingTest extends CodeTestAbstract
  6. {
  7. /**
  8. * @dataProvider provideTestParse
  9. */
  10. public function testParse($name, $code, $expected, $modeLine) {
  11. if (null !== $modeLine) {
  12. $modes = array_fill_keys(explode(',', $modeLine), true);
  13. } else {
  14. $modes = [];
  15. }
  16. list($parser5, $parser7) = $this->createParsers($modes);
  17. list($stmts5, $output5) = $this->getParseOutput($parser5, $code, $modes);
  18. list($stmts7, $output7) = $this->getParseOutput($parser7, $code, $modes);
  19. if (isset($modes['php5'])) {
  20. $this->assertSame($expected, $output5, $name);
  21. $this->assertNotSame($expected, $output7, $name);
  22. } elseif (isset($modes['php7'])) {
  23. $this->assertNotSame($expected, $output5, $name);
  24. $this->assertSame($expected, $output7, $name);
  25. } else {
  26. $this->assertSame($expected, $output5, $name);
  27. $this->assertSame($expected, $output7, $name);
  28. }
  29. $this->checkAttributes($stmts5);
  30. $this->checkAttributes($stmts7);
  31. }
  32. public function createParsers(array $modes) {
  33. $lexer = new Lexer\Emulative(['usedAttributes' => [
  34. 'startLine', 'endLine',
  35. 'startFilePos', 'endFilePos',
  36. 'startTokenPos', 'endTokenPos',
  37. 'comments'
  38. ]]);
  39. return [
  40. new Parser\Php5($lexer),
  41. new Parser\Php7($lexer),
  42. ];
  43. }
  44. // Must be public for updateTests.php
  45. public function getParseOutput(Parser $parser, $code, array $modes) {
  46. $dumpPositions = isset($modes['positions']);
  47. $errors = new ErrorHandler\Collecting;
  48. $stmts = $parser->parse($code, $errors);
  49. $output = '';
  50. foreach ($errors->getErrors() as $error) {
  51. $output .= $this->formatErrorMessage($error, $code) . "\n";
  52. }
  53. if (null !== $stmts) {
  54. $dumper = new NodeDumper(['dumpComments' => true, 'dumpPositions' => $dumpPositions]);
  55. $output .= $dumper->dump($stmts, $code);
  56. }
  57. return [$stmts, canonicalize($output)];
  58. }
  59. public function provideTestParse() {
  60. return $this->getTests(__DIR__ . '/../code/parser', 'test');
  61. }
  62. private function formatErrorMessage(Error $e, $code) {
  63. if ($e->hasColumnInfo()) {
  64. return $e->getMessageWithColumnInfo($code);
  65. }
  66. return $e->getMessage();
  67. }
  68. private function checkAttributes($stmts) {
  69. if ($stmts === null) {
  70. return;
  71. }
  72. $traverser = new NodeTraverser();
  73. $traverser->addVisitor(new class extends NodeVisitorAbstract {
  74. public function enterNode(Node $node) {
  75. $startLine = $node->getStartLine();
  76. $endLine = $node->getEndLine();
  77. $startFilePos = $node->getStartFilePos();
  78. $endFilePos = $node->getEndFilePos();
  79. $startTokenPos = $node->getStartTokenPos();
  80. $endTokenPos = $node->getEndTokenPos();
  81. if ($startLine < 0 || $endLine < 0 ||
  82. $startFilePos < 0 || $endFilePos < 0 ||
  83. $startTokenPos < 0 || $endTokenPos < 0
  84. ) {
  85. throw new \Exception('Missing location information on ' . $node->getType());
  86. }
  87. if ($endLine < $startLine ||
  88. $endFilePos < $startFilePos ||
  89. $endTokenPos < $startTokenPos
  90. ) {
  91. // Nops and error can have inverted order, if they are empty
  92. if (!$node instanceof Stmt\Nop && !$node instanceof Expr\Error) {
  93. throw new \Exception('End < start on ' . $node->getType());
  94. }
  95. }
  96. }
  97. });
  98. $traverser->traverse($stmts);
  99. }
  100. }