Skip to content

Commit cce5e37

Browse files
Display code coverage
1 parent c7765c0 commit cce5e37

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.gitattributes

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
/.gitignore export-ignore
2121
/composer.json export-ignore
2222
/composer.lock export-ignore
23-
/coverage.xml export-ignore
2423
/eslint.config.cjs export-ignore
2524
/package.json export-ignore
2625
/package-lock.json export-ignore
2726
/phpunit.xml export-ignore
27+
/phpunit_coverage.xml export-ignore
28+
/phpunit_report.xml export-ignore
2829
/webpack.mix.js

.github/workflows/tests.yml

+31-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name: Run tests
33
on:
44
- push
55

6+
permissions:
7+
checks: write
8+
pull-requests: write # necessary to publish results in PR
9+
610
jobs:
711
tests:
812
runs-on: ubuntu-latest
@@ -41,18 +45,42 @@ jobs:
4145
- name: Build
4246
run: npm run production
4347

44-
- name: Run tests (without coverage)
48+
- name: Run tests (PHP <= 8.3)
4549
if: matrix.php-version != '8.4'
4650
run: composer test
4751

4852
- name: Run tests with coverage (PHP 8.4)
4953
if: matrix.php-version == '8.4'
50-
run: composer test -- --coverage-clover=coverage.xml
54+
run: composer test -- --coverage-clover=phpunit_coverage.xml --log-junit=phpunit_report.xml
5155

5256
- name: Upload coverage report to GitHub Artifacts
57+
id: artifact-upload-step
5358
if: matrix.php-version == '8.4'
5459
uses: actions/upload-artifact@v4
5560
with:
5661
name: Code coverage report (PHP ${{ matrix.php-version }})
57-
path: coverage.xml
62+
path: |
63+
phpunit_coverage.xml
64+
phpunit_report.xml
5865
if-no-files-found: warn
66+
67+
- name: Calculate coverage percentage
68+
if: matrix.php-version == '8.4'
69+
run: |
70+
COVERAGE=$(php -r "
71+
\$xml = simplexml_load_file('phpunit_coverage.xml');
72+
\$metrics = \$xml->project->metrics;
73+
\$covered = (int) \$metrics['coveredelements'];
74+
\$total = (int) \$metrics['elements'];
75+
echo \$total > 0 ? round((\$covered / \$total) * 100, 2) : 0;
76+
")
77+
echo "COVERAGE=$COVERAGE"
78+
echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV
79+
80+
- name: Comment test results in PR
81+
if: matrix.php-version == '8.4'
82+
uses: EnricoMi/publish-unit-test-result-action@v2
83+
with:
84+
files: phpunit_report.xml
85+
check_run: false # disabled because of https://github.com/EnricoMi/publish-unit-test-result-action/issues/12
86+
comment_title: "Test results (coverage: ${{ env.COVERAGE }}%)"

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
/vendor
1414
/.env
1515
/.env.backup
16-
/coverage.xml
16+
/phpunit_coverage.xml
17+
/phpunit_report.xml

0 commit comments

Comments
 (0)