5 abstract class CodeTestAbstract extends \PHPUnit_Framework_TestCase
7 protected function getTests($directory, $fileExtension) {
8 $directory = realpath($directory);
9 $it = new \RecursiveDirectoryIterator($directory);
10 $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
11 $it = new \RegexIterator($it, '(\.' . preg_quote($fileExtension) . '$)');
14 foreach ($it as $file) {
15 $fileName = $file->getPathname();
16 $fileContents = file_get_contents($fileName);
17 $fileContents = canonicalize($fileContents);
19 // evaluate @@{expr}@@ expressions
20 $fileContents = preg_replace_callback(
23 return eval('return ' . $matches[1] . ';');
29 $parts = preg_split("/\n-----(?:\n|$)/", $fileContents);
31 // first part is the name
32 $name = array_shift($parts) . ' (' . $fileName . ')';
33 $shortName = ltrim(str_replace($directory, '', $fileName), '/\\');
35 // multiple sections possible with always two forming a pair
36 $chunks = array_chunk($parts, 2);
37 foreach ($chunks as $i => $chunk) {
38 $dataSetName = $shortName . (count($chunks) > 1 ? '#' . $i : '');
39 list($expected, $mode) = $this->extractMode($chunk[1]);
40 $tests[$dataSetName] = array($name, $chunk[0], $expected, $mode);
47 private function extractMode($expected) {
48 $firstNewLine = strpos($expected, "\n");
49 if (false === $firstNewLine) {
50 $firstNewLine = strlen($expected);
53 $firstLine = substr($expected, 0, $firstNewLine);
54 if (0 !== strpos($firstLine, '!!')) {
55 return [$expected, null];
58 $expected = (string) substr($expected, $firstNewLine + 1);
59 return [$expected, substr($firstLine, 2)];