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 Translation\Extractor\FileExtractor\PHPFileExtractor; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Translation\Extractor\Model\SourceCollection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class BasePHPVisitorTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param $visitor |
25
|
|
|
* @param $namespaceForTestFile |
26
|
|
|
* |
27
|
|
|
* @return SourceCollection |
28
|
|
|
* |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
14 |
|
protected function getSourceLocations($visitor, $namespaceForTestFile) |
32
|
|
|
{ |
33
|
14 |
|
$extractor = new PHPFileExtractor(); |
34
|
|
|
|
35
|
14 |
|
if (is_array($visitor)) { |
36
|
2 |
|
foreach ($visitor as $nodeVisitor) { |
37
|
1 |
|
$extractor->addVisitor($nodeVisitor); |
38
|
1 |
|
} |
39
|
1 |
|
} else { |
40
|
13 |
|
$extractor->addVisitor($visitor); |
41
|
|
|
} |
42
|
|
|
|
43
|
14 |
|
$currentNamespace = explode('\\', __NAMESPACE__); |
44
|
14 |
|
$fileNamespace = explode('\\', $namespaceForTestFile); |
45
|
14 |
|
$filename = array_pop($fileNamespace).'*'; |
46
|
|
|
|
47
|
14 |
|
$path = __DIR__.'/../../..'; |
48
|
14 |
|
foreach ($fileNamespace as $i => $part) { |
49
|
14 |
|
if ($currentNamespace[$i] !== $part) { |
50
|
|
|
// Assert: The namespaces is different now |
51
|
14 |
|
for ($j = $i; $j < count($fileNamespace); ++$j) { |
|
|
|
|
52
|
14 |
|
$path .= '/'.$fileNamespace[$j]; |
53
|
14 |
|
} |
54
|
|
|
|
55
|
14 |
|
break; |
56
|
|
|
} |
57
|
14 |
|
} |
58
|
|
|
|
59
|
14 |
|
$finder = new Finder(); |
60
|
14 |
|
$finder->files()->name($filename)->in($path); |
61
|
|
|
|
62
|
14 |
|
if ($finder->count() === 0) { |
63
|
|
|
throw new \Exception("Cannot find file for: $namespaceForTestFile. Tried path: $path - Maybe filename doesn't match the class?"); |
64
|
|
|
} |
65
|
|
|
|
66
|
14 |
|
$collection = new SourceCollection(); |
67
|
14 |
|
foreach ($finder as $file) { |
68
|
14 |
|
$extractor->getSourceLocations($file, $collection); |
69
|
14 |
|
} |
70
|
|
|
|
71
|
14 |
|
return $collection; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: