mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-30 04:19:30 +01:00
67 lines
2.1 KiB
PHP
67 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace PhpParser;
|
|
|
|
abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
|
|
{
|
|
protected function getTests($directory, $fileExtension) {
|
|
$it = new \RecursiveDirectoryIterator($directory);
|
|
$it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
|
|
$it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
|
|
|
|
$tests = array();
|
|
foreach ($it as $file) {
|
|
$fileName = realpath($file->getPathname());
|
|
$fileContents = file_get_contents($fileName);
|
|
|
|
// evaluate @@{expr}@@ expressions
|
|
$fileContents = preg_replace_callback(
|
|
'/@@\{(.*?)\}@@/',
|
|
function($matches) {
|
|
return eval('return ' . $matches[1] . ';');
|
|
},
|
|
$fileContents
|
|
);
|
|
|
|
// parse sections
|
|
$parts = array_map('trim', explode('-----', $fileContents));
|
|
|
|
// first part is the name
|
|
$name = array_shift($parts) . ' (' . $fileName . ')';
|
|
|
|
// multiple sections possible with always two forming a pair
|
|
foreach (array_chunk($parts, 2) as $chunk) {
|
|
list($expected, $mode) = $this->extractMode($this->canonicalize($chunk[1]));
|
|
$tests[] = array($name, $chunk[0], $expected, $mode);
|
|
}
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
private function extractMode($expected) {
|
|
$firstNewLine = strpos($expected, "\n");
|
|
if (false === $firstNewLine) {
|
|
return [$expected, null];
|
|
}
|
|
|
|
$firstLine = substr($expected, 0, $firstNewLine);
|
|
if (0 !== strpos($firstLine, '!!')) {
|
|
return [$expected, null];
|
|
}
|
|
|
|
return [substr($expected, $firstNewLine + 1), substr($firstLine, 2)];
|
|
}
|
|
|
|
protected function canonicalize($str) {
|
|
// trim from both sides
|
|
$str = trim($str);
|
|
|
|
// normalize EOL to \n
|
|
$str = str_replace(array("\r\n", "\r"), "\n", $str);
|
|
|
|
// trim right side of all lines
|
|
return implode("\n", array_map('rtrim', explode("\n", $str)));
|
|
}
|
|
}
|