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