Blame view

vendor/nikic/php-parser/test_old/run.php 6.14 KB
c4650843   Etienne Pallier   Ajout du dossier ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

error_reporting(E_ALL | E_STRICT);
ini_set('short_open_tag', false);

if ('cli' !== php_sapi_name()) {
    die('This script is designed for running on the command line.');
}

function showHelp($error) {
    die($error . "\n\n" .
<<<OUTPUT
This script has to be called with the following signature:

    php run.php [--no-progress] testType pathToTestFiles

The test type must be one of: PHP5, PHP7 or Symfony.

The following options are available:

    --no-progress    Disables showing which file is currently tested.

OUTPUT
    );
}

$options = array();
$arguments = array();

// remove script name from argv
array_shift($argv);

foreach ($argv as $arg) {
    if ('-' === $arg[0]) {
        $options[] = $arg;
    } else {
        $arguments[] = $arg;
    }
}

if (count($arguments) !== 2) {
    showHelp('Too little arguments passed!');
}

$showProgress = true;
$verbose = false;
foreach ($options as $option) {
    if ($option === '--no-progress') {
        $showProgress = false;
    } elseif ($option === '--verbose') {
        $verbose = true;
    } else {
        showHelp('Invalid option passed!');
    }
}

$testType = $arguments[0];
$dir = $arguments[1];

switch ($testType) {
    case 'Symfony':
d06254b2   Etienne Pallier   bugfix plugin mig...
62
        $version = 'Php5';
c4650843   Etienne Pallier   Ajout du dossier ...
63
        $fileFilter = function($path) {
d06254b2   Etienne Pallier   bugfix plugin mig...
64
            return preg_match('~\.php(?:\.cache)?$~', $path) && false === strpos($path, 'skeleton');
c4650843   Etienne Pallier   Ajout du dossier ...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        };
        $codeExtractor = function($file, $code) {
            return $code;
        };
        break;
    case 'PHP5':
    case 'PHP7':
    $version = $testType === 'PHP5' ? 'Php5' : 'Php7';
        $fileFilter = function($path) {
            return preg_match('~\.phpt$~', $path);
        };
        $codeExtractor = function($file, $code) {
            if (preg_match('~(?:
# skeleton files
  ext.gmp.tests.001
d06254b2   Etienne Pallier   bugfix plugin mig...
80
| ext.skeleton.tests.001
c4650843   Etienne Pallier   Ajout du dossier ...
81
82
83
84
85
# multibyte encoded files
| ext.mbstring.tests.zend_multibyte-01
| Zend.tests.multibyte.multibyte_encoding_001
| Zend.tests.multibyte.multibyte_encoding_004
| Zend.tests.multibyte.multibyte_encoding_005
c4650843   Etienne Pallier   Ajout du dossier ...
86
87
88
# pretty print difference due to INF vs 1e1000
| ext.standard.tests.general_functions.bug27678
| tests.lang.bug24640
c4650843   Etienne Pallier   Ajout du dossier ...
89
90
91
92
93
94
# pretty print differences due to negative LNumbers
| Zend.tests.neg_num_string
| Zend.tests.bug72918
# pretty print difference due to nop statements
| ext.mbstring.tests.htmlent
| ext.standard.tests.file.fread_basic
c4650843   Etienne Pallier   Ajout du dossier ...
95
96
97
98
)\.phpt$~x', $file)) {
                return null;
            }

d06254b2   Etienne Pallier   bugfix plugin mig...
99
            if (!preg_match('~--FILE--\s*(.*?)--[A-Z]+--~s', $code, $matches)) {
c4650843   Etienne Pallier   Ajout du dossier ...
100
101
102
103
104
105
106
107
108
109
110
111
112
                return null;
            }
            if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
                return null;
            }

            return $matches[1];
        };
        break;
    default:
        showHelp('Test type must be one of: PHP5, PHP7 or Symfony');
}

d06254b2   Etienne Pallier   bugfix plugin mig...
113
114
require_once dirname(__FILE__) . '/../lib/PhpParser/Autoloader.php';
PhpParser\Autoloader::register();
c4650843   Etienne Pallier   Ajout du dossier ...
115

d06254b2   Etienne Pallier   bugfix plugin mig...
116
117
$parserName    = 'PhpParser\Parser\\' . $version;
$parser        = new $parserName(new PhpParser\Lexer\Emulative);
c4650843   Etienne Pallier   Ajout du dossier ...
118
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
d06254b2   Etienne Pallier   bugfix plugin mig...
119
$nodeDumper    = new PhpParser\NodeDumper;
c4650843   Etienne Pallier   Ajout du dossier ...
120

d06254b2   Etienne Pallier   bugfix plugin mig...
121
$parseFail = $ppFail = $compareFail = $count = 0;
c4650843   Etienne Pallier   Ajout du dossier ...
122

d06254b2   Etienne Pallier   bugfix plugin mig...
123
$readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
c4650843   Etienne Pallier   Ajout du dossier ...
124
125
126
127
128
129
130
131
132
133
134
$totalStartTime = microtime(true);

foreach (new RecursiveIteratorIterator(
             new RecursiveDirectoryIterator($dir),
             RecursiveIteratorIterator::LEAVES_ONLY)
         as $file) {
    if (!$fileFilter($file)) {
        continue;
    }

    $startTime = microtime(true);
d06254b2   Etienne Pallier   bugfix plugin mig...
135
    $code = file_get_contents($file);
c4650843   Etienne Pallier   Ajout du dossier ...
136
137
    $readTime += microtime(true) - $startTime;

d06254b2   Etienne Pallier   bugfix plugin mig...
138
    if (null === $code = $codeExtractor($file, $code)) {
c4650843   Etienne Pallier   Ajout du dossier ...
139
140
141
142
143
144
145
146
147
148
149
150
151
        continue;
    }

    set_time_limit(10);

    ++$count;

    if ($showProgress) {
        echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
    }

    try {
        $startTime = microtime(true);
d06254b2   Etienne Pallier   bugfix plugin mig...
152
        $stmts = $parser->parse($code);
c4650843   Etienne Pallier   Ajout du dossier ...
153
154
        $parseTime += microtime(true) - $startTime;

c4650843   Etienne Pallier   Ajout du dossier ...
155
        $startTime = microtime(true);
d06254b2   Etienne Pallier   bugfix plugin mig...
156
        $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
c4650843   Etienne Pallier   Ajout du dossier ...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        $ppTime += microtime(true) - $startTime;

        try {
            $startTime = microtime(true);
            $ppStmts = $parser->parse($code);
            $reparseTime += microtime(true) - $startTime;

            $startTime = microtime(true);
            $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
            $compareTime += microtime(true) - $startTime;

            if (!$same) {
                echo $file, ":\n    Result of initial parse and parse after pretty print differ\n";
                if ($verbose) {
                    echo "Pretty printer output:\n=====\n$code\n=====\n\n";
                }

                ++$compareFail;
            }
        } catch (PhpParser\Error $e) {
            echo $file, ":\n    Parse of pretty print failed with message: {$e->getMessage()}\n";
            if ($verbose) {
                echo "Pretty printer output:\n=====\n$code\n=====\n\n";
            }

            ++$ppFail;
        }
    } catch (PhpParser\Error $e) {
        echo $file, ":\n    Parse failed with message: {$e->getMessage()}\n";

        ++$parseFail;
    }
}

if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
    $exit = 0;
    echo "\n\n", 'All tests passed.', "\n";
} else {
    $exit = 1;
    echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
    if (0 !== $parseFail) {
        echo '    ', $parseFail,   ' parse failures.',        "\n";
    }
    if (0 !== $ppFail) {
        echo '    ', $ppFail,      ' pretty print failures.', "\n";
    }
c4650843   Etienne Pallier   Ajout du dossier ...
203
204
205
206
207
208
209
210
211
212
    if (0 !== $compareFail) {
        echo '    ', $compareFail, ' compare failures.',      "\n";
    }
}

echo "\n",
     'Tested files:         ', $count,        "\n",
     "\n",
     'Reading files took:   ', $readTime,    "\n",
     'Parsing took:         ', $parseTime,   "\n",
c4650843   Etienne Pallier   Ajout du dossier ...
213
214
215
216
217
218
219
220
     'Pretty printing took: ', $ppTime,      "\n",
     'Reparsing took:       ', $reparseTime, "\n",
     'Comparing took:       ', $compareTime, "\n",
     "\n",
     'Total time:           ', microtime(true) - $totalStartTime, "\n",
     'Maximum memory usage: ', memory_get_peak_usage(true), "\n";

exit($exit);