2012-05-11 16:18:14 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2015-01-18 19:57:09 +01:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Scalar\String;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\PrettyPrinter\Standard;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
require_once __DIR__ . '/CodeTestAbstract.php';
|
|
|
|
|
|
|
|
class PrettyPrinterTest extends CodeTestAbstract
|
2012-05-11 16:18:14 +02:00
|
|
|
{
|
2013-04-15 20:53:23 +02:00
|
|
|
protected function doTestPrettyPrintMethod($method, $name, $code, $dump) {
|
2014-02-06 14:44:16 +01:00
|
|
|
$parser = new Parser(new Lexer\Emulative);
|
2015-01-18 19:57:09 +01:00
|
|
|
$prettyPrinter = new Standard;
|
2012-05-11 16:18:14 +02:00
|
|
|
|
|
|
|
$stmts = $parser->parse($code);
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame(
|
2012-05-11 16:18:14 +02:00
|
|
|
$this->canonicalize($dump),
|
2013-04-15 20:53:23 +02:00
|
|
|
$this->canonicalize($prettyPrinter->$method($stmts)),
|
2012-05-11 16:18:14 +02:00
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:53:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrint
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
|
|
|
public function testPrettyPrint($name, $code, $dump) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $dump);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestPrettyPrintFile
|
2014-09-28 12:48:55 +02:00
|
|
|
* @covers PhpParser\PrettyPrinter\Standard<extended>
|
2013-04-15 20:53:23 +02:00
|
|
|
*/
|
|
|
|
public function testPrettyPrintFile($name, $code, $dump) {
|
|
|
|
$this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $dump);
|
|
|
|
}
|
|
|
|
|
2012-05-11 16:18:14 +02:00
|
|
|
public function provideTestPrettyPrint() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
2013-04-15 20:53:23 +02:00
|
|
|
|
|
|
|
public function provideTestPrettyPrintFile() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
2015-01-18 19:57:09 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|