1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-12 17:37:21 +01:00
PHP-Parser/test/PhpParser/CodeParsingTest.php

81 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace PhpParser;
require_once __DIR__ . '/CodeTestAbstract.php';
class CodeParsingTest extends CodeTestAbstract
{
/**
* @dataProvider provideTestParse
*/
public function testParse($name, $code, $expected, $modeLine) {
if (null !== $modeLine) {
$modes = array_fill_keys(explode(',', $modeLine), true);
} else {
$modes = [];
}
2017-01-19 22:23:19 +01:00
list($parser5, $parser7) = $this->createParsers($modes);
$output5 = $this->getParseOutput($parser5, $code, $modes);
$output7 = $this->getParseOutput($parser7, $code, $modes);
if (isset($modes['php5'])) {
$this->assertSame($expected, $output5, $name);
$this->assertNotSame($expected, $output7, $name);
} else if (isset($modes['php7'])) {
$this->assertNotSame($expected, $output5, $name);
$this->assertSame($expected, $output7, $name);
} else {
$this->assertSame($expected, $output5, $name);
$this->assertSame($expected, $output7, $name);
}
}
2017-01-19 22:23:19 +01:00
public function createParsers(array $modes) {
$parserOptions = [
'useIdentifierNodes' => isset($modes['ident']),
];
$lexer = new Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
)));
return [
new Parser\Php5($lexer, $parserOptions),
new Parser\Php7($lexer, $parserOptions),
];
}
public function getParseOutput(Parser $parser, $code, array $modes) {
$dumpPositions = isset($modes['positions']);
$errors = new ErrorHandler\Collecting;
$stmts = $parser->parse($code, $errors);
$output = '';
foreach ($errors->getErrors() as $error) {
$output .= $this->formatErrorMessage($error, $code) . "\n";
}
if (null !== $stmts) {
$dumper = new NodeDumper(['dumpComments' => true, 'dumpPositions' => $dumpPositions]);
$output .= $dumper->dump($stmts, $code);
}
return canonicalize($output);
}
public function provideTestParse() {
return $this->getTests(__DIR__ . '/../code/parser', 'test');
}
private function formatErrorMessage(Error $e, $code) {
if ($e->hasColumnInfo()) {
2016-09-30 18:24:43 +02:00
return $e->getMessageWithColumnInfo($code);
} else {
return $e->getMessage();
}
}
}