2011-05-29 17:33:03 +02:00
|
|
|
<?php
|
|
|
|
|
2011-10-20 22:36:10 +02:00
|
|
|
error_reporting(E_ALL | E_STRICT);
|
|
|
|
ini_set('short_open_tag', false);
|
|
|
|
|
2011-10-19 17:53:40 +02:00
|
|
|
if ('cli' !== php_sapi_name()) {
|
|
|
|
die('This script is designed for running on the command line.');
|
|
|
|
}
|
|
|
|
|
2012-05-05 12:22:23 +02:00
|
|
|
function showHelp($error) {
|
|
|
|
die($error . "\n\n" .
|
|
|
|
<<<OUTPUT
|
|
|
|
This script has to be called with the following signature:
|
|
|
|
|
|
|
|
php run.php [--no-progress] testType pathToTestFiles
|
|
|
|
|
2015-07-09 12:27:15 +02:00
|
|
|
The test type must be one of: PHP5, PHP7 or Symfony.
|
2012-05-05 12:22:23 +02:00
|
|
|
|
|
|
|
The following options are available:
|
|
|
|
|
|
|
|
--no-progress Disables showing which file is currently tested.
|
2015-07-09 12:27:15 +02:00
|
|
|
|
2012-05-05 12:22:23 +02:00
|
|
|
OUTPUT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array();
|
|
|
|
$arguments = array();
|
|
|
|
|
|
|
|
// remove script name from argv
|
|
|
|
array_shift($argv);
|
|
|
|
|
|
|
|
foreach ($argv as $arg) {
|
|
|
|
if ('-' === $arg[0]) {
|
|
|
|
$options[] = $arg;
|
|
|
|
} else {
|
|
|
|
$arguments[] = $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($arguments) !== 2) {
|
|
|
|
showHelp('Too little arguments passed!');
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$showProgress = true;
|
2016-04-05 12:07:28 +02:00
|
|
|
$verbose = false;
|
|
|
|
foreach ($options as $option) {
|
|
|
|
if ($option === '--no-progress') {
|
2015-06-13 19:09:24 +02:00
|
|
|
$showProgress = false;
|
2016-04-05 12:07:28 +02:00
|
|
|
} elseif ($option === '--verbose') {
|
|
|
|
$verbose = true;
|
2012-05-05 12:22:23 +02:00
|
|
|
} else {
|
|
|
|
showHelp('Invalid option passed!');
|
|
|
|
}
|
2011-10-19 17:53:40 +02:00
|
|
|
}
|
2011-10-17 17:49:24 +02:00
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
$testType = $arguments[0];
|
|
|
|
$dir = $arguments[1];
|
|
|
|
|
|
|
|
switch ($testType) {
|
|
|
|
case 'Symfony':
|
2018-09-22 10:41:58 +02:00
|
|
|
$version = 'Php7';
|
2015-06-13 19:09:24 +02:00
|
|
|
$fileFilter = function($path) {
|
2018-09-22 10:41:58 +02:00
|
|
|
if (!preg_match('~\.php$~', $path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('~(?:
|
|
|
|
# invalid php code
|
|
|
|
dependency-injection.Tests.Fixtures.xml.xml_with_wrong_ext
|
|
|
|
# difference in nop statement
|
|
|
|
| framework-bundle.Resources.views.Form.choice_widget_options\.html
|
|
|
|
# difference due to INF
|
|
|
|
| yaml.Tests.InlineTest
|
|
|
|
)\.php$~x', $path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-13 19:09:24 +02:00
|
|
|
};
|
|
|
|
$codeExtractor = function($file, $code) {
|
|
|
|
return $code;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'PHP5':
|
|
|
|
case 'PHP7':
|
|
|
|
$version = $testType === 'PHP5' ? 'Php5' : 'Php7';
|
|
|
|
$fileFilter = function($path) {
|
|
|
|
return preg_match('~\.phpt$~', $path);
|
|
|
|
};
|
|
|
|
$codeExtractor = function($file, $code) {
|
|
|
|
if (preg_match('~(?:
|
|
|
|
# skeleton files
|
|
|
|
ext.gmp.tests.001
|
2018-09-21 11:40:36 +02:00
|
|
|
| ext.skeleton.tests.00\d
|
2015-06-13 19:09:24 +02:00
|
|
|
# multibyte encoded files
|
|
|
|
| ext.mbstring.tests.zend_multibyte-01
|
|
|
|
| Zend.tests.multibyte.multibyte_encoding_001
|
|
|
|
| Zend.tests.multibyte.multibyte_encoding_004
|
|
|
|
| Zend.tests.multibyte.multibyte_encoding_005
|
2018-09-21 12:40:40 +02:00
|
|
|
# invalid code due to missing WS after opening tag
|
|
|
|
| tests.run-test.bug75042-3
|
2015-06-13 19:09:24 +02:00
|
|
|
# pretty print difference due to INF vs 1e1000
|
|
|
|
| ext.standard.tests.general_functions.bug27678
|
|
|
|
| tests.lang.bug24640
|
2018-09-21 11:40:36 +02:00
|
|
|
| Zend.tests.bug74947
|
2016-12-11 16:47:47 +01:00
|
|
|
# pretty print differences due to negative LNumbers
|
|
|
|
| Zend.tests.neg_num_string
|
|
|
|
| Zend.tests.bug72918
|
2016-04-07 04:58:36 +02:00
|
|
|
# pretty print difference due to nop statements
|
|
|
|
| ext.mbstring.tests.htmlent
|
|
|
|
| ext.standard.tests.file.fread_basic
|
2018-09-21 12:35:11 +02:00
|
|
|
# its too hard to emulate these on old PHP versions
|
|
|
|
| Zend.tests.flexible-heredoc-complex-test[1-4]
|
2015-06-13 19:09:24 +02:00
|
|
|
)\.phpt$~x', $file)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-21 11:40:36 +02:00
|
|
|
if (!preg_match('~--FILE--\s*(.*?)\n--[A-Z]+--~s', $code, $matches)) {
|
2015-06-13 19:09:24 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (preg_match('~--EXPECT(?:F|REGEX)?--\s*(?:Parse|Fatal) error~', $code)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-09 12:27:15 +02:00
|
|
|
showHelp('Test type must be one of: PHP5, PHP7 or Symfony');
|
2011-10-19 17:53:40 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 23:13:53 +01:00
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2017-10-29 14:19:01 +01:00
|
|
|
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
|
|
|
|
'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos',
|
|
|
|
]]);
|
|
|
|
$parserName = 'PhpParser\Parser\\' . $version;
|
|
|
|
/** @var PhpParser\Parser $parser */
|
|
|
|
$parser = new $parserName($lexer);
|
2014-02-06 14:44:16 +01:00
|
|
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
2017-10-29 14:19:01 +01:00
|
|
|
$nodeDumper = new PhpParser\NodeDumper;
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2017-10-29 14:19:01 +01:00
|
|
|
$cloningTraverser = new PhpParser\NodeTraverser;
|
|
|
|
$cloningTraverser->addVisitor(new PhpParser\NodeVisitor\CloningVisitor);
|
2011-06-01 20:00:52 +02:00
|
|
|
|
2017-10-29 14:19:01 +01:00
|
|
|
$parseFail = $fpppFail = $ppFail = $compareFail = $count = 0;
|
|
|
|
|
|
|
|
$readTime = $parseTime = $cloneTime = 0;
|
|
|
|
$fpppTime = $ppTime = $reparseTime = $compareTime = 0;
|
2011-06-03 17:44:23 +02:00
|
|
|
$totalStartTime = microtime(true);
|
|
|
|
|
2011-05-29 17:33:03 +02:00
|
|
|
foreach (new RecursiveIteratorIterator(
|
2015-06-13 19:09:24 +02:00
|
|
|
new RecursiveDirectoryIterator($dir),
|
2011-05-29 17:33:03 +02:00
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY)
|
|
|
|
as $file) {
|
2015-06-13 19:09:24 +02:00
|
|
|
if (!$fileFilter($file)) {
|
2011-05-29 17:33:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-30 12:16:31 +01:00
|
|
|
$startTime = microtime(true);
|
2017-10-29 14:19:01 +01:00
|
|
|
$origCode = file_get_contents($file);
|
2011-10-30 12:16:31 +01:00
|
|
|
$readTime += microtime(true) - $startTime;
|
2011-10-19 17:53:40 +02:00
|
|
|
|
2017-10-29 14:19:01 +01:00
|
|
|
if (null === $origCode = $codeExtractor($file, $origCode)) {
|
2015-06-13 19:09:24 +02:00
|
|
|
continue;
|
2011-10-19 17:53:40 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
set_time_limit(10);
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2011-09-05 21:48:00 +02:00
|
|
|
++$count;
|
|
|
|
|
2015-06-13 19:09:24 +02:00
|
|
|
if ($showProgress) {
|
|
|
|
echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
|
2012-05-05 12:22:23 +02:00
|
|
|
}
|
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
try {
|
|
|
|
$startTime = microtime(true);
|
2017-10-29 14:19:01 +01:00
|
|
|
$origStmts = $parser->parse($origCode);
|
2011-06-03 17:44:23 +02:00
|
|
|
$parseTime += microtime(true) - $startTime;
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2017-10-29 14:19:01 +01:00
|
|
|
$origTokens = $lexer->getTokens();
|
|
|
|
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$stmts = $cloningTraverser->traverse($origStmts);
|
|
|
|
$cloneTime += microtime(true) - $startTime;
|
|
|
|
|
|
|
|
$startTime = microtime(true);
|
|
|
|
$code = $prettyPrinter->printFormatPreserving($stmts, $origStmts, $origTokens);
|
|
|
|
$fpppTime += microtime(true) - $startTime;
|
|
|
|
|
|
|
|
if ($code !== $origCode) {
|
|
|
|
echo $file, ":\n Result of format-preserving pretty-print differs\n";
|
|
|
|
if ($verbose) {
|
|
|
|
echo "FPPP output:\n=====\n$code\n=====\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
++$fpppFail;
|
|
|
|
}
|
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
$startTime = microtime(true);
|
2017-10-29 14:19:01 +01:00
|
|
|
$code = "<?php\n" . $prettyPrinter->prettyPrint($stmts);
|
2011-06-03 17:44:23 +02:00
|
|
|
$ppTime += microtime(true) - $startTime;
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
try {
|
2011-10-30 12:16:31 +01:00
|
|
|
$startTime = microtime(true);
|
2012-04-29 23:04:06 +02:00
|
|
|
$ppStmts = $parser->parse($code);
|
2011-10-30 12:16:31 +01:00
|
|
|
$reparseTime += microtime(true) - $startTime;
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2011-06-03 17:44:23 +02:00
|
|
|
$startTime = microtime(true);
|
2011-06-01 20:00:52 +02:00
|
|
|
$same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
|
2011-06-03 17:44:23 +02:00
|
|
|
$compareTime += microtime(true) - $startTime;
|
2011-06-01 20:00:52 +02:00
|
|
|
|
2011-09-05 21:48:00 +02:00
|
|
|
if (!$same) {
|
|
|
|
echo $file, ":\n Result of initial parse and parse after pretty print differ\n";
|
2016-04-05 12:07:28 +02:00
|
|
|
if ($verbose) {
|
|
|
|
echo "Pretty printer output:\n=====\n$code\n=====\n\n";
|
|
|
|
}
|
2011-05-29 20:38:36 +02:00
|
|
|
|
|
|
|
++$compareFail;
|
2011-05-29 17:33:03 +02:00
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} catch (PhpParser\Error $e) {
|
2011-09-05 21:48:00 +02:00
|
|
|
echo $file, ":\n Parse of pretty print failed with message: {$e->getMessage()}\n";
|
2016-04-05 12:07:28 +02:00
|
|
|
if ($verbose) {
|
|
|
|
echo "Pretty printer output:\n=====\n$code\n=====\n\n";
|
|
|
|
}
|
2011-05-29 20:38:36 +02:00
|
|
|
|
|
|
|
++$ppFail;
|
2011-05-29 17:33:03 +02:00
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
} catch (PhpParser\Error $e) {
|
2011-09-05 21:48:00 +02:00
|
|
|
echo $file, ":\n Parse failed with message: {$e->getMessage()}\n";
|
2011-05-29 20:38:36 +02:00
|
|
|
|
|
|
|
++$parseFail;
|
2019-06-30 12:13:28 +02:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
echo $file, ":\n Unknown error occurred: $e\n";
|
2011-05-29 17:33:03 +02:00
|
|
|
}
|
2011-09-05 21:48:00 +02:00
|
|
|
}
|
2011-05-29 17:33:03 +02:00
|
|
|
|
2011-09-05 21:48:00 +02:00
|
|
|
if (0 === $parseFail && 0 === $ppFail && 0 === $compareFail) {
|
2016-04-07 06:37:47 +02:00
|
|
|
$exit = 0;
|
2012-05-05 12:22:23 +02:00
|
|
|
echo "\n\n", 'All tests passed.', "\n";
|
2011-09-05 21:48:00 +02:00
|
|
|
} else {
|
2016-04-07 06:37:47 +02:00
|
|
|
$exit = 1;
|
2012-05-05 12:22:23 +02:00
|
|
|
echo "\n\n", '==========', "\n\n", 'There were: ', "\n";
|
2011-09-05 21:48:00 +02:00
|
|
|
if (0 !== $parseFail) {
|
|
|
|
echo ' ', $parseFail, ' parse failures.', "\n";
|
|
|
|
}
|
|
|
|
if (0 !== $ppFail) {
|
|
|
|
echo ' ', $ppFail, ' pretty print failures.', "\n";
|
|
|
|
}
|
2017-10-29 14:19:01 +01:00
|
|
|
if (0 !== $fpppFail) {
|
|
|
|
echo ' ', $fpppFail, ' FPPP failures.', "\n";
|
|
|
|
}
|
2011-09-05 21:48:00 +02:00
|
|
|
if (0 !== $compareFail) {
|
|
|
|
echo ' ', $compareFail, ' compare failures.', "\n";
|
|
|
|
}
|
2011-05-29 17:33:03 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 21:48:00 +02:00
|
|
|
echo "\n",
|
|
|
|
'Tested files: ', $count, "\n",
|
|
|
|
"\n",
|
2011-10-30 12:16:31 +01:00
|
|
|
'Reading files took: ', $readTime, "\n",
|
2011-09-05 21:48:00 +02:00
|
|
|
'Parsing took: ', $parseTime, "\n",
|
2017-10-29 14:19:01 +01:00
|
|
|
'Cloning took: ', $cloneTime, "\n",
|
|
|
|
'FPPP took: ', $fpppTime, "\n",
|
2011-09-05 21:48:00 +02:00
|
|
|
'Pretty printing took: ', $ppTime, "\n",
|
2011-10-30 12:16:31 +01:00
|
|
|
'Reparsing took: ', $reparseTime, "\n",
|
2011-09-05 21:48:00 +02:00
|
|
|
'Comparing took: ', $compareTime, "\n",
|
|
|
|
"\n",
|
|
|
|
'Total time: ', microtime(true) - $totalStartTime, "\n",
|
2015-06-13 19:09:24 +02:00
|
|
|
'Maximum memory usage: ', memory_get_peak_usage(true), "\n";
|
2016-04-07 06:37:47 +02:00
|
|
|
|
|
|
|
exit($exit);
|