1
|
|
|
<?php |
2
|
|
|
namespace ParaTest\Parser; |
3
|
|
|
|
4
|
|
|
class Parser |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* The path to the source file to parse |
8
|
|
|
* |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
private $path; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \ReflectionClass |
15
|
|
|
*/ |
16
|
|
|
private $refl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Matches a test method beginning with the conventional "test" |
20
|
|
|
* word |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private static $testName = '/^test/'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A pattern for matching test methods that use the @test annotation |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private static $testAnnotation = '/@test\b/'; |
32
|
|
|
|
33
|
30 |
|
public function __construct($srcPath) |
34
|
|
|
{ |
35
|
30 |
|
if (!file_exists($srcPath)) { |
36
|
1 |
|
throw new \InvalidArgumentException("file not found: " . $srcPath); |
37
|
|
|
} |
38
|
|
|
|
39
|
29 |
|
$this->path = $srcPath; |
40
|
29 |
|
$declaredClasses = get_declared_classes(); |
41
|
29 |
|
require_once($this->path); |
42
|
29 |
|
$class = $this->getClassName($this->path, $declaredClasses); |
43
|
29 |
|
if (!$class) { |
44
|
2 |
|
throw new NoClassInFileException(); |
45
|
|
|
} |
46
|
|
|
try { |
47
|
27 |
|
$this->refl = new \ReflectionClass($class); |
48
|
27 |
|
} catch (\ReflectionException $e) { |
49
|
|
|
throw new \InvalidArgumentException("Unable to instantiate ReflectionClass. " . $class . " not found in: " . $srcPath); |
50
|
|
|
} |
51
|
27 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the fully constructed class |
55
|
|
|
* with methods or null if the class is abstract |
56
|
|
|
* |
57
|
|
|
* @return null|ParsedClass |
58
|
|
|
*/ |
59
|
27 |
|
public function getClass() |
60
|
|
|
{ |
61
|
27 |
|
return ($this->refl->isAbstract()) |
62
|
27 |
|
? null |
63
|
27 |
|
: new ParsedClass( |
64
|
27 |
|
$this->refl->getDocComment(), |
65
|
27 |
|
$this->getCleanReflectionName(), |
66
|
27 |
|
$this->refl->getNamespaceName(), |
67
|
27 |
|
$this->getMethods() |
68
|
27 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return reflection name with null bytes stripped |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
27 |
|
private function getCleanReflectionName() |
77
|
|
|
{ |
78
|
27 |
|
return str_replace("\x00", '', $this->refl->getName()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return all test methods present in the file |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
27 |
|
private function getMethods() |
87
|
|
|
{ |
88
|
27 |
|
$tests = array(); |
89
|
27 |
|
$methods = $this->refl->getMethods(\ReflectionMethod::IS_PUBLIC); |
90
|
27 |
|
foreach ($methods as $method) { |
91
|
25 |
|
$hasTestName = preg_match(self::$testName, $method->getName()); |
92
|
25 |
|
$hasTestAnnotation = preg_match(self::$testAnnotation, $method->getDocComment()); |
93
|
25 |
|
$isTestMethod = $hasTestName || $hasTestAnnotation; |
94
|
25 |
|
if ($isTestMethod) { |
95
|
25 |
|
$tests[] = new ParsedFunction($method->getDocComment(), 'public', $method->getName()); |
96
|
25 |
|
} |
97
|
27 |
|
} |
98
|
27 |
|
return $tests; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the class name of the class contained |
103
|
|
|
* in the file |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
29 |
|
private function getClassName($filename, $previousDeclaredClasses) |
108
|
|
|
{ |
109
|
29 |
|
$filename = realpath($filename); |
110
|
29 |
|
$classes = get_declared_classes(); |
111
|
29 |
|
$newClasses = array_values(array_diff($classes, $previousDeclaredClasses)); |
112
|
|
|
|
113
|
29 |
|
foreach ($newClasses as $className) { |
114
|
11 |
|
$class = new \ReflectionClass($className); |
115
|
11 |
|
if ($class->getFileName() == $filename) { |
116
|
11 |
|
if ($this->classNameMatchesFileName($filename, $className)) { |
117
|
9 |
|
return $className; |
118
|
|
|
} |
119
|
4 |
|
} |
120
|
26 |
|
} |
121
|
|
|
|
122
|
|
|
// Test class was loaded before somehow (referenced from other test class, or explicitly loaded) |
123
|
24 |
|
foreach ($classes as $className) { |
124
|
24 |
|
$class = new \ReflectionClass($className); |
125
|
24 |
|
if ($class->getFileName() == $filename) { |
126
|
22 |
|
return $className; |
127
|
|
|
} |
128
|
24 |
|
} |
129
|
2 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $filename |
133
|
|
|
* @param $className |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
11 |
|
private function classNameMatchesFileName($filename, $className) |
137
|
|
|
{ |
138
|
11 |
|
return strpos($filename, $className) !== false |
139
|
11 |
|
|| strpos($filename, $this->invertSlashes($className)) !== false; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
private function invertSlashes($className) |
143
|
|
|
{ |
144
|
4 |
|
return str_replace('\\', '/', $className); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|