1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Extractor\Tests\Functional\Visitor\Php; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Translation\Extractor\FileExtractor\PHPFileExtractor; |
16
|
|
|
use Symfony\Component\Finder\Finder; |
17
|
|
|
use Translation\Extractor\Model\SourceCollection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
21
|
|
|
*/ |
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
|
|
|
|