2011-11-26 16:09:39 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
|
2011-11-26 16:09:39 +01:00
|
|
|
{
|
2012-05-11 16:18:14 +02:00
|
|
|
protected function getTests($directory, $fileExtension) {
|
2016-04-20 15:03:18 +02:00
|
|
|
$directory = realpath($directory);
|
2014-02-06 14:44:16 +01:00
|
|
|
$it = new \RecursiveDirectoryIterator($directory);
|
|
|
|
$it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
|
|
|
|
$it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
|
2011-11-26 16:09:39 +01:00
|
|
|
|
2011-11-27 21:43:27 +01:00
|
|
|
$tests = array();
|
2011-11-26 16:09:39 +01:00
|
|
|
foreach ($it as $file) {
|
2016-04-20 15:03:18 +02:00
|
|
|
$fileName = $file->getPathname();
|
2015-03-23 11:43:22 +01:00
|
|
|
$fileContents = file_get_contents($fileName);
|
2016-04-05 03:17:30 +02:00
|
|
|
$fileContents = canonicalize($fileContents);
|
2011-11-26 16:09:39 +01:00
|
|
|
|
|
|
|
// evaluate @@{expr}@@ expressions
|
2012-03-02 00:28:46 +01:00
|
|
|
$fileContents = preg_replace_callback(
|
|
|
|
'/@@\{(.*?)\}@@/',
|
2015-05-02 22:35:15 +02:00
|
|
|
function($matches) {
|
|
|
|
return eval('return ' . $matches[1] . ';');
|
|
|
|
},
|
2012-03-02 00:28:46 +01:00
|
|
|
$fileContents
|
|
|
|
);
|
2011-11-26 16:09:39 +01:00
|
|
|
|
2011-11-27 11:20:35 +01:00
|
|
|
// parse sections
|
2016-04-16 21:39:49 +02:00
|
|
|
$parts = preg_split("/\n-----(?:\n|$)/", $fileContents);
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
// first part is the name
|
2015-03-12 13:17:31 +01:00
|
|
|
$name = array_shift($parts) . ' (' . $fileName . ')';
|
2016-04-20 15:03:18 +02:00
|
|
|
$shortName = ltrim(str_replace($directory, '', $fileName), '/\\');
|
2011-11-27 21:43:27 +01:00
|
|
|
|
|
|
|
// multiple sections possible with always two forming a pair
|
2015-08-25 21:36:08 +02:00
|
|
|
$chunks = array_chunk($parts, 2);
|
|
|
|
foreach ($chunks as $i => $chunk) {
|
|
|
|
$dataSetName = $shortName . (count($chunks) > 1 ? '#' . $i : '');
|
2016-04-05 03:17:30 +02:00
|
|
|
list($expected, $mode) = $this->extractMode($chunk[1]);
|
2015-08-25 21:36:08 +02:00
|
|
|
$tests[$dataSetName] = array($name, $chunk[0], $expected, $mode);
|
2011-11-27 21:43:27 +01:00
|
|
|
}
|
2011-11-26 16:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
2011-11-26 17:10:18 +01:00
|
|
|
|
2015-06-13 15:10:46 +02:00
|
|
|
private function extractMode($expected) {
|
|
|
|
$firstNewLine = strpos($expected, "\n");
|
|
|
|
if (false === $firstNewLine) {
|
2015-06-13 15:42:09 +02:00
|
|
|
$firstNewLine = strlen($expected);
|
2015-06-13 15:10:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$firstLine = substr($expected, 0, $firstNewLine);
|
|
|
|
if (0 !== strpos($firstLine, '!!')) {
|
|
|
|
return [$expected, null];
|
|
|
|
}
|
|
|
|
|
2015-06-13 15:42:09 +02:00
|
|
|
$expected = (string) substr($expected, $firstNewLine + 1);
|
|
|
|
return [$expected, substr($firstLine, 2)];
|
2015-06-13 15:10:46 +02:00
|
|
|
}
|
2015-03-12 13:17:31 +01:00
|
|
|
}
|