run.php 8.0 KB

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