Skip to content

Commit c1888bb

Browse files
Implement option for not generating the <source> element
1 parent 4511161 commit c1888bb

File tree

7 files changed

+98
-15
lines changed

7 files changed

+98
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
name: CI
88

99
env:
10-
COMPOSER_ROOT_VERSION: 12.4.x-dev
10+
COMPOSER_ROOT_VERSION: 12.5.x-dev
1111
PHP_VERSION: 8.4
1212

1313
jobs:

ChangeLog-12.4.md renamed to ChangeLog-12.5.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44

5-
## [12.4.1] - 2025-MM-DD
5+
## [12.5.0] - 2025-MM-DD
6+
7+
### Added
8+
9+
* Option to not generate the `<source>` element for the XML report
610

711
### Changed
812

@@ -11,11 +15,4 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1115
* [#1108](https://github.com/sebastianbergmann/php-code-coverage/pull/1108): Do not sort covered files data over and over
1216
* [#1109](https://github.com/sebastianbergmann/php-code-coverage/pull/1109): Represent line coverage data using objects
1317

14-
## [12.4.0] - 2025-09-24
15-
16-
### Added
17-
18-
* [#1095](https://github.com/sebastianbergmann/php-code-coverage/pull/1095): Support for light/dark mode in HTML report
19-
20-
[12.4.1]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.4.0...main
21-
[12.4.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.3.8...12.4.0
18+
[12.5.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/12.4.0...main

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
"extra": {
6363
"branch-alias": {
64-
"dev-main": "12.4.x-dev"
64+
"dev-main": "12.5.x-dev"
6565
}
6666
}
6767
}

src/Report/Xml/Facade.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ final class Facade
4646
private string $target;
4747
private Project $project;
4848
private readonly string $phpUnitVersion;
49+
private readonly bool $includeSource;
4950

50-
public function __construct(string $version)
51+
public function __construct(string $version, bool $includeSource = true)
5152
{
5253
$this->phpUnitVersion = $version;
54+
$this->includeSource = $includeSource;
5355
}
5456

5557
/**
@@ -170,9 +172,11 @@ private function processFile(FileNode $file, Directory $context): void
170172
$coverage->finalize($tests);
171173
}
172174

173-
$fileReport->source()->setSourceCode(
174-
file_get_contents($file->pathAsString()),
175-
);
175+
if ($this->includeSource) {
176+
$fileReport->source()->setSourceCode(
177+
file_get_contents($file->pathAsString()),
178+
);
179+
}
176180

177181
$this->saveDocument($fileReport->asDom(), $file->id());
178182
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="https://schema.phpunit.de/coverage/1.0">
3+
<file name="BankAccount.php" path="%e">
4+
<totals>
5+
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
6+
<methods count="4" tested="3" percent="75.00"/>
7+
<functions count="0" tested="0" percent="0"/>
8+
<classes count="1" tested="0" percent="0.00"/>
9+
<traits count="0" tested="0" percent="0"/>
10+
</totals>
11+
<class name="BankAccount" start="2" executable="8" executed="5" crap="6.32">
12+
<namespace name=""/>
13+
<method name="getBalance" signature="getBalance()" start="6" end="9" crap="1" executable="1" executed="1" coverage="100"/>
14+
<method name="setBalance" signature="setBalance($balance)" start="11" end="18" crap="6" executable="3" executed="0" coverage="0"/>
15+
<method name="depositMoney" signature="depositMoney($balance)" start="20" end="25" crap="1" executable="2" executed="2" coverage="100"/>
16+
<method name="withdrawMoney" signature="withdrawMoney($balance)" start="27" end="33" crap="1" executable="2" executed="2" coverage="100"/>
17+
</class>
18+
<coverage>
19+
<line nr="8">
20+
<covered by="BankAccountTest::testBalanceIsInitiallyZero"/>
21+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
22+
</line>
23+
<line nr="22">
24+
<covered by="BankAccountTest::testBalanceCannotBecomeNegative2"/>
25+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
26+
</line>
27+
<line nr="24">
28+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
29+
</line>
30+
<line nr="29">
31+
<covered by="BankAccountTest::testBalanceCannotBecomeNegative"/>
32+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
33+
</line>
34+
<line nr="31">
35+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
36+
</line>
37+
</coverage>
38+
</file>
39+
</phpunit>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="https://schema.phpunit.de/coverage/1.0">
3+
<build time="%s" phpunit="%s" coverage="%s">
4+
<runtime name="%s" version="%s" url="%s"/>
5+
<driver%S/>
6+
</build>
7+
<project source="%s">
8+
<tests>
9+
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" status="unknown"/>
10+
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" status="unknown"/>
11+
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" status="unknown"/>
12+
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" status="unknown"/>
13+
</tests>
14+
<directory name="%s">
15+
<totals>
16+
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
17+
<methods count="4" tested="3" percent="75.00"/>
18+
<functions count="0" tested="0" percent="0"/>
19+
<classes count="1" tested="0" percent="0.00"/>
20+
<traits count="0" tested="0" percent="0"/>
21+
</totals>
22+
<file name="BankAccount.php" href="BankAccount.php.xml">
23+
<totals>
24+
<lines total="35" comments="0" code="35" executable="8" executed="5" percent="62.50"/>
25+
<methods count="4" tested="3" percent="75.00"/>
26+
<functions count="0" tested="0" percent="0"/>
27+
<classes count="1" tested="0" percent="0.00"/>
28+
<traits count="0" tested="0" percent="0"/>
29+
</totals>
30+
</file>
31+
</directory>
32+
</project>
33+
</phpunit>

tests/tests/Report/XmlTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function testForBankAccountTest(): void
4747
$this->assertFilesEquals($expectedFilesPath, TEST_FILES_PATH . 'tmp');
4848
}
4949

50+
public function testForBankAccountTestWithoutSource(): void
51+
{
52+
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccountWithoutSource';
53+
54+
$xml = new Facade('1.0.0', false);
55+
$xml->process($this->getLineCoverageForBankAccount(), TEST_FILES_PATH . 'tmp');
56+
57+
$this->assertFilesEquals($expectedFilesPath, TEST_FILES_PATH . 'tmp');
58+
}
59+
5060
public function testForFileWithIgnoredLines(): void
5161
{
5262
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines';

0 commit comments

Comments
 (0)