1 | <?php |
||
22 | abstract class BasePHPVisitorTest extends TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @param $visitor |
||
26 | * @param $namespaceForTestFile |
||
27 | * |
||
28 | * @return SourceCollection |
||
29 | * |
||
30 | * @throws \Exception |
||
31 | */ |
||
32 | protected function getSourceLocations($visitor, $namespaceForTestFile) |
||
33 | { |
||
34 | $extractor = new PHPFileExtractor(); |
||
35 | |||
36 | if (is_array($visitor)) { |
||
37 | foreach ($visitor as $nodeVisitor) { |
||
38 | $extractor->addVisitor($nodeVisitor); |
||
39 | } |
||
40 | } else { |
||
41 | $extractor->addVisitor($visitor); |
||
42 | } |
||
43 | |||
44 | $currentNamespace = explode('\\', __NAMESPACE__); |
||
45 | $fileNamespace = explode('\\', $namespaceForTestFile); |
||
46 | $filename = array_pop($fileNamespace).'*'; |
||
47 | |||
48 | $path = __DIR__.'/../../..'; |
||
49 | foreach ($fileNamespace as $i => $part) { |
||
50 | if ($currentNamespace[$i] !== $part) { |
||
51 | // Assert: The namespaces is different now |
||
52 | for ($j = $i; $j < count($fileNamespace); ++$j) { |
||
53 | $path .= '/'.$fileNamespace[$j]; |
||
54 | } |
||
55 | |||
56 | break; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | $finder = new Finder(); |
||
61 | $finder->files()->name($filename)->in($path); |
||
62 | |||
63 | if (0 === $finder->count()) { |
||
64 | throw new \Exception("Cannot find file for: $namespaceForTestFile. Tried path: $path - Maybe filename doesn't match the class?"); |
||
65 | } |
||
66 | |||
67 | $collection = new SourceCollection(); |
||
68 | foreach ($finder as $file) { |
||
69 | $extractor->getSourceLocations($file, $collection); |
||
70 | } |
||
71 | |||
72 | return $collection; |
||
73 | } |
||
74 | } |
||
75 |