Skip to content

Commit 8ec19d0

Browse files
committed
Cobertura report: removed additional metrics, added comment to report and added XML document validation to one test
Removed additional metrics because they were not conform the document definition of Cobertura. Only added XML document validation to one test, because the validation requests the DTD file, which has an impact on the test duration/load.
1 parent 4f5db1d commit 8ec19d0

7 files changed

+40
-58
lines changed

src/Report/Cobertura/CoberturaCoverage.php

+10-43
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report\Cobertura;
1111

12+
use Composer\InstalledVersions;
1213
use DOMDocument;
1314
use DOMElement;
1415
use DOMImplementation;
@@ -31,38 +32,14 @@ class CoberturaCoverage extends CoberturaElement
3132
*/
3233
private $timestamp;
3334

34-
/**
35-
* @var int
36-
*/
37-
private $methodsValid;
38-
39-
/**
40-
* @var int
41-
*/
42-
private $methodsCovered;
43-
44-
/**
45-
* @var int
46-
*/
47-
private $classesValid;
48-
49-
/**
50-
* @var int
51-
*/
52-
private $classesCovered;
53-
5435
public static function create(Directory $report): self
5536
{
5637
$coverage = new self(
5738
time(),
5839
$report->numberOfExecutableLines(),
5940
$report->numberOfExecutedLines(),
6041
$report->numberOfExecutableBranches(),
61-
$report->numberOfExecutedBranches(),
62-
$report->numberOfFunctionsAndMethods(),
63-
$report->numberOfTestedFunctionsAndMethods(),
64-
$report->numberOfClassesAndTraits(),
65-
$report->numberOfTestedClassesAndTraits()
42+
$report->numberOfExecutedBranches()
6643
);
6744

6845
foreach ($report as $item) {
@@ -81,17 +58,9 @@ private function __construct(
8158
int $linesValid,
8259
int $linesCovered,
8360
int $branchesValid,
84-
int $branchesCovered,
85-
int $methodsValid,
86-
int $methodsCovered,
87-
int $classesValid,
88-
int $classesCovered
61+
int $branchesCovered
8962
) {
90-
$this->timestamp = $timestamp;
91-
$this->methodsValid = $methodsValid;
92-
$this->methodsCovered = $methodsCovered;
93-
$this->classesValid = $classesValid;
94-
$this->classesCovered = $classesCovered;
63+
$this->timestamp = $timestamp;
9564
parent::__construct($linesValid, $linesCovered, $branchesValid, $branchesCovered);
9665
}
9766

@@ -110,22 +79,20 @@ public function generateDocument(): DOMDocument
11079
$document->encoding = 'UTF-8';
11180
$document->formatOutput = true;
11281

113-
$methodRate = $this->methodsValid === 0 ? 0 : $this->methodsCovered / $this->methodsValid;
114-
$classRate = $this->classesValid === 0 ? 0 : $this->classesCovered / $this->classesValid;
82+
$comment = $document->createComment(sprintf(
83+
'Cobertura coverage report generated by the PHP library "%s" on %s.',
84+
InstalledVersions::getRootPackage()['name'],
85+
date('c', $this->timestamp),
86+
));
87+
$document->appendChild($comment);
11588

11689
$coverageElement = $document->createElement('coverage');
11790
$coverageElement->setAttribute('line-rate', (string) $this->lineRate());
11891
$coverageElement->setAttribute('branch-rate', (string) $this->branchRate());
119-
$coverageElement->setAttribute('method-rate', (string) $methodRate);
120-
$coverageElement->setAttribute('class-rate', (string) $classRate);
12192
$coverageElement->setAttribute('lines-covered', (string) $this->linesCovered);
12293
$coverageElement->setAttribute('lines-valid', (string) $this->linesValid);
12394
$coverageElement->setAttribute('branches-covered', (string) $this->branchesCovered);
12495
$coverageElement->setAttribute('branches-valid', (string) $this->branchesValid);
125-
$coverageElement->setAttribute('methods-covered', (string) $this->methodsCovered);
126-
$coverageElement->setAttribute('methods-valid', (string) $this->methodsValid);
127-
$coverageElement->setAttribute('classes-covered', (string) $this->classesCovered);
128-
$coverageElement->setAttribute('classes-valid', (string) $this->classesValid);
12996
$coverageElement->setAttribute('complexity', (string) $this->complexity());
13097
$coverageElement->setAttribute('version', '0.4');
13198
$coverageElement->setAttribute('timestamp', (string) $this->timestamp);

tests/_files/BankAccount-cobertura-line.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="0.55555555555556" branch-rate="0" method-rate="0.75" class-rate="0" lines-covered="5" lines-valid="9" branches-covered="0" branches-valid="0" methods-covered="3" methods-valid="4" classes-covered="0" classes-valid="1" complexity="5" version="0.4" timestamp="%i">
3+
<!--Cobertura coverage report generated by the PHP library "phpunit/php-code-coverage" on %s.-->
4+
<coverage line-rate="0.55555555555556" branch-rate="0" lines-covered="5" lines-valid="9" branches-covered="0" branches-valid="0" complexity="5" version="0.4" timestamp="%i">
45
<sources>
56
<source>tests%e_files</source>
67
</sources>

tests/_files/BankAccount-cobertura-path.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="0.55555555555556" branch-rate="0.42857142857143" method-rate="0.75" class-rate="0" lines-covered="5" lines-valid="9" branches-covered="3" branches-valid="7" methods-covered="3" methods-valid="4" classes-covered="0" classes-valid="1" complexity="5" version="0.4" timestamp="%i">
3+
<!--Cobertura coverage report generated by the PHP library "phpunit/php-code-coverage" on %s.-->
4+
<coverage line-rate="0.55555555555556" branch-rate="0.42857142857143" lines-covered="5" lines-valid="9" branches-covered="3" branches-valid="7" complexity="5" version="0.4" timestamp="%i">
45
<sources>
56
<source>tests%e_files</source>
67
</sources>

tests/_files/class-with-anonymous-function-cobertura.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="1" branch-rate="0" method-rate="1" class-rate="1" lines-covered="5" lines-valid="5" branches-covered="0" branches-valid="0" methods-covered="1" methods-valid="1" classes-covered="1" classes-valid="1" complexity="1" version="0.4" timestamp="%i">
3+
<!--Cobertura coverage report generated by the PHP library "phpunit/php-code-coverage" on %s.-->
4+
<coverage line-rate="1" branch-rate="0" lines-covered="5" lines-valid="5" branches-covered="0" branches-valid="0" complexity="1" version="0.4" timestamp="%i">
45
<sources>
56
<source>tests%e_files</source>
67
</sources>

tests/_files/class-with-outside-function-cobertura.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="0.75" branch-rate="0" method-rate="0.5" class-rate="1" lines-covered="3" lines-valid="4" branches-covered="0" branches-valid="0" methods-covered="1" methods-valid="2" classes-covered="1" classes-valid="1" complexity="3" version="0.4" timestamp="%i">
3+
<!--Cobertura coverage report generated by the PHP library "phpunit/php-code-coverage" on %s.-->
4+
<coverage line-rate="0.75" branch-rate="0" lines-covered="3" lines-valid="4" branches-covered="0" branches-valid="0" complexity="3" version="0.4" timestamp="%i">
45
<sources>
56
<source>tests%e_files</source>
67
</sources>

tests/_files/ignored-lines-cobertura.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3-
<coverage line-rate="1" branch-rate="0" method-rate="1" class-rate="0" lines-covered="1" lines-valid="1" branches-covered="0" branches-valid="0" methods-covered="1" methods-valid="1" classes-covered="0" classes-valid="0" complexity="2" version="0.4" timestamp="%i">
3+
<!--Cobertura coverage report generated by the PHP library "phpunit/php-code-coverage" on %s.-->
4+
<coverage line-rate="1" branch-rate="0" lines-covered="1" lines-valid="1" branches-covered="0" branches-valid="0" complexity="2" version="0.4" timestamp="%i">
45
<sources>
56
<source>tests%e_files</source>
67
</sources>

tests/tests/Report/CoberturaTest.php

+20-10
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,67 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use DOMDocument;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
final class CoberturaTest extends TestCase
1516
{
1617
public function testLineCoverageForBankAccountTest(): void
1718
{
18-
$cobertura = new Cobertura;
19+
$report = (new Cobertura)->process($this->getLineCoverageForBankAccount(), null);
1920

2021
$this->assertStringMatchesFormatFile(
2122
TEST_FILES_PATH . 'BankAccount-cobertura-line.xml',
22-
$cobertura->process($this->getLineCoverageForBankAccount(), null)
23+
$report
2324
);
25+
26+
$this->validateReport($report);
2427
}
2528

2629
public function testPathCoverageForBankAccountTest(): void
2730
{
28-
$cobertura = new Cobertura;
31+
$report = (new Cobertura)->process($this->getPathCoverageForBankAccount(), null);
2932

3033
$this->assertStringMatchesFormatFile(
3134
TEST_FILES_PATH . 'BankAccount-cobertura-path.xml',
32-
$cobertura->process($this->getPathCoverageForBankAccount(), null)
35+
$report
3336
);
3437
}
3538

3639
public function testCoberturaForFileWithIgnoredLines(): void
3740
{
38-
$cobertura = new Cobertura;
41+
$report = (new Cobertura)->process($this->getCoverageForFileWithIgnoredLines());
3942

4043
$this->assertStringMatchesFormatFile(
4144
TEST_FILES_PATH . 'ignored-lines-cobertura.xml',
42-
$cobertura->process($this->getCoverageForFileWithIgnoredLines())
45+
$report
4346
);
4447
}
4548

4649
public function testCoberturaForClassWithAnonymousFunction(): void
4750
{
48-
$cobertura = new Cobertura;
51+
$report = (new Cobertura)->process($this->getCoverageForClassWithAnonymousFunction());
4952

5053
$this->assertStringMatchesFormatFile(
5154
TEST_FILES_PATH . 'class-with-anonymous-function-cobertura.xml',
52-
$cobertura->process($this->getCoverageForClassWithAnonymousFunction())
55+
$report
5356
);
5457
}
5558

5659
public function testCoberturaForClassAndOutsideFunction(): void
5760
{
58-
$cobertura = new Cobertura;
61+
$report = (new Cobertura)->process($this->getCoverageForClassWithOutsideFunction());
5962

6063
$this->assertStringMatchesFormatFile(
6164
TEST_FILES_PATH . 'class-with-outside-function-cobertura.xml',
62-
$cobertura->process($this->getCoverageForClassWithOutsideFunction())
65+
$report
6366
);
6467
}
68+
69+
private function validateReport(string $coberturaReport): void
70+
{
71+
$document = (new DOMDocument);
72+
$this->assertTrue($document->loadXML($coberturaReport));
73+
$this->assertTrue($document->validate());
74+
}
6575
}

0 commit comments

Comments
 (0)