Pull merge.
[yaffs-website] / vendor / nikic / php-parser / test_old / run.php
1 <?php
2
3 error_reporting(E_ALL | E_STRICT);
4 ini_set('short_open_tag', false);
5
6 if ('cli' !== php_sapi_name()) {
7     die('This script is designed for running on the command line.');
8 }
9
10 function showHelp($error) {
11     die($error . "\n\n" .
12 <<<OUTPUT
13 This script has to be called with the following signature:
14
15     php run.php [--no-progress] testType pathToTestFiles
16
17 The test type must be one of: PHP5, PHP7 or Symfony.
18
19 The following options are available:
20
21     --no-progress    Disables showing which file is currently tested.
22
23 OUTPUT
24     );
25 }
26
27 $options = array();
28 $arguments = array();
29
30 // remove script name from argv
31 array_shift($argv);
32
33 foreach ($argv as $arg) {
34     if ('-' === $arg[0]) {
35         $options[] = $arg;
36     } else {
37         $arguments[] = $arg;
38     }
39 }
40
41 if (count($arguments) !== 2) {
42     showHelp('Too little arguments passed!');
43 }
44
45 $showProgress = true;
46 $verbose = false;
47 foreach ($options as $option) {
48     if ($option === '--no-progress') {
49         $showProgress = false;
50     } elseif ($option === '--verbose') {
51         $verbose = true;
52     } else {
53         showHelp('Invalid option passed!');
54     }
55 }
56
57 $testType = $arguments[0];
58 $dir = $arguments[1];
59
60 switch ($testType) {
61     case 'Symfony':
62         $version = 'Php7';
63         $fileFilter = function($path) {
64             if (!preg_match('~\.php$~', $path)) {
65                 return false;
66             }
67
68             if (preg_match('~(?:
69 # invalid php code
70   dependency-injection.Tests.Fixtures.xml.xml_with_wrong_ext
71 # difference in nop statement
72 | framework-bundle.Resources.views.Form.choice_widget_options\.html
73 # difference due to INF
74 | yaml.Tests.InlineTest
75 )\.php$~x', $path)) {
76                 return false;
77             }
78
79             return true;
80         };
81         $codeExtractor = function($file, $code) {
82             return $code;
83         };
84         break;
85     case 'PHP5':
86     case 'PHP7':
87     $version = $testType === 'PHP5' ? 'Php5' : 'Php7';
88         $fileFilter = function($path) {
89             return preg_match('~\.phpt$~', $path);
90         };
91         $codeExtractor = function($file, $code) {
92             if (preg_match('~(?:
93 # skeleton files
94   ext.gmp.tests.001
95 | ext.skeleton.tests.00\d
96 # multibyte encoded files
97 | ext.mbstring.tests.zend_multibyte-01
98 | Zend.tests.multibyte.multibyte_encoding_001
99 | Zend.tests.multibyte.multibyte_encoding_004
100 | Zend.tests.multibyte.multibyte_encoding_005
101 # invalid code due to missing WS after opening tag
102 | tests.run-test.bug75042-3
103 # pretty print difference due to INF vs 1e1000
104 | ext.standard.tests.general_functions.bug27678
105 | tests.lang.bug24640
106 | Zend.tests.bug74947
107 # pretty print differences due to negative LNumbers
108 | Zend.tests.neg_num_string
109 | Zend.tests.bug72918
110 # pretty print difference due to nop statements
111 | ext.mbstring.tests.htmlent
112 | ext.standard.tests.file.fread_basic
113 # its too hard to emulate these on old PHP versions
114 | Zend.tests.flexible-heredoc-complex-test[1-4]
115 )\.phpt$~x', $file)) {
116                 return null;
117             }
118
119             if (!preg_match('~--FILE--\s*(.*?)\n--[A-Z]+--~s', $code, $matches)) {
120                 return null;
121             }
122             if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
123                 return null;
124             }
125
126             return $matches[1];
127         };
128         break;
129     default:
130         showHelp('Test type must be one of: PHP5, PHP7 or Symfony');
131 }
132
133 require_once __DIR__ . '/../vendor/autoload.php';
134
135 $lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
136     'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos',
137 ]]);
138 $parserName = 'PhpParser\Parser\\' . $version;
139 /** @var PhpParser\Parser $parser */
140 $parser = new $parserName($lexer);
141 $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
142 $nodeDumper = new PhpParser\NodeDumper;
143
144 $cloningTraverser = new PhpParser\NodeTraverser;
145 $cloningTraverser->addVisitor(new PhpParser\NodeVisitor\CloningVisitor);
146
147 $parseFail = $fpppFail = $ppFail = $compareFail = $count = 0;
148
149 $readTime = $parseTime = $cloneTime = 0;
150 $fpppTime = $ppTime = $reparseTime = $compareTime = 0;
151 $totalStartTime = microtime(true);
152
153 foreach (new RecursiveIteratorIterator(
154              new RecursiveDirectoryIterator($dir),
155              RecursiveIteratorIterator::LEAVES_ONLY)
156          as $file) {
157     if (!$fileFilter($file)) {
158         continue;
159     }
160
161     $startTime = microtime(true);
162     $origCode = file_get_contents($file);
163     $readTime += microtime(true) - $startTime;
164
165     if (null === $origCode = $codeExtractor($file, $origCode)) {
166         continue;
167     }
168
169     set_time_limit(10);
170
171     ++$count;
172
173     if ($showProgress) {
174         echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
175     }
176
177     try {
178         $startTime = microtime(true);
179         $origStmts = $parser->parse($origCode);
180         $parseTime += microtime(true) - $startTime;
181
182         $origTokens = $lexer->getTokens();
183
184         $startTime = microtime(true);
185         $stmts = $cloningTraverser->traverse($origStmts);
186         $cloneTime += microtime(true) - $startTime;
187
188         $startTime = microtime(true);
189         $code = $prettyPrinter->printFormatPreserving($stmts, $origStmts, $origTokens);
190         $fpppTime += microtime(true) - $startTime;
191
192         if ($code !== $origCode) {
193             echo $file, ":\n Result of format-preserving pretty-print differs\n";
194             if ($verbose) {
195                 echo "FPPP output:\n=====\n$code\n=====\n\n";
196             }
197
198             ++$fpppFail;
199         }
200
201         $startTime = microtime(true);
202         $code = "<?php\n" . $prettyPrinter->prettyPrint($stmts);
203         $ppTime += microtime(true) - $startTime;
204
205         try {
206             $startTime = microtime(true);
207             $ppStmts = $parser->parse($code);
208             $reparseTime += microtime(true) - $startTime;
209
210             $startTime = microtime(true);
211             $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
212             $compareTime += microtime(true) - $startTime;
213
214             if (!$same) {
215                 echo $file, ":\n    Result of initial parse and parse after pretty print differ\n";
216                 if ($verbose) {
217                     echo "Pretty printer output:\n=====\n$code\n=====\n\n";
218                 }
219
220                 ++$compareFail;
221             }
222         } catch (PhpParser\Error $e) {
223             echo $file, ":\n    Parse of pretty print failed with message: {$e->getMessage()}\n";
224             if ($verbose) {
225                 echo "Pretty printer output:\n=====\n$code\n=====\n\n";
226             }
227
228             ++$ppFail;
229         }
230     } catch (PhpParser\Error $e) {
231         echo $file, ":\n    Parse failed with message: {$e->getMessage()}\n";
232
233         ++$parseFail;
234     }
235 }
236
237 if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
238     $exit = 0;
239     echo "\n\n", 'All tests passed.', "\n";
240 } else {
241     $exit = 1;
242     echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
243     if (0 !== $parseFail) {
244         echo '    ', $parseFail,   ' parse failures.',        "\n";
245     }
246     if (0 !== $ppFail) {
247         echo '    ', $ppFail,      ' pretty print failures.', "\n";
248     }
249     if (0 !== $fpppFail) {
250         echo '    ', $fpppFail,      ' FPPP failures.', "\n";
251     }
252     if (0 !== $compareFail) {
253         echo '    ', $compareFail, ' compare failures.',      "\n";
254     }
255 }
256
257 echo "\n",
258      'Tested files:         ', $count,        "\n",
259      "\n",
260      'Reading files took:   ', $readTime,    "\n",
261      'Parsing took:         ', $parseTime,   "\n",
262      'Cloning took:         ', $cloneTime,   "\n",
263      'FPPP took:            ', $fpppTime,    "\n",
264      'Pretty printing took: ', $ppTime,      "\n",
265      'Reparsing took:       ', $reparseTime, "\n",
266      'Comparing took:       ', $compareTime, "\n",
267      "\n",
268      'Total time:           ', microtime(true) - $totalStartTime, "\n",
269      'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
270
271 exit($exit);