1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00
PHP-Parser/test/PhpParser/PrettyPrinterTest.php
Nikita Popov d8312a09a3 Split parsing tests into code tests and other stuff
And run the other stuff against Php5 and Php7 parsers.

Also move canonicalize() from CodeTestAbstract into a free-standing
function.
2015-06-20 11:44:29 +02:00

84 lines
2.8 KiB
PHP

<?php
namespace PhpParser;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\PrettyPrinter\Standard;
require_once __DIR__ . '/CodeTestAbstract.php';
class PrettyPrinterTest extends CodeTestAbstract
{
protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $mode) {
$lexer = new Lexer\Emulative;
$parser5 = new Parser\Php5($lexer);
$parser7 = new Parser\Php7($lexer);
$prettyPrinter = new Standard;
try {
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
} catch (Error $e) {
$output5 = null;
$this->assertEquals('php7', $mode);
}
try {
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
} catch (Error $e) {
$output7 = null;
$this->assertEquals('php5', $mode);
}
if ('php5' === $mode) {
$this->assertSame($expected, $output5, $name);
$this->assertNotSame($expected, $output7, $name);
} else if ('php7' === $mode) {
$this->assertSame($expected, $output7, $name);
$this->assertNotSame($expected, $output5, $name);
} else {
$this->assertSame($expected, $output5, $name);
$this->assertSame($expected, $output7, $name);
}
}
/**
* @dataProvider provideTestPrettyPrint
* @covers PhpParser\PrettyPrinter\Standard<extended>
*/
public function testPrettyPrint($name, $code, $expected, $mode) {
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
}
/**
* @dataProvider provideTestPrettyPrintFile
* @covers PhpParser\PrettyPrinter\Standard<extended>
*/
public function testPrettyPrintFile($name, $code, $expected, $mode) {
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
}
public function provideTestPrettyPrint() {
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
}
public function provideTestPrettyPrintFile() {
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
}
public function testPrettyPrintExpr() {
$prettyPrinter = new Standard;
$expr = new Expr\BinaryOp\Mul(
new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
new Expr\Variable('c')
);
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
$expr = new Expr\Closure(array(
'stmts' => array(new Stmt\Return_(new String_("a\nb")))
));
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
}
}