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