Blame view

vendor/nikic/php-parser/test/PhpParser/CodeParsingTest.php 2.19 KB
d06254b2   Etienne Pallier   bugfix plugin mig...
1
<?php
c4650843   Etienne Pallier   Ajout du dossier ...
2
3
4

namespace PhpParser;

c4650843   Etienne Pallier   Ajout du dossier ...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 = [];
        }

d06254b2   Etienne Pallier   bugfix plugin mig...
19
20
21
22
23
24
25
26
27
28

        $lexer = new Lexer\Emulative(array('usedAttributes' => array(
            'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
        )));
        $parser5 = new Parser\Php5($lexer);
        $parser7 = new Parser\Php7($lexer);

        $dumpPositions = isset($modes['positions']);
        $output5 = $this->getParseOutput($parser5, $code, $dumpPositions);
        $output7 = $this->getParseOutput($parser7, $code, $dumpPositions);
c4650843   Etienne Pallier   Ajout du dossier ...
29
30
31
32

        if (isset($modes['php5'])) {
            $this->assertSame($expected, $output5, $name);
            $this->assertNotSame($expected, $output7, $name);
d06254b2   Etienne Pallier   bugfix plugin mig...
33
        } else if (isset($modes['php7'])) {
c4650843   Etienne Pallier   Ajout du dossier ...
34
35
36
37
38
39
            $this->assertNotSame($expected, $output5, $name);
            $this->assertSame($expected, $output7, $name);
        } else {
            $this->assertSame($expected, $output5, $name);
            $this->assertSame($expected, $output7, $name);
        }
c4650843   Etienne Pallier   Ajout du dossier ...
40
41
    }

d06254b2   Etienne Pallier   bugfix plugin mig...
42
    private function getParseOutput(Parser $parser, $code, $dumpPositions) {
c4650843   Etienne Pallier   Ajout du dossier ...
43
44
45
46
47
48
49
50
51
52
53
54
55
        $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);
        }

d06254b2   Etienne Pallier   bugfix plugin mig...
56
        return canonicalize($output);
c4650843   Etienne Pallier   Ajout du dossier ...
57
58
59
60
61
62
63
64
65
66
67
68
69
    }

    public function provideTestParse() {
        return $this->getTests(__DIR__ . '/../code/parser', 'test');
    }

    private function formatErrorMessage(Error $e, $code) {
        if ($e->hasColumnInfo()) {
            return $e->getMessageWithColumnInfo($code);
        } else {
            return $e->getMessage();
        }
    }
c4650843   Etienne Pallier   Ajout du dossier ...
70
}