Skip to content

Commit 11dde60

Browse files
committed
feat(coverage): add CoverageSummary and CoverageSummaryLoader for programmatic access to PHPUnit coverage data
Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent d7320b5 commit 11dde60

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

docs/api/phpunit-support.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,55 @@ The packaged test configuration includes a small integration layer under
1919
* - ``FastForward\DevTools\PhpUnit\Event\TestSuite\ByPassfinalsStartedSubscriber``
2020
- Enables ``DG\BypassFinals``
2121
- Allows tests to work with final constructs.
22-
* - ``FastForward\DevTools\PhpUnit\Event\TestSuite\JoliNotifExecutionFinishedSubscriber``
23-
- Sends desktop notifications
24-
- Summarizes pass, failure, error, runtime, and memory data.
22+
* - ``FastForward\DevTools\PhpUnit\Event\TestSuite\JoliNotifExecutionFinishedSubscriber``
23+
- Sends desktop notifications
24+
- Summarizes pass, failure, error, runtime, and memory data.
25+
* - ``FastForward\DevTools\PhpUnit\Coverage\CoverageSummaryLoaderInterface``
26+
- Loads PHPUnit coverage reports
27+
- Contract for loading serialized PHP coverage data.
28+
* - ``FastForward\DevTools\PhpUnit\Coverage\CoverageSummaryLoader``
29+
- Loads PHPUnit coverage reports
30+
- Implementation that reads ``coverage-php`` output.
31+
* - ``FastForward\DevTools\PhpUnit\Coverage\CoverageSummary``
32+
- Represents line coverage metrics
33+
- Provides executed lines, total executable lines, and percentage calculations.
34+
35+
Coverage Report Loading
36+
-----------------------
37+
38+
DevTools provides a reusable layer for loading PHPUnit's serialized
39+
``coverage-php`` output. This is useful when you need to extract line
40+
coverage metrics programmatically.
41+
42+
``CoverageSummaryLoaderInterface`` defines the contract:
43+
44+
.. code-block:: php
45+
46+
namespace FastForward\DevTools\PhpUnit\Coverage;
47+
48+
interface CoverageSummaryLoaderInterface
49+
{
50+
public function load(string $coverageReportPath): CoverageSummary;
51+
}
52+
53+
``CoverageSummaryLoader`` implements this contract:
54+
55+
.. code-block:: php
56+
57+
use FastForward\DevTools\PhpUnit\Coverage\CoverageSummaryLoader;
58+
59+
$loader = new CoverageSummaryLoader();
60+
$summary = $loader->load('public/coverage/coverage.php');
61+
62+
$summary->executedLines(); // Number of covered lines
63+
$summary->executableLines(); // Total number of executable lines
64+
$summary->percentage(); // Coverage as float (0-100)
65+
$summary->percentageAsString(); // Formatted string like "85.50%"
66+
67+
.. note::
68+
69+
The loader expects the PHP file produced by PHPUnit's ``--coverage-php`` option.
70+
It must contain a ``SebastianBergmann\CodeCoverage\CodeCoverage`` instance.
2571

2672
These classes are especially relevant when a consumer project overrides the
2773
packaged ``phpunit.xml`` and wants to preserve the same runtime behavior.

docs/usage/testing-and-coverage.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ That extension wires together:
5252
which sends a desktop notification after the run when the local platform
5353
supports it.
5454

55+
Programmatic Coverage Access
56+
-----------------------------
57+
58+
The ``CoverageSummaryLoader`` class provides programmatic access to coverage
59+
data. This is useful when you need to integrate coverage metrics into
60+
external tooling or build custom reports:
61+
62+
.. code-block:: php
63+
64+
use FastForward\DevTools\PhpUnit\Coverage\CoverageSummaryLoader;
65+
66+
$loader = new CoverageSummaryLoader();
67+
$summary = $loader->load('public/coverage/coverage.php');
68+
69+
$summary->executedLines(); // e.g., 142
70+
$summary->executableLines(); // e.g., 168
71+
$summary->percentage(); // e.g., 84.52
72+
$summary->percentageAsString(); // e.g., "84.52%"
73+
5574
When to Override Locally
5675
------------------------
5776

src/PhpUnit/Coverage/CoverageSummary.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,40 @@
2424
final readonly class CoverageSummary
2525
{
2626
/**
27-
* @param int $executedLines the number of executable lines that were covered
28-
* @param int $executableLines the total number of executable lines
27+
* Initializes a new instance of the CoverageSummary class.
28+
*
29+
* @param int $executedLines Number of executable lines covered
30+
* @param int $executableLines Total executable lines in the analyzed code
2931
*/
3032
public function __construct(
3133
private int $executedLines,
3234
private int $executableLines,
3335
) {}
3436

3537
/**
36-
* @return int the number of covered executable lines
38+
* Returns the number of executable lines that were executed.
39+
*
40+
* @return int
3741
*/
3842
public function executedLines(): int
3943
{
4044
return $this->executedLines;
4145
}
4246

4347
/**
44-
* @return int the total number of executable lines
48+
* Returns the total number of executable lines.
49+
*
50+
* @return int
4551
*/
4652
public function executableLines(): int
4753
{
4854
return $this->executableLines;
4955
}
5056

5157
/**
52-
* @return float the executed line coverage percentage
58+
* Returns the executed line coverage as a percentage.
59+
*
60+
* @return float
5361
*/
5462
public function percentage(): float
5563
{
@@ -61,7 +69,9 @@ public function percentage(): float
6169
}
6270

6371
/**
64-
* @return string the formatted executed line coverage percentage
72+
* Returns the executed line coverage as a formatted percentage string.
73+
*
74+
* @return string
6575
*/
6676
public function percentageAsString(): string
6777
{

src/PhpUnit/Coverage/CoverageSummaryLoader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
final readonly class CoverageSummaryLoader implements CoverageSummaryLoaderInterface
3030
{
3131
/**
32-
* @param string $coverageReportPath the path to the PHPUnit `coverage-php` report
32+
* @param string $coverageReportPath Path to the PHPUnit `coverage-php` report file
3333
*
34-
* @return CoverageSummary the extracted line coverage summary
34+
* @return CoverageSummary Extracted line coverage summary
35+
*
36+
* @throws RuntimeException When the report file does not exist or contains invalid data
3537
*/
3638
public function load(string $coverageReportPath): CoverageSummary
3739
{

src/PhpUnit/Coverage/CoverageSummaryLoaderInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
interface CoverageSummaryLoaderInterface
2525
{
2626
/**
27-
* @param string $coverageReportPath the path to the PHPUnit `coverage-php` report
27+
* Loads the coverage summary from a PHPUnit `coverage-php` report file.
2828
*
29-
* @return CoverageSummary the extracted coverage summary
29+
* @param string $coverageReportPath Path to the PHPUnit `coverage-php` report file
30+
*
31+
* @return CoverageSummary Extracted coverage summary
32+
*
33+
* @throws RuntimeException When the report file does not exist or contains invalid data
3034
*/
3135
public function load(string $coverageReportPath): CoverageSummary;
3236
}

0 commit comments

Comments
 (0)