Mercurial > hg > isophonics-drupal-site
diff vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php @ 13:5fb285c0d0e3
Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've
been lucky to get away with this so far, as we don't support self-registration
which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5
was vulnerable to.
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:33:26 +0100 |
parents | 4c8ae668cc8c |
children | c2387f117808 |
line wrap: on
line diff
--- a/vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php Fri Feb 23 15:52:07 2018 +0000 +++ b/vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php Mon Apr 23 09:33:26 2018 +0100 @@ -1,8 +1,7 @@ -<?php +<?php declare(strict_types=1); namespace PhpParser; -use PhpParser\Comment; use PhpParser\Node\Expr; use PhpParser\Node\Name; use PhpParser\Node\Scalar\DNumber; @@ -46,7 +45,7 @@ if ('php5' === $version) { $this->assertSame($expected, $output5, $name); $this->assertNotSame($expected, $output7, $name); - } else if ('php7' === $version) { + } elseif ('php7' === $version) { $this->assertSame($expected, $output7, $name); $this->assertNotSame($expected, $output5, $name); } else { @@ -57,7 +56,7 @@ /** * @dataProvider provideTestPrettyPrint - * @covers PhpParser\PrettyPrinter\Standard<extended> + * @covers \PhpParser\PrettyPrinter\Standard<extended> */ public function testPrettyPrint($name, $code, $expected, $mode) { $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode); @@ -65,7 +64,7 @@ /** * @dataProvider provideTestPrettyPrintFile - * @covers PhpParser\PrettyPrinter\Standard<extended> + * @covers \PhpParser\PrettyPrinter\Standard<extended> */ public function testPrettyPrintFile($name, $code, $expected, $mode) { $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode); @@ -87,9 +86,9 @@ ); $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr)); - $expr = new Expr\Closure(array( - 'stmts' => array(new Stmt\Return_(new String_("a\nb"))) - )); + $expr = new Expr\Closure([ + 'stmts' => [new Stmt\Return_(new String_("a\nb"))] + ]); $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr)); } @@ -102,8 +101,8 @@ } private function parseModeLine($modeLine) { - $parts = explode(' ', $modeLine, 2); - $version = isset($parts[0]) ? $parts[0] : 'both'; + $parts = explode(' ', (string) $modeLine, 2); + $version = $parts[0] ?? 'both'; $options = isset($parts[1]) ? json_decode($parts[1], true) : []; return [$version, $options]; } @@ -190,7 +189,9 @@ * @expectedExceptionMessage Cannot pretty-print AST with Error nodes */ public function testPrettyPrintWithError() { - $stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())]; + $stmts = [new Stmt\Expression( + new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error()) + )]; $prettyPrinter = new PrettyPrinter\Standard; $prettyPrinter->prettyPrint($stmts); } @@ -200,8 +201,105 @@ * @expectedExceptionMessage Cannot pretty-print AST with Error nodes */ public function testPrettyPrintWithErrorInClassConstFetch() { - $stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())]; + $stmts = [new Stmt\Expression( + new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error()) + )]; $prettyPrinter = new PrettyPrinter\Standard; $prettyPrinter->prettyPrint($stmts); } + + /** + * @dataProvider provideTestFormatPreservingPrint + * @covers \PhpParser\PrettyPrinter\Standard<extended> + */ + public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) { + $lexer = new Lexer\Emulative([ + 'usedAttributes' => [ + 'comments', + 'startLine', 'endLine', + 'startTokenPos', 'endTokenPos', + ], + ]); + + $parser = new Parser\Php7($lexer); + $traverser = new NodeTraverser(); + $traverser->addVisitor(new NodeVisitor\CloningVisitor()); + + $printer = new PrettyPrinter\Standard(); + + $oldStmts = $parser->parse($code); + $oldTokens = $lexer->getTokens(); + + $newStmts = $traverser->traverse($oldStmts); + + /** @var callable $fn */ + eval(<<<CODE +use PhpParser\Comment; +use PhpParser\Node; +use PhpParser\Node\Expr; +use PhpParser\Node\Scalar; +use PhpParser\Node\Stmt; +\$fn = function(&\$stmts) { $modification }; +CODE + ); + $fn($newStmts); + + $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); + $this->assertSame(canonicalize($expected), canonicalize($newCode), $name); + } + + public function provideTestFormatPreservingPrint() { + return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3); + } + + /** + * @dataProvider provideTestRoundTripPrint + * @covers \PhpParser\PrettyPrinter\Standard<extended> + */ + public function testRoundTripPrint($name, $code, $expected, $modeLine) { + /** + * This test makes sure that the format-preserving pretty printer round-trips for all + * the pretty printer tests (i.e. returns the input if no changes occurred). + */ + + list($version) = $this->parseModeLine($modeLine); + + $lexer = new Lexer\Emulative([ + 'usedAttributes' => [ + 'comments', + 'startLine', 'endLine', + 'startTokenPos', 'endTokenPos', + ], + ]); + + $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class; + /** @var Parser $parser */ + $parser = new $parserClass($lexer); + + $traverser = new NodeTraverser(); + $traverser->addVisitor(new NodeVisitor\CloningVisitor()); + + $printer = new PrettyPrinter\Standard(); + + try { + $oldStmts = $parser->parse($code); + } catch (Error $e) { + // Can't do a format-preserving print on a file with errors + return; + } + + $oldTokens = $lexer->getTokens(); + + $newStmts = $traverser->traverse($oldStmts); + + $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); + $this->assertSame(canonicalize($code), canonicalize($newCode), $name); + } + + public function provideTestRoundTripPrint() { + return array_merge( + $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'), + $this->getTests(__DIR__ . '/../code/parser', 'test') + ); + } }