run.php 7.7 KB

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