diff --git a/.github/workflows/php-code-coverage.yml b/.github/workflows/php-code-coverage.yml new file mode 100644 index 000000000..2c75dda47 --- /dev/null +++ b/.github/workflows/php-code-coverage.yml @@ -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 + diff --git a/.github/workflows/php-tests.yml b/.github/workflows/php-tests.yml index 35eea3e50..b2bc0ad5a 100644 --- a/.github/workflows/php-tests.yml +++ b/.github/workflows/php-tests.yml @@ -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 + \ No newline at end of file diff --git a/.gitignore b/.gitignore index bde1b18cc..d30cdf5c3 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,7 @@ docker/logs # PHPUnit cache files .phpunit.cache/ .phpunit.result.cache + +# PHPUnit coverage results files +php-coverage.xml +phpunit-html/ diff --git a/bin/phpunit.sh b/bin/phpunit.sh index 5c2df55ae..faf7b014f 100644 --- a/bin/phpunit.sh +++ b/bin/phpunit.sh @@ -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; diff --git a/bin/run-ci-tests.bash b/bin/run-ci-tests.bash index 1ceff3ac6..029e539cd 100644 --- a/bin/run-ci-tests.bash +++ b/bin/run-ci-tests.bash @@ -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 \ No newline at end of file +bash bin/phpunit.sh diff --git a/bin/run-tests.sh b/bin/run-tests.sh index e705a266a..f4a060f08 100755 --- a/bin/run-tests.sh +++ b/bin/run-tests.sh @@ -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 \ $* diff --git a/changelog.txt b/changelog.txt index bd733f383..275bf7ab5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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). diff --git a/phpunit-9.xml.dist b/phpunit-9.xml.dist new file mode 100644 index 000000000..d9cade792 --- /dev/null +++ b/phpunit-9.xml.dist @@ -0,0 +1,26 @@ + + + + + ./tests/phpunit + + + + + ./woocommerce-gateway-stripe.php + ./includes + + + + + + + diff --git a/readme.txt b/readme.txt index e5744e7e6..7178ae922 100644 --- a/readme.txt +++ b/readme.txt @@ -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).