7
7
use TheSeer \phpDox \FileInfo ;
8
8
use TheSeer \phpDox \Generator \ClassStartEvent ;
9
9
use TheSeer \phpDox \Generator \PHPDoxEndEvent ;
10
+ use TheSeer \phpDox \Generator \TokenFileStartEvent ;
10
11
use TheSeer \phpDox \Generator \TraitStartEvent ;
11
12
12
- class PHPUnit extends AbstractEnricher implements EndEnricherInterface, ClassEnricherInterface, TraitEnricherInterface {
13
+ class PHPUnit extends AbstractEnricher implements
14
+ EndEnricherInterface, ClassEnricherInterface, TraitEnricherInterface, TokenFileEnricherInterface {
13
15
14
16
const XMLNS = 'http://schema.phpunit.de/coverage/1.0 ' ;
15
17
16
18
/**
17
- * @var PHPUnitConfig
19
+ * @var Fileinfo
18
20
*/
19
- private $ config ;
21
+ private $ coveragePath ;
20
22
21
23
/**
22
24
* @var fDOMDocument
@@ -27,8 +29,13 @@ class PHPUnit extends AbstractEnricher implements EndEnricherInterface, ClassEnr
27
29
private $ results = array ();
28
30
private $ coverage = array ();
29
31
32
+ /**
33
+ * @param PHPUnitConfig $config
34
+ *
35
+ * @throws EnricherException
36
+ */
30
37
public function __construct (PHPUnitConfig $ config ) {
31
- $ this ->config = $ config ;
38
+ $ this ->coveragePath = $ config-> getCoveragePath () ;
32
39
$ this ->index = $ this ->loadXML ('index.xml ' );
33
40
}
34
41
@@ -69,36 +76,40 @@ public function enrichTrait(TraitStartEvent $event) {
69
76
$ this ->enrichByFile ($ event ->getTrait ()->asDom ());
70
77
}
71
78
72
- private function enrichByFile (fDOMDocument $ dom ) {
73
- $ fileNode = $ dom ->queryOne ('//phpdox:file ' );
74
- if (!$ fileNode ) {
75
- return ;
79
+ public function enrichTokenFile (TokenFileStartEvent $ event ) {
80
+ try {
81
+ $ tokenDom = $ event ->getTokenFile ()->asDom ();
82
+ $ coverageDom = $ this ->loadConverageInformation ($ tokenDom );
83
+ $ coverage = $ coverageDom ->queryOne ('//pu:coverage[pu:line] ' );
84
+ if ($ coverage ) {
85
+ $ container = $ this ->getEnrichtmentContainer ($ tokenDom ->documentElement , 'phpunit ' );
86
+ $ container ->appendChild ($ tokenDom ->importNode ($ coverage , true ));
87
+ }
88
+ } catch (PHPUnitEnricherException $ e ) {
89
+ // Silently ignore for now
76
90
}
77
91
78
- $ fileInfo = new FileInfo ($ fileNode ->getAttribute ('path ' ));
79
- $ srcDir = $ this ->config ->getSourceDirectory ();
80
- $ paths = explode ('/ ' , (string )$ fileInfo ->getRelative ($ srcDir ));
81
- $ file = $ fileNode ->getAttribute ('file ' );
82
- $ paths = array_slice ($ paths , 1 );
83
-
84
- $ query = sprintf ('//pu:project/pu:directory[@name = "%s"] ' , $ srcDir ->getRealPath ());
85
- foreach ($ paths as $ path ) {
86
- $ query .= sprintf ('/pu:directory[@name = "%s"] ' , $ path );
87
- }
88
- $ query .= sprintf ('/pu:file[@name = "%s"] ' , $ file );
92
+ }
89
93
90
- $ phpunitFileNode = $ this ->index ->queryOne ($ query );
91
- if (!$ phpunitFileNode ) {
92
- return ;
94
+ private function enrichByFile (fDOMDocument $ dom ) {
95
+ try {
96
+ $ coverageDom = $ this ->loadConverageInformation ($ dom );
97
+ $ this ->processUnit ($ dom , $ coverageDom );
98
+ } catch (PHPUnitEnricherException $ e ) {
99
+ // Silently ignore for now
93
100
}
94
-
95
- $ refDom = $ this ->loadXML ($ phpunitFileNode ->getAttribute ('href ' ));
96
- $ this ->processUnit ($ dom , $ refDom );
97
101
}
98
102
103
+ /**
104
+ * @param $fname
105
+ *
106
+ * @return fDOMDocument
107
+ *
108
+ * @throws EnricherException
109
+ */
99
110
private function loadXML ($ fname ) {
100
111
try {
101
- $ fname = $ this ->config -> getCoveragePath () . '/ ' . $ fname ;
112
+ $ fname = ( string ) $ this ->coveragePath . '/ ' . $ fname ;
102
113
if (!file_exists ($ fname )) {
103
114
throw new EnricherException (
104
115
sprintf ('PHPUnit xml file "%s" not found. ' , $ fname ),
@@ -203,6 +214,54 @@ private function processUnit(fDOMDocument $unit, fDOMDocument $coverage) {
203
214
$ this ->results [$ classNamespace ][$ className ] = $ result ;
204
215
$ this ->coverage [$ classNamespace ][$ className ] = $ coverageTarget ->cloneNode (false );
205
216
}
217
+
218
+ /**
219
+ * @return FileInfo
220
+ */
221
+ private function getSourceDirectory () {
222
+ $ baseDir = $ this ->index ->queryOne ('//pu:project/pu:directory ' );
223
+ $ srcDir = new FileInfo ($ baseDir ->getAttribute ('name ' ));
224
+ return $ srcDir ;
225
+ }
226
+
227
+ /**
228
+ * @param fDOMDocument $dom
229
+ *
230
+ * @return fDOMDocument
231
+ *
232
+ * @throws EnricherException
233
+ * @throws PHPUnitEnricherException
234
+ */
235
+ private function loadConverageInformation (fDOMDocument $ dom ) {
236
+ $ fileNode = $ dom ->queryOne ('//phpdox:file ' );
237
+ if (!$ fileNode ) {
238
+ throw new PHPUnitEnricherException ('No file header in event dom ' );
239
+ }
240
+
241
+ $ fileInfo = new FileInfo ($ fileNode ->getAttribute ('path ' ));
242
+ $ srcDir = $ this ->getSourceDirectory ();
243
+
244
+ $ paths = explode ('/ ' , (string )$ fileInfo ->getRelative ($ srcDir ));
245
+ $ file = $ fileNode ->getAttribute ('file ' );
246
+ $ paths = array_slice ($ paths , 1 );
247
+
248
+ $ query = sprintf ('//pu:project/pu:directory[@name = "%s"] ' , $ srcDir ->getRealPath ());
249
+ foreach ($ paths as $ path ) {
250
+ $ query .= sprintf ('/pu:directory[@name = "%s"] ' , $ path );
251
+ }
252
+ $ query .= sprintf ('/pu:file[@name = "%s"] ' , $ file );
253
+
254
+ $ phpunitFileNode = $ this ->index ->queryOne ($ query );
255
+ if (!$ phpunitFileNode ) {
256
+ throw new PHPUnitEnricherException ('No coverage information for file ' );
257
+ }
258
+
259
+ $ refDom = $ this ->loadXML ($ phpunitFileNode ->getAttribute ('href ' ));
260
+ return $ refDom ;
261
+ }
206
262
}
207
263
264
+
265
+ class PHPUnitEnricherException extends EnricherException {
266
+ }
208
267
}
0 commit comments