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@0
|
113 require_once dirname(__FILE__) . '/../lib/PhpParser/Autoloader.php';
|
Chris@0
|
114 PhpParser\Autoloader::register();
|
Chris@0
|
115
|
Chris@0
|
116 $parserName = 'PhpParser\Parser\\' . $version;
|
Chris@0
|
117 $parser = new $parserName(new PhpParser\Lexer\Emulative);
|
Chris@0
|
118 $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
Chris@0
|
119 $nodeDumper = new PhpParser\NodeDumper;
|
Chris@0
|
120
|
Chris@0
|
121 $parseFail = $ppFail = $compareFail = $count = 0;
|
Chris@0
|
122
|
Chris@0
|
123 $readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
|
Chris@0
|
124 $totalStartTime = microtime(true);
|
Chris@0
|
125
|
Chris@0
|
126 foreach (new RecursiveIteratorIterator(
|
Chris@0
|
127 new RecursiveDirectoryIterator($dir),
|
Chris@0
|
128 RecursiveIteratorIterator::LEAVES_ONLY)
|
Chris@0
|
129 as $file) {
|
Chris@0
|
130 if (!$fileFilter($file)) {
|
Chris@0
|
131 continue;
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 $startTime = microtime(true);
|
Chris@0
|
135 $code = file_get_contents($file);
|
Chris@0
|
136 $readTime += microtime(true) - $startTime;
|
Chris@0
|
137
|
Chris@0
|
138 if (null === $code = $codeExtractor($file, $code)) {
|
Chris@0
|
139 continue;
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 set_time_limit(10);
|
Chris@0
|
143
|
Chris@0
|
144 ++$count;
|
Chris@0
|
145
|
Chris@0
|
146 if ($showProgress) {
|
Chris@0
|
147 echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 try {
|
Chris@0
|
151 $startTime = microtime(true);
|
Chris@0
|
152 $stmts = $parser->parse($code);
|
Chris@0
|
153 $parseTime += microtime(true) - $startTime;
|
Chris@0
|
154
|
Chris@0
|
155 $startTime = microtime(true);
|
Chris@0
|
156 $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
|
Chris@0
|
157 $ppTime += microtime(true) - $startTime;
|
Chris@0
|
158
|
Chris@0
|
159 try {
|
Chris@0
|
160 $startTime = microtime(true);
|
Chris@0
|
161 $ppStmts = $parser->parse($code);
|
Chris@0
|
162 $reparseTime += microtime(true) - $startTime;
|
Chris@0
|
163
|
Chris@0
|
164 $startTime = microtime(true);
|
Chris@0
|
165 $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
|
Chris@0
|
166 $compareTime += microtime(true) - $startTime;
|
Chris@0
|
167
|
Chris@0
|
168 if (!$same) {
|
Chris@0
|
169 echo $file, ":\n Result of initial parse and parse after pretty print differ\n";
|
Chris@0
|
170 if ($verbose) {
|
Chris@0
|
171 echo "Pretty printer output:\n=====\n$code\n=====\n\n";
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 ++$compareFail;
|
Chris@0
|
175 }
|
Chris@0
|
176 } catch (PhpParser\Error $e) {
|
Chris@0
|
177 echo $file, ":\n Parse of pretty print failed with message: {$e->getMessage()}\n";
|
Chris@0
|
178 if ($verbose) {
|
Chris@0
|
179 echo "Pretty printer output:\n=====\n$code\n=====\n\n";
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 ++$ppFail;
|
Chris@0
|
183 }
|
Chris@0
|
184 } catch (PhpParser\Error $e) {
|
Chris@0
|
185 echo $file, ":\n Parse failed with message: {$e->getMessage()}\n";
|
Chris@0
|
186
|
Chris@0
|
187 ++$parseFail;
|
Chris@0
|
188 }
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
|
Chris@0
|
192 $exit = 0;
|
Chris@0
|
193 echo "\n\n", 'All tests passed.', "\n";
|
Chris@0
|
194 } else {
|
Chris@0
|
195 $exit = 1;
|
Chris@0
|
196 echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
|
Chris@0
|
197 if (0 !== $parseFail) {
|
Chris@0
|
198 echo ' ', $parseFail, ' parse failures.', "\n";
|
Chris@0
|
199 }
|
Chris@0
|
200 if (0 !== $ppFail) {
|
Chris@0
|
201 echo ' ', $ppFail, ' pretty print failures.', "\n";
|
Chris@0
|
202 }
|
Chris@0
|
203 if (0 !== $compareFail) {
|
Chris@0
|
204 echo ' ', $compareFail, ' compare failures.', "\n";
|
Chris@0
|
205 }
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 echo "\n",
|
Chris@0
|
209 'Tested files: ', $count, "\n",
|
Chris@0
|
210 "\n",
|
Chris@0
|
211 'Reading files took: ', $readTime, "\n",
|
Chris@0
|
212 'Parsing took: ', $parseTime, "\n",
|
Chris@0
|
213 'Pretty printing took: ', $ppTime, "\n",
|
Chris@0
|
214 'Reparsing took: ', $reparseTime, "\n",
|
Chris@0
|
215 'Comparing took: ', $compareTime, "\n",
|
Chris@0
|
216 "\n",
|
Chris@0
|
217 'Total time: ', microtime(true) - $totalStartTime, "\n",
|
Chris@0
|
218 'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
|
Chris@0
|
219
|
Chris@0
|
220 exit($exit);
|