Skip to content

Added PHPUnit code coverage reporting to CI with threshold enforcement.#330

Merged
AlexSkrypnyk merged 5 commits intomasterfrom
feature/ci-coverage
Apr 17, 2026
Merged

Added PHPUnit code coverage reporting to CI with threshold enforcement.#330
AlexSkrypnyk merged 5 commits intomasterfrom
feature/ci-coverage

Conversation

@AlexSkrypnyk
Copy link
Copy Markdown
Collaborator

@AlexSkrypnyk AlexSkrypnyk commented Apr 17, 2026

Summary

  • Adds PHPUnit code coverage reporting to CI, running only on the PHP 8.4 / Drupal 11 matrix entry using pcov.
  • Generates cobertura.xml, an HTML report, and a text summary; all artifacts are uploaded for inspection.
  • Posts a sticky PR comment (via marocchino/sticky-pull-request-comment) containing the coverage summary and a collapsible per-class breakdown parsed from the Cobertura XML.
  • Fails the build if total line coverage drops below the configured threshold (default: 10%).

Changes

.github/workflows/ci.yml

  • Added a top-level CI_COVERAGE_THRESHOLD env var (default 10).
  • Added pull-requests: write permission so the workflow can post PR comments.
  • Added a coverage: true flag to the PHP 8.4 / Drupal 11 matrix entry.
  • Split the existing Test step into two conditional steps: one that runs with pcov coverage instrumentation (if: matrix.coverage) and one that runs the standard composer test command for all other matrix entries.
  • Added an Extract total coverage step that reads cobertura.xml, calculates the percentage, writes it to $GITHUB_STEP_SUMMARY, and exports per-class details via env vars.
  • Added a Post coverage summary as PR comment step that posts (and updates) a sticky comment on pull requests.
  • Added a Check coverage threshold step that exits non-zero when coverage is below the threshold.
  • Added an Upload test artifacts step that uploads the .logs directory as a workflow artifact.

phpunit.xml.dist

  • Updated to use the <source> element (replacing the deprecated <coverage><include> form).
  • Configured coverage reports: HTML (with low/high bounds), Cobertura XML, and a plain-text summary.
  • Added JUnit XML logging output.
  • Set cacheDirectory, executionOrder, failOnWarning, and beStrictAboutCoverageMetadata for improved test hygiene.

.gitignore

  • Added /.logs to exclude generated coverage and log output from version control.

Test plan

  • Verify the Test with coverage step runs only on the PHP 8.4 / Drupal 11 matrix entry.
  • Confirm a coverage-report artifact is uploaded after a successful run.
  • Open a pull request against master and confirm a sticky comment is posted with the coverage summary and collapsible per-class details.
  • Temporarily lower CI_COVERAGE_THRESHOLD to a value above the actual coverage and confirm the build fails with the expected message.
  • Confirm all other matrix entries still run composer test without coverage overhead.

Summary by CodeRabbit

Release Notes

  • Chores
    • Enabled code coverage tracking with automated PR comments
    • Configured coverage thresholds for code quality enforcement
    • Enhanced PHPUnit test configuration and reporting capabilities

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 17, 2026

Warning

Rate limit exceeded

@AlexSkrypnyk has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 27 minutes and 31 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 27 minutes and 31 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3d39ef78-b6f5-443e-8190-34e561ca7d12

📥 Commits

Reviewing files that changed from the base of the PR and between 34770c8 and e4f4618.

📒 Files selected for processing (2)
  • .github/workflows/ci.yml
  • composer.json
📝 Walkthrough

Walkthrough

The PR introduces comprehensive code coverage instrumentation to the CI pipeline. It configures PHPUnit to generate coverage reports with minimum thresholds, modifies the workflow to extract metrics and post coverage summaries as PR comments, and ensures builds fail below coverage requirements.

Changes

Cohort / File(s) Summary
CI Coverage Instrumentation
.github/workflows/ci.yml
Adds global CI_COVERAGE_THRESHOLD environment variable, grants pull-requests: write permissions, introduces matrix coverage flag for a specific PHP/Drupal combination, runs PHPUnit with pcov coverage enabled and cobertura XML output, extracts coverage metrics from generated XML, calculates percentage-based coverage, posts sticky PR comments with total and per-class coverage details, fails workflow when coverage is below threshold, and uploads logs as coverage-report artifacts.
PHPUnit Coverage Configuration
phpunit.xml.dist
Extends PHPUnit configuration with cache directory, test execution ordering, failure-on-warning mode, and relaxed coverage metadata strictness. Restructures coverage section to include HTML, Cobertura XML, and text report outputs to .logs/coverage/ with threshold bounds, and adds JUnit logging output to .logs/phpunit/junit.xml.
Version Control
.gitignore
Adds /.logs directory to ignore list for version control.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 Coverage hops through workflows with glee,
Each metric now posted for all eyes to see,
Thresholds are guarding the code's quality,
The CI now speaks with precision so free! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately summarizes the main change: adding PHPUnit code coverage reporting to CI with threshold enforcement, which is reflected across all three modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/ci-coverage

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.github/workflows/ci.yml:
- Around line 127-134: The "Upload test artifacts" step currently is skipped
when the coverage threshold check exits non-zero; update the step's conditional
to run regardless of job outcome by changing its if expression to use always()
(e.g., if: ${{ always() && matrix.coverage }} or equivalent) so artifacts are
uploaded even on failures; apply the same change to the "Post coverage summary
as PR comment" step (referencing that step name) so the PR is updated when the
threshold check fails.
- Around line 119-125: The threshold check step ("Check coverage threshold")
fails for non-integer CI_COVERAGE_THRESHOLD because the bash arithmetic
$((CI_COVERAGE_THRESHOLD*100)) rejects floats; change the comparison to a
float-safe one by using a tool that supports decimals (e.g., awk or bc or
python) to multiply/compare TOTAL_COVERAGE_PERCENT and CI_COVERAGE_THRESHOLD (or
normalize both to the same scale) and return non-zero/zero for the exit status;
update the step to use that float-safe comparison so TOTAL_COVERAGE_PERCENT (a
%.2f float) is correctly compared against CI_COVERAGE_THRESHOLD (which may be a
decimal).
- Around line 82-98: The script that extracts coverage is brittle: ensure
.logs/coverage/cobertura.xml exists and contains a valid line-rate before
computing RATE/PERCENT (fail early with a clear error message if missing/empty),
replace the grep/tr/tr brittle pipeline with a small Python parse (reuse the
existing python3 block to compute RATE→PERCENT and export
TOTAL_COVERAGE_PERCENT), add safety checks before cat-ing
.logs/coverage/coverage-summary.txt (e.g., test -f before appending
COVERAGE_SUMMARY to GITHUB_ENV) and wrap the shell block with strict options
(set -euo pipefail) or explicit guards so empty RATE won't produce a broken awk
invocation; also only write COVERAGE_DETAILS/COVERAGE_SUMMARY env blocks when
their contents are present.
- Around line 100-117: The workflow step using
marocchino/sticky-pull-request-comment will fail for forked-repo pull_request
events because GITHUB_TOKEN is read-only for forks; update the step to either
(A) move this comment-posting logic into a separate pull_request_target workflow
(and handle untrusted code safely) so it can run with write permissions, or (B)
make the step non-blocking by adding continue-on-error: true so fork PRs don't
fail CI; also remove hide_and_recreate: true (or change to the default
header/recreate behavior) to avoid accumulating hidden comments—refer to the
action usage block and the hide_and_recreate/header fields when making these
changes.
- Around line 74-76: The workflow step "Test with coverage" sets
pcov.directory=., which instruments the entire repo (including vendor/) — change
the pcov.directory value to the actual source directory used by PHPUnit (the
same path configured in phpunit.xml.dist, e.g., "src" or the <source> path) so
pcov only instruments your source tree; update the run command in the "Test with
coverage" step (the docker compose exec -T php ... php -d pcov.directory=...
vendor/bin/phpunit ...) to use that specific directory instead of "." to avoid
instrumenting vendor/.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7317d9d1-2211-4a55-a1d0-f0f0cc88fdfd

📥 Commits

Reviewing files that changed from the base of the PR and between 9434519 and 34770c8.

📒 Files selected for processing (3)
  • .github/workflows/ci.yml
  • .gitignore
  • phpunit.xml.dist

Comment thread .github/workflows/ci.yml Outdated
Comment thread .github/workflows/ci.yml
Comment thread .github/workflows/ci.yml Outdated
Comment thread .github/workflows/ci.yml
Comment thread .github/workflows/ci.yml
@github-actions
Copy link
Copy Markdown

Code coverage (threshold: 10%)



Code Coverage Report Summary:
  Classes: 19.44% (7/36)
  Methods:  5.93% (16/270)
  Lines:   10.34% (129/1248)

Per-class coverage
Drupal\Driver\BaseDriver                                       0.00%
Drupal\Driver\BlackboxDriver                                   0.00%
Drupal\Driver\Cores\AbstractCore                              23.81%
Drupal\Driver\Cores\Drupal6                                    0.00%
Drupal\Driver\Cores\Drupal7                                    0.00%
Drupal\Driver\Cores\Drupal8                                    7.30%
Drupal\Driver\DrupalDriver                                     0.00%
Drupal\Driver\DrushDriver                                     23.24%
Drupal\Driver\Exception\BootstrapException                   100.00%
Drupal\Driver\Exception\Exception                             66.67%
Drupal\Driver\Exception\UnsupportedDriverActionException       0.00%
Drupal\Driver\Fields\Drupal6\TaxonomyHandler                   0.00%
Drupal\Driver\Fields\Drupal7\AbstractHandler                  41.67%
Drupal\Driver\Fields\Drupal7\DatetimeHandler                 100.00%
Drupal\Driver\Fields\Drupal7\DefaultHandler                  100.00%
Drupal\Driver\Fields\Drupal7\EntityreferenceHandler            0.00%
Drupal\Driver\Fields\Drupal7\FileHandler                       0.00%
Drupal\Driver\Fields\Drupal7\ImageHandler                      0.00%
Drupal\Driver\Fields\Drupal7\LinkFieldHandler                  0.00%
Drupal\Driver\Fields\Drupal7\ListBooleanHandler              100.00%
Drupal\Driver\Fields\Drupal7\ListTextHandler                   0.00%
Drupal\Driver\Fields\Drupal7\TaxonomyTermReferenceHandler      0.00%
Drupal\Driver\Fields\Drupal8\AbstractHandler                   0.00%
Drupal\Driver\Fields\Drupal8\AddressHandler                    0.00%
Drupal\Driver\Fields\Drupal8\DaterangeHandler                  0.00%
Drupal\Driver\Fields\Drupal8\DatetimeHandler                   0.00%
Drupal\Driver\Fields\Drupal8\DefaultHandler                    0.00%
Drupal\Driver\Fields\Drupal8\EmbridgeAssetItemHandler          0.00%
Drupal\Driver\Fields\Drupal8\EntityReferenceHandler            0.00%
Drupal\Driver\Fields\Drupal8\FileHandler                       0.00%
Drupal\Driver\Fields\Drupal8\ImageHandler                      0.00%
Drupal\Driver\Fields\Drupal8\LinkHandler                     100.00%
Drupal\Driver\Fields\Drupal8\ListFloatHandler                  0.00%
Drupal\Driver\Fields\Drupal8\ListHandlerBase                   0.00%
Drupal\Driver\Fields\Drupal8\ListIntegerHandler                0.00%
Drupal\Driver\Fields\Drupal8\ListStringHandler                 0.00%
Drupal\Driver\Fields\Drupal8\NameHandler                     100.00%
Drupal\Driver\Fields\Drupal8\OgStandardReferenceHandler        0.00%
Drupal\Driver\Fields\Drupal8\SupportedImageHandler             0.00%
Drupal\Driver\Fields\Drupal8\TaxonomyTermReferenceHandler      0.00%
Drupal\Driver\Fields\Drupal8\TextWithSummaryHandler            0.00%
Drupal\Driver\Fields\Drupal8\TimeHandler                     100.00%

@AlexSkrypnyk AlexSkrypnyk merged commit 1ca3702 into master Apr 17, 2026
6 checks passed
@AlexSkrypnyk AlexSkrypnyk added this to the 2.5 milestone Apr 17, 2026
@AlexSkrypnyk AlexSkrypnyk deleted the feature/ci-coverage branch April 17, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant