Skip to content

Commit

Permalink
Generating PHP Unit coverage reports (#3911)
Browse files Browse the repository at this point in the history
* Add logging for invalid IP issue

* Clean up

* Add test flag

* Add changelog and readme entries

* Remove unintended changes

* Updating PHPUnit version to 9.4.4

* Generating coverage reports

* Updating workflow

* Fix coverage file path

* Workflow update

* Test commit

* Setting env variable

* Setting env variable

* Test commit

* Test commit

* Revert unnecessary changes

* Revert unnecessary changes

* Test commit

* Test commit

* Test commit

* Updating PHP unit version

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Test commit

* Reverting unnecessary changes

* Changelog and readme entries

* Removing unnecessary conditions

* Improving code coverage

* Adding summary line

* Adding headers

* Adding headers

* Fix summary

* Fix header

* Attempt to simplify

* Fix headers

* Including rule for tests without PHP files changed

* Adding title

* Improve coverage script titles

* Improve coverage script titles

* Improve coverage script titles

* Improve coverage script titles

* Removing testing method

* Adding new PHP unit configuration file for the newer versions

* Skipping execution if there are no changed files

* Fix condition

* Reverting unnecessary changes

* Using env var for PHP version in workflow

* Renaming cobertura XML file

* Changing trigger + adding filter + skipping if comment is identical to previous

* Improving PHP filters

* Fix merge issues

---------

Co-authored-by: Anne Mirasol <anne.mirasol@automattic.com>
  • Loading branch information
wjrosa and annemirasol authored Feb 25, 2025
1 parent 70fc5f8 commit cee0da3
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 6 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/php-code-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: PHP code coverage

on:
push:
paths:
- '**.php'

jobs:
test:
runs-on: ubuntu-22.04
name: PHP=8.1, WP=6.6, WC=9.1.2
env:
PHP_VERSION: '8.1'
WP_VERSION: '6.6'
WC_VERSION: '9.1.2'
steps:
- name: Get Changed Files
id: get-changed-files
uses: hanseltimeindustries/get-changed-files@v1
with:
format: 'space-delimited'
filter: '**/*.php'

- name: Checking out
uses: actions/checkout@v2

- name: Set up dependencies caching
uses: actions/cache@v4
with:
path: ~/.cache/composer/
key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHP_VERSION }}
coverage: xdebug

- name: Set up PHPUnit 9.5
run: wget https://phar.phpunit.de/phpunit-9.5.28.phar && mv phpunit-9.5.28.phar phpunit.phar

- name: Run CI checks
run: bash bin/run-ci-tests.bash

- name: Code Coverage Report
uses: irongut/CodeCoverageSummary@v1.3.0
with:
filename: php-coverage.xml
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: '60 80'

- name: Filter Changed Files in Report
run: |
echo "### 📈 PHP Unit Code Coverage Report" > code-coverage-results-filtered.md
echo "Package | Line Rate | Health" >> code-coverage-results-filtered.md
echo "-------- | --------- | ------" >> code-coverage-results-filtered.md
grep -E "$(echo ${{ steps.get-changed-files.outputs.all }} | tr ' ' '|')" code-coverage-results.md >> code-coverage-results-filtered.md
tail -n1 code-coverage-results.md >> code-coverage-results-filtered.md
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
with:
header: code-coverage
recreate: true
skip_unchanged: true
path: code-coverage-results-filtered.md

7 changes: 4 additions & 3 deletions .github/workflows/php-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
coverage: xdebug

- name: If PHP >= 8.0 set up PHPUnit 9.5 for compatibility
if: ${{ matrix.php >= '8.0' }}
run: wget https://phar.phpunit.de/phpunit-9.5.13.phar && mv phpunit-9.5.13.phar phpunit.phar

run: wget https://phar.phpunit.de/phpunit-9.5.28.phar && mv phpunit-9.5.28.phar phpunit.phar
- name: Run CI checks
run: bash bin/run-ci-tests.bash

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ docker/logs
# PHPUnit cache files
.phpunit.cache/
.phpunit.result.cache

# PHPUnit coverage results files
php-coverage.xml
phpunit-html/
2 changes: 1 addition & 1 deletion bin/phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if [[ $RUN_PHPCS == 1 || $SHOULD_DEPLOY == 1 ]]; then
exit
fi

if [ -f "phpunit.phar" ]; then php phpunit.phar -c phpunit.xml.dist; else ./vendor/bin/phpunit; fi;
if [ -f "phpunit.phar" ]; then php phpunit.phar -c phpunit-9.xml.dist; else ./vendor/bin/phpunit; fi;
2 changes: 1 addition & 1 deletion bin/run-ci-tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ echo 'Setting up test environment...'
bash bin/install-wp-tests.sh woocommerce_test root root localhost $WP_VERSION $WC_VERSION false

echo 'Running the tests...'
bash bin/phpunit.sh
bash bin/phpunit.sh
2 changes: 1 addition & 1 deletion bin/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ docker compose exec -u www-data wordpress \

echo "Running the tests..."

docker compose exec -u www-data wordpress \
docker compose exec -u www-data -e XDEBUG_MODE=coverage wordpress \
/var/www/html/wp-content/plugins/woocommerce-gateway-stripe/vendor/bin/phpunit \
--configuration /var/www/html/wp-content/plugins/woocommerce-gateway-stripe/phpunit.xml.dist \
$*
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*** Changelog ***

= 9.3.0 - xxxx-xx-xx =
* Dev - Generates a code coverage report for PHP Unit tests as a comment on PRs.
* Add - Adds Stripe specific information to the System Status Report data.
* Fix - Fixes a fatal error that might happen during extension install due to missing Amazon Pay default settings data, when registering the settings route.
* Dev - Adds the payment method constants to the payment methods map file (frontend side).
Expand Down
26 changes: 26 additions & 0 deletions phpunit-9.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="false"
>
<testsuites>
<testsuite name="WCStripe">
<directory prefix="test-" suffix=".php">./tests/phpunit</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage">
<include>
<file>./woocommerce-gateway-stripe.php</file>
<directory>./includes</directory>
</include>
<report>
<cobertura outputFile="php-coverage.xml"/>
<html outputDirectory="phpunit-html"/>
</report>
</coverage>
</phpunit>
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
== Changelog ==

= 9.3.0 - xxxx-xx-xx =
* Dev - Generates a code coverage report for PHP Unit tests as a comment on PRs.
* Add - Adds Stripe specific information to the System Status Report data.
* Fix - Fixes a fatal error that might happen during extension install due to missing Amazon Pay default settings data, when registering the settings route.
* Dev - Adds the payment method constants to the payment methods map file (frontend side).
Expand Down

0 comments on commit cee0da3

Please sign in to comment.