/vendor/nikic/php-parser/test_old/run.php

https://gitlab.com/matijabelec/bigpandadev · PHP · 148 lines · 122 code · 26 blank · 0 comment · 19 complexity · 999caf378181ef03600a0d3200f4b1a1 MD5 · raw file

  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. if (3 !== $argc) {
  8. die('This script expects exactly two arguments:
  9. 1. The test type (either "Symfony" or "PHP")
  10. 2. The path to the test files');
  11. }
  12. $TEST_TYPE = $argv[1];
  13. $DIR = $argv[2];
  14. if ('Symfony' === $TEST_TYPE) {
  15. function filter_func($path) {
  16. return preg_match('~\.php(?:\.cache)?$~', $path) && false === strpos($path, 'skeleton');
  17. };
  18. } elseif ('PHP' === $TEST_TYPE) {
  19. function filter_func($path) {
  20. return preg_match('~\.phpt$~', $path);
  21. };
  22. } else {
  23. die('The test type must be either "Symfony" or "PHP".');
  24. }
  25. require_once dirname(__FILE__) . '/../lib/PHPParser/Autoloader.php';
  26. PHPParser_Autoloader::register();
  27. $parser = new PHPParser_Parser;
  28. $prettyPrinter = new PHPParser_PrettyPrinter_Zend;
  29. $nodeDumper = new PHPParser_NodeDumper;
  30. $parseFail = $ppFail = $compareFail = $count = 0;;
  31. $readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
  32. $totalStartTime = microtime(true);
  33. foreach (new RecursiveIteratorIterator(
  34. new RecursiveDirectoryIterator($DIR),
  35. RecursiveIteratorIterator::LEAVES_ONLY)
  36. as $file) {
  37. if (!filter_func($file)) {
  38. continue;
  39. }
  40. $startTime = microtime(true);
  41. $code = file_get_contents($file);
  42. $readTime += microtime(true) - $startTime;
  43. if ('PHP' === $TEST_TYPE) {
  44. if (preg_match('~(?:
  45. # skeleton files
  46. ext.gmp.tests.001
  47. | ext.skeleton.tests.001
  48. # multibyte encoded files
  49. | ext.mbstring.tests.zend_multibyte-01
  50. | Zend.tests.multibyte.multibyte_encoding_001
  51. | Zend.tests.multibyte.multibyte_encoding_004
  52. | Zend.tests.multibyte.multibyte_encoding_005
  53. # token_get_all bug (https://bugs.php.net/bug.php?id=60097)
  54. | Zend.tests.bug47516
  55. # pretty print difference due to INF vs 1e1000
  56. | ext.standard.tests.general_functions.bug27678
  57. | tests.lang.bug24640
  58. )\.phpt$~x', $file)) {
  59. continue;
  60. }
  61. if (!preg_match('~--FILE--\s*(.*?)--[A-Z]+--~s', $code, $matches)) {
  62. continue;
  63. }
  64. if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
  65. continue;
  66. }
  67. $code = $matches[1];
  68. }
  69. set_time_limit(10);
  70. ++$count;
  71. try {
  72. $startTime = microtime(true);
  73. $stmts = $parser->parse(new PHPParser_Lexer_Emulative($code));
  74. $parseTime += microtime(true) - $startTime;
  75. $startTime = microtime(true);
  76. $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
  77. $ppTime += microtime(true) - $startTime;
  78. try {
  79. $startTime = microtime(true);
  80. $ppStmts = $parser->parse(new PHPParser_Lexer_Emulative($code));
  81. $reparseTime += microtime(true) - $startTime;
  82. $startTime = microtime(true);
  83. $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
  84. $compareTime += microtime(true) - $startTime;
  85. if (!$same) {
  86. echo $file, ":\n Result of initial parse and parse after pretty print differ\n";
  87. ++$compareFail;
  88. }
  89. } catch (PHPParser_Error $e) {
  90. echo $file, ":\n Parse of pretty print failed with message: {$e->getMessage()}\n";
  91. ++$ppFail;
  92. }
  93. } catch (PHPParser_Error $e) {
  94. echo $file, ":\n Parse failed with message: {$e->getMessage()}\n";
  95. ++$parseFail;
  96. }
  97. }
  98. if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
  99. echo 'All tests passed.', "\n";
  100. } else {
  101. echo "\n", '==========', "\n\n", 'There were: ', "\n";
  102. if (0 !== $parseFail) {
  103. echo ' ', $parseFail, ' parse failures.', "\n";
  104. }
  105. if (0 !== $ppFail) {
  106. echo ' ', $ppFail, ' pretty print failures.', "\n";
  107. }
  108. if (0 !== $compareFail) {
  109. echo ' ', $compareFail, ' compare failures.', "\n";
  110. }
  111. }
  112. echo "\n",
  113. 'Tested files: ', $count, "\n",
  114. "\n",
  115. 'Reading files took: ', $readTime, "\n",
  116. 'Parsing took: ', $parseTime, "\n",
  117. 'Pretty printing took: ', $ppTime, "\n",
  118. 'Reparsing took: ', $reparseTime, "\n",
  119. 'Comparing took: ', $compareTime, "\n",
  120. "\n",
  121. 'Total time: ', microtime(true) - $totalStartTime, "\n",
  122. 'Maximum memory usage: ', memory_get_peak_usage(true), "\n";