Вход Регистрация
Файл: src/vendor/nikic/php-parser/test/PhpParser/PrettyPrinterTest.php
Строк: 144
<?php

namespace PhpParser;

use 
PhpParserComment;
use 
PhpParserNodeExpr;
use 
PhpParserNodeScalarString_;
use 
PhpParserNodeStmt;
use 
PhpParserPrettyPrinterStandard;

require_once 
__DIR__ . '/CodeTestAbstract.php';

class 
PrettyPrinterTest extends CodeTestAbstract
{
    protected function 
doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
        
$lexer = new LexerEmulative;
        
$parser5 = new ParserPhp5($lexer);
        
$parser7 = new ParserPhp7($lexer);

        list(
$version, $options) = $this->parseModeLine($modeLine);
        
$prettyPrinter = new Standard($options);

        try {
            
$output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
        } catch (
Error $e) {
            
$output5 = null;
            if (
'php7' !== $version) {
                throw 
$e;
            }
        }

        try {
            
$output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
        } catch (
Error $e) {
            
$output7 = null;
            if (
'php5' !== $version) {
                throw 
$e;
            }
        }

        if (
'php5' === $version) {
            
$this->assertSame($expected, $output5, $name);
            
$this->assertNotSame($expected, $output7, $name);
        } else if (
'php7' === $version) {
            
$this->assertSame($expected, $output7, $name);
            
$this->assertNotSame($expected, $output5, $name);
        } else {
            
$this->assertSame($expected, $output5, $name);
            
$this->assertSame($expected, $output7, $name);
        }
    }

    
/**
     * @dataProvider provideTestPrettyPrint
     * @covers PhpParserPrettyPrinterStandard<extended>
     */
    
public function testPrettyPrint($name, $code, $expected, $mode) {
        
$this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
    }

    
/**
     * @dataProvider provideTestPrettyPrintFile
     * @covers PhpParserPrettyPrinterStandard<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 ExprBinaryOpMul(
            new 
ExprBinaryOpPlus(new ExprVariable('a'), new ExprVariable('b')),
            new 
ExprVariable('c')
        );
        
$this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));

        
$expr = new ExprClosure(array(
            
'stmts' => array(new StmtReturn_(new String_("anb")))
        ));
        
$this->assertEquals("function () {n    return 'anb';n}", $prettyPrinter->prettyPrintExpr($expr));
    }

    public function 
testCommentBeforeInlineHTML() {
        
$prettyPrinter = new PrettyPrinterStandard;
        
$comment = new CommentDoc("/**n * This is a commentn */");
        
$stmts = [new StmtInlineHTML('Hello World!', ['comments' => [$comment]])];
        
$expected = "<?phpnn/**n * This is a commentn */n?>nHello World!";
        
$this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
    }

    private function 
parseModeLine($modeLine) {
        
$parts = explode(' ', $modeLine, 2);
        
$version = isset($parts[0]) ? $parts[0] : 'both';
        
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
        return [
$version, $options];
    }
}
Онлайн: 2
Реклама