Skip to content

Commit d3dc62f

Browse files
committed
remove some debug and add some testing placeholders
1 parent a823bae commit d3dc62f

File tree

6 files changed

+319
-0
lines changed

6 files changed

+319
-0
lines changed

src/Report/Cobertura.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report;
11+
12+
use function count;
13+
use function dirname;
14+
use function file_put_contents;
15+
use function max;
16+
use function range;
17+
use function time;
18+
use DOMDocument;
19+
use SebastianBergmann\CodeCoverage\CodeCoverage;
20+
use SebastianBergmann\CodeCoverage\Directory;
21+
use SebastianBergmann\CodeCoverage\Driver\WriteOperationFailedException;
22+
use SebastianBergmann\CodeCoverage\Node\File;
23+
24+
/**
25+
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
26+
*/
27+
final class Cobertura
28+
{
29+
/**
30+
* @throws WriteOperationFailedException
31+
*/
32+
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
33+
{
34+
$time = (string) time();
35+
36+
$xmlDocument = new DOMDocument('1.0', 'UTF-8');
37+
$xmlDocument->formatOutput = true;
38+
39+
$xmlCoverage = $xmlDocument->createElement('coverage');
40+
$xmlCoverage->setAttribute('line-rate', '');
41+
$xmlCoverage->setAttribute('branch-rate', '');
42+
$xmlCoverage->setAttribute('lines-covered', '');
43+
$xmlCoverage->setAttribute('lines-valid', '');
44+
$xmlCoverage->setAttribute('branches-covered', '');
45+
$xmlCoverage->setAttribute('branches-valid', '');
46+
$xmlCoverage->setAttribute('complexity', '');
47+
$xmlCoverage->setAttribute('version', '0.4');
48+
$xmlCoverage->setAttribute('timestamp', $time);
49+
$xmlDocument->appendChild($xmlCoverage);
50+
51+
$xmlPackages = $xmlDocument->createElement('packages');
52+
$xmlCoverage->appendChild($xmlPackages);
53+
54+
$xmlPackage = $xmlDocument->createElement('package');
55+
$xmlPackages->appendChild($xmlPackage);
56+
57+
$report = $coverage->getReport();
58+
59+
foreach ($report as $item) {
60+
if (!$item instanceof File) {
61+
continue;
62+
}
63+
64+
$classes = $item->classesAndTraits();
65+
$coverageData = $item->lineCoverageData();
66+
67+
foreach ($classes as $className => $class) {
68+
$xmlClass = $xmlDocument->createElement('class');
69+
$xmlClass->setAttribute('name', $className);
70+
$xmlClass->setAttribute('filename', $item->pathAsString());
71+
$xmlClass->setAttribute('line-rate', '');
72+
$xmlClass->setAttribute('branch-rate', '');
73+
$xmlClass->setAttribute('complexity', (string) $class['ccn']);
74+
$xmlPackage->appendChild($xmlClass);
75+
76+
$xmlMethods = $xmlDocument->createElement('methods');
77+
$xmlClass->appendChild($xmlMethods);
78+
79+
$xmlClassLines = $xmlDocument->createElement('lines');
80+
$xmlClass->appendChild($xmlClassLines);
81+
82+
foreach ($class['methods'] as $methodName => $method) {
83+
if ($method['executableLines'] == 0) {
84+
continue;
85+
}
86+
87+
$methodCount = 0;
88+
89+
foreach (range($method['startLine'], $method['endLine']) as $line) {
90+
if (isset($coverageData[$line]) && ($coverageData[$line] !== null)) {
91+
92+
$methodCount = max($methodCount, count($coverageData[$line]));
93+
94+
$xmlClassLine = $xmlDocument->createElement('line');
95+
$xmlClassLine->setAttribute('number', (string) $line);
96+
$xmlClassLine->setAttribute('hits', (string) count($coverageData[$line]));
97+
$xmlClassLines->appendChild($xmlClassLine);
98+
}
99+
}
100+
101+
$xmlMethod = $xmlDocument->createElement('method');
102+
$xmlMethod->setAttribute('name', $methodName);
103+
$xmlMethod->setAttribute('signature', '');
104+
$xmlMethod->setAttribute('line-rate', '');
105+
$xmlMethod->setAttribute('branch-rate', '');
106+
$xmlMethod->setAttribute('complexity', (string) $method['ccn']);
107+
108+
$xmlLines = $xmlDocument->createElement('lines');
109+
$xmlMethod->appendChild($xmlLines);
110+
111+
$xmlLine = $xmlDocument->createElement('line');
112+
$xmlLine->setAttribute('number', (string) $method['startLine']);
113+
$xmlLine->setAttribute('hits', (string) $methodCount);
114+
$xmlLines->appendChild($xmlLine);
115+
116+
$xmlMethods->appendChild($xmlMethod);
117+
}
118+
}
119+
}
120+
121+
$buffer = $xmlDocument->saveXML();
122+
123+
if ($target !== null) {
124+
Directory::create(dirname($target));
125+
126+
if (@file_put_contents($target, $buffer) === false) {
127+
throw new WriteOperationFailedException($target);
128+
}
129+
}
130+
131+
return $buffer;
132+
}
133+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<coverage line-rate="" branch-rate="" lines-covered="" lines-valid="" branches-covered="" branches-valid="" complexity="" version="0.4" timestamp="%i">
3+
<packages>
4+
<package>
5+
<class name="BankAccount" filename="%s%eBankAccount.php" line-rate="" branch-rate="" complexity="5">
6+
<methods>
7+
<method name="getBalance" signature="" line-rate="" branch-rate="" complexity="1">
8+
<lines>
9+
<line number="6" hits="2"/>
10+
</lines>
11+
</method>
12+
<method name="setBalance" signature="" line-rate="" branch-rate="" complexity="2">
13+
<lines>
14+
<line number="11" hits="0"/>
15+
</lines>
16+
</method>
17+
<method name="depositMoney" signature="" line-rate="" branch-rate="" complexity="1">
18+
<lines>
19+
<line number="20" hits="2"/>
20+
</lines>
21+
</method>
22+
<method name="withdrawMoney" signature="" line-rate="" branch-rate="" complexity="1">
23+
<lines>
24+
<line number="27" hits="2"/>
25+
</lines>
26+
</method>
27+
</methods>
28+
<lines>
29+
<line number="8" hits="2"/>
30+
<line number="13" hits="0"/>
31+
<line number="14" hits="0"/>
32+
<line number="15" hits="0"/>
33+
<line number="16" hits="0"/>
34+
<line number="18" hits="0"/>
35+
<line number="22" hits="2"/>
36+
<line number="24" hits="1"/>
37+
<line number="29" hits="2"/>
38+
<line number="31" hits="1"/>
39+
</lines>
40+
</class>
41+
</package>
42+
</packages>
43+
</coverage>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<coverage line-rate="" branch-rate="" lines-covered="" lines-valid="" branches-covered="" branches-valid="" complexity="" version="0.4" timestamp="%i">
3+
<packages>
4+
<package>
5+
<class name="BankAccount" filename="%s%eBankAccount.php" line-rate="" branch-rate="" complexity="5">
6+
<methods>
7+
<method name="getBalance" signature="" line-rate="" branch-rate="" complexity="1">
8+
<lines>
9+
<line number="6" hits="2"/>
10+
</lines>
11+
</method>
12+
<method name="setBalance" signature="" line-rate="" branch-rate="" complexity="2">
13+
<lines>
14+
<line number="11" hits="0"/>
15+
</lines>
16+
</method>
17+
<method name="depositMoney" signature="" line-rate="" branch-rate="" complexity="1">
18+
<lines>
19+
<line number="20" hits="2"/>
20+
</lines>
21+
</method>
22+
<method name="withdrawMoney" signature="" line-rate="" branch-rate="" complexity="1">
23+
<lines>
24+
<line number="27" hits="2"/>
25+
</lines>
26+
</method>
27+
</methods>
28+
<lines>
29+
<line number="8" hits="2"/>
30+
<line number="13" hits="0"/>
31+
<line number="14" hits="0"/>
32+
<line number="15" hits="0"/>
33+
<line number="16" hits="0"/>
34+
<line number="18" hits="0"/>
35+
<line number="22" hits="2"/>
36+
<line number="24" hits="1"/>
37+
<line number="29" hits="2"/>
38+
<line number="31" hits="1"/>
39+
</lines>
40+
</class>
41+
</package>
42+
</packages>
43+
</coverage>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<coverage line-rate="" branch-rate="" lines-covered="" lines-valid="" branches-covered="" branches-valid="" complexity="" version="0.4" timestamp="%i">
3+
<packages>
4+
<package>
5+
<class name="CoveredClassWithAnonymousFunctionInStaticMethod" filename="%s%esource_with_class_and_anonymous_function.php" line-rate="" branch-rate="" complexity="1">
6+
<methods>
7+
<method name="runAnonymous" signature="" line-rate="" branch-rate="" complexity="1">
8+
<lines>
9+
<line number="5" hits="1"/>
10+
</lines>
11+
</method>
12+
</methods>
13+
<lines>
14+
<line number="7" hits="1"/>
15+
<line number="9" hits="1"/>
16+
<line number="10" hits="0"/>
17+
<line number="11" hits="1"/>
18+
<line number="12" hits="1"/>
19+
<line number="13" hits="1"/>
20+
<line number="14" hits="1"/>
21+
<line number="17" hits="1"/>
22+
<line number="18" hits="1"/>
23+
</lines>
24+
</class>
25+
</package>
26+
</packages>
27+
</coverage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<coverage line-rate="" branch-rate="" lines-covered="" lines-valid="" branches-covered="" branches-valid="" complexity="" version="0.4" timestamp="%i">
3+
<packages>
4+
<package>
5+
<class name="Foo" filename="%s%esource_with_ignore.php" line-rate="" branch-rate="" complexity="1">
6+
<methods/>
7+
<lines/>
8+
</class>
9+
<class name="Bar" filename="%s%esource_with_ignore.php" line-rate="" branch-rate="" complexity="1">
10+
<methods/>
11+
<lines/>
12+
</class>
13+
</package>
14+
</packages>
15+
</coverage>

tests/tests/CoberturaTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report;
11+
12+
use SebastianBergmann\CodeCoverage\TestCase;
13+
14+
/**
15+
* @covers \SebastianBergmann\CodeCoverage\Report\Cobertura
16+
*/
17+
final class CoberturaTest extends TestCase
18+
{
19+
public function testLineCoverageForBankAccountTest(): void
20+
{
21+
$cobertura = new Cobertura;
22+
23+
$this->assertStringMatchesFormatFile(
24+
TEST_FILES_PATH . 'BankAccount-cobertura-line.xml',
25+
$cobertura->process($this->getLineCoverageForBankAccount(), null, 'BankAccount')
26+
);
27+
}
28+
29+
public function testPathCoverageForBankAccountTest(): void
30+
{
31+
$cobertura = new Cobertura;
32+
33+
$this->assertStringMatchesFormatFile(
34+
TEST_FILES_PATH . 'BankAccount-cobertura-path.xml',
35+
$cobertura->process($this->getPathCoverageForBankAccount(), null, 'BankAccount')
36+
);
37+
}
38+
39+
public function testCoberturaForFileWithIgnoredLines(): void
40+
{
41+
$cobertura = new Cobertura;
42+
43+
$this->assertStringMatchesFormatFile(
44+
TEST_FILES_PATH . 'ignored-lines-cobertura.xml',
45+
$cobertura->process($this->getCoverageForFileWithIgnoredLines())
46+
);
47+
}
48+
49+
public function testCoberturaForClassWithAnonymousFunction(): void
50+
{
51+
$cobertura = new Cobertura;
52+
53+
$this->assertStringMatchesFormatFile(
54+
TEST_FILES_PATH . 'class-with-anonymous-function-cobertura.xml',
55+
$cobertura->process($this->getCoverageForClassWithAnonymousFunction())
56+
);
57+
}
58+
}

0 commit comments

Comments
 (0)