Skip to content

Commit fc6dd38

Browse files
committed
Added Coverage info to token files
Changed path resolution (Fixes issue #147)
1 parent cc4f52b commit fc6dd38

File tree

2 files changed

+90
-30
lines changed

2 files changed

+90
-30
lines changed

src/generator/enricher/phpunit/PHPUnit.php

+85-26
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
use TheSeer\phpDox\FileInfo;
88
use TheSeer\phpDox\Generator\ClassStartEvent;
99
use TheSeer\phpDox\Generator\PHPDoxEndEvent;
10+
use TheSeer\phpDox\Generator\TokenFileStartEvent;
1011
use TheSeer\phpDox\Generator\TraitStartEvent;
1112

12-
class PHPUnit extends AbstractEnricher implements EndEnricherInterface, ClassEnricherInterface, TraitEnricherInterface {
13+
class PHPUnit extends AbstractEnricher implements
14+
EndEnricherInterface, ClassEnricherInterface, TraitEnricherInterface, TokenFileEnricherInterface {
1315

1416
const XMLNS = 'http://schema.phpunit.de/coverage/1.0';
1517

1618
/**
17-
* @var PHPUnitConfig
19+
* @var Fileinfo
1820
*/
19-
private $config;
21+
private $coveragePath;
2022

2123
/**
2224
* @var fDOMDocument
@@ -27,8 +29,13 @@ class PHPUnit extends AbstractEnricher implements EndEnricherInterface, ClassEnr
2729
private $results = array();
2830
private $coverage = array();
2931

32+
/**
33+
* @param PHPUnitConfig $config
34+
*
35+
* @throws EnricherException
36+
*/
3037
public function __construct(PHPUnitConfig $config) {
31-
$this->config = $config;
38+
$this->coveragePath = $config->getCoveragePath();
3239
$this->index = $this->loadXML('index.xml');
3340
}
3441

@@ -69,36 +76,40 @@ public function enrichTrait(TraitStartEvent $event) {
6976
$this->enrichByFile($event->getTrait()->asDom());
7077
}
7178

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
7690
}
7791

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+
}
8993

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
93100
}
94-
95-
$refDom = $this->loadXML($phpunitFileNode->getAttribute('href'));
96-
$this->processUnit($dom, $refDom);
97101
}
98102

103+
/**
104+
* @param $fname
105+
*
106+
* @return fDOMDocument
107+
*
108+
* @throws EnricherException
109+
*/
99110
private function loadXML($fname) {
100111
try {
101-
$fname = $this->config->getCoveragePath() . '/' . $fname;
112+
$fname = (string)$this->coveragePath . '/' . $fname;
102113
if (!file_exists($fname)) {
103114
throw new EnricherException(
104115
sprintf('PHPUnit xml file "%s" not found.', $fname),
@@ -203,6 +214,54 @@ private function processUnit(fDOMDocument $unit, fDOMDocument $coverage) {
203214
$this->results[$classNamespace][$className] = $result;
204215
$this->coverage[$classNamespace][$className] = $coverageTarget->cloneNode(false);
205216
}
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+
}
206262
}
207263

264+
265+
class PHPUnitEnricherException extends EnricherException {
266+
}
208267
}

src/generator/enricher/phpunit/PHPUnitConfig.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TheSeer\phpDox\Generator\Enricher {
44

55
use TheSeer\fDOM\fDOMElement;
6+
use TheSeer\phpDox\FileInfo;
67
use TheSeer\phpDox\GeneratorConfig;
78

89
class PHPUnitConfig {
@@ -22,6 +23,9 @@ public function __construct(GeneratorConfig $generator, fDOMElement $ctx) {
2223
$this->generator = $generator;
2324
}
2425

26+
/**
27+
* @return FileInfo
28+
*/
2529
public function getCoveragePath() {
2630
$basedirDefault = dirname($this->context->ownerDocument->baseURI);
2731
$path = $basedirDefault . '/build/logs';
@@ -35,12 +39,9 @@ public function getCoveragePath() {
3539
} else {
3640
$path .= 'coverage';
3741
}
38-
return $path;
42+
return new FileInfo($path);
3943
}
4044

41-
public function getSourceDirectory() {
42-
return $this->generator->getProjectConfig()->getSourceDirectory();
43-
}
4445
}
4546

4647
}

0 commit comments

Comments
 (0)