Skip to content

Commit

Permalink
RPP / Add 100% unit test coverage requirement for src (#7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoslavGeorgiev authored Oct 27, 2023
1 parent 9df23da commit a08754d
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 23 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ jobs:
woocommerce: [ 'latest' ]
wordpress: [ 'latest' ]
php: [ '7.4' ]
directory: [ 'includes', 'src' ]
env:
WP_VERSION: ${{ matrix.wordpress }}
WC_VERSION: ${{ matrix.woocommerce }}
WP_VERSION: ${{ matrix.wordpress }}
WC_VERSION: ${{ matrix.woocommerce }}
COVERAGE_DIR: ${{ matrix.directory }}
steps:
# clone the repository
- uses: actions/checkout@v3
Expand Down
16 changes: 13 additions & 3 deletions bin/check-test-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

set -e

if [ "$1" == "src" ]; then
CONFIGURATION_FILE=phpunit-src.xml.dist
COVERAGE=100
else
CONFIGURATION_FILE=phpunit-includes.xml.dist
COVERAGE=60
fi

echo "Installing the test environment..."

docker-compose exec -u www-data wordpress \
Expand All @@ -12,6 +20,8 @@ echo "Checking coverage..."
docker-compose exec -u www-data wordpress \
php -d xdebug.remote_autostart=on \
/var/www/html/wp-content/plugins/woocommerce-payments/vendor/bin/phpunit \
--configuration /var/www/html/wp-content/plugins/woocommerce-payments/phpunit.xml.dist \
--coverage-html /var/www/html/php-test-coverage
$*
--configuration "/var/www/html/wp-content/plugins/woocommerce-payments/$CONFIGURATION_FILE" \
--coverage-html /var/www/html/php-test-coverage \
--coverage-clover /var/www/html/clover.xml

./vendor/bin/coverage-check docker/wordpress/clover.xml $COVERAGE
6 changes: 3 additions & 3 deletions bin/phpunit.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ SUPPORTED_PHP_MAJOR_VERSION_FOR_PHPUNIT_INSTALLED_VIA_COMPOSER_JSON=7
SUPPORTED_PHP_MINOR_VERSION_FOR_PHPUNIT_INSTALLED_VIA_COMPOSER_JSON=3

if [ $CURRENT_PHP_MAJOR_VERSION -gt $SUPPORTED_PHP_MAJOR_VERSION_FOR_PHPUNIT_INSTALLED_VIA_COMPOSER_JSON ]; then
./vendor/bin/phpunit -c phpunit.xml.dist "$@";
./vendor/bin/phpunit "$@";
else
if [ $CURRENT_PHP_MINOR_VERSION -ge $SUPPORTED_PHP_MINOR_VERSION_FOR_PHPUNIT_INSTALLED_VIA_COMPOSER_JSON ]; then
./vendor/bin/phpunit -c phpunit.xml.dist "$@";
./vendor/bin/phpunit "$@";
else
chmod +x ./bin/phpunit6
./bin/phpunit6 -c phpunit.xml.dist "$@";
./bin/phpunit6 "$@";
fi
fi

13 changes: 11 additions & 2 deletions bin/run-ci-tests-check-coverage.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ IFS=$'\n\t'
# set environment variables
WCPAY_DIR="$GITHUB_WORKSPACE"

# determine whether to test everything, or just src, and what coverage to require
if [ "$COVERAGE_DIR" == "src" ]; then
CONFIGURATION_FILE=phpunit-src.xml.dist
COVERAGE=100
else
CONFIGURATION_FILE=phpunit-includes.xml.dist
COVERAGE=60
fi

composer self-update && composer install --no-progress
sudo systemctl start mysql.service
bash bin/install-wp-tests.sh woocommerce_test root root localhost $WP_VERSION $WC_VERSION false
echo 'Running the tests...'
bash bin/phpunit.sh --coverage-clover /tmp/clover.xml
vendor/bin/coverage-check /tmp/clover.xml 60
bash bin/phpunit.sh -c $CONFIGURATION_FILE --coverage-clover /tmp/clover.xml
vendor/bin/coverage-check /tmp/clover.xml $COVERAGE

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 $GUTENBERG_VERSION

echo 'Running the tests...'
bash bin/phpunit.sh
bash bin/phpunit.sh -c phpunit.xml.dist
5 changes: 5 additions & 0 deletions changelog/patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Adding 100% src coverage helpers.


1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"test:update-snapshots": "npm run test:js -- --updateSnapshot",
"test:php": "./bin/run-tests.sh",
"test:php-coverage": "./bin/check-test-coverage.sh",
"test:php-coverage-src": "./bin/check-test-coverage.sh src",
"test:php-watch": "npm run test:php -- -w",
"test:qit": "npm run build:release && ./tests/qit/security.sh",
"watch": "webpack --watch",
Expand Down
30 changes: 30 additions & 0 deletions phpunit-includes.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!-- Configuration for coverage checks for `includes` -->
<phpunit
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="WCPay">
<directory suffix=".php">./tests/unit</directory>
<exclude>./tests/unit/multi-currency</exclude>
<!-- This configuration is only for `includes` -->
<exclude>./tests/unit/src</exclude>
</testsuite>
<testsuite name="WCPayMultiCurrency">
<directory suffix=".php">./tests/unit/helpers</directory>
<directory suffix=".php">./tests/unit/multi-currency</directory>
</testsuite>
</testsuites>

<!-- Set a whitelist for code coverage analysis -->
<filter>
<whitelist>
<directory suffix=".php">includes</directory>
</whitelist>
</filter>
</phpunit>
28 changes: 28 additions & 0 deletions phpunit-src.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!-- Configuration for coverage checks for `src` -->
<phpunit
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="WCPaySrc">
<directory suffix=".php">./tests/unit/helpers</directory>
<directory suffix=".php">./tests/unit/src</directory>
</testsuite>
</testsuites>

<!-- Set a whitelist for code coverage analysis -->
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<!-- Service providers are simple, and ideally should not be used within tests. -->
<directory suffix=".php">src/Internal/DependencyManagement/ServiceProvider</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
14 changes: 2 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0"?>
<!-- Configuration for tests for both `includes` and `src`, but without checking code coverage. -->
<phpunit
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
Expand All @@ -18,16 +19,5 @@
</testsuite>
</testsuites>

<!-- Set a whitelist for code coverage analysis -->
<filter>
<whitelist>
<directory suffix=".php">includes</directory>
<directory suffix=".php">src</directory>
<exclude>
<!-- Service providers are simple, and ideally should not be used within tests. -->
<directory suffix=".php">src/Internal/DependencyManagement/ServiceProvider</directory>
</exclude>
</whitelist>
</filter>

<!-- No code coverage settings here, there are separate coverages for `includes` and `src`. -->
</phpunit>
4 changes: 4 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use WCPay\Internal\DependencyManagement\ServiceProvider\GenericServiceProvider;
use WCPay\Internal\DependencyManagement\ServiceProvider\ProxiesServiceProvider;

// @codeCoverageIgnoreStart

/**
* Hides errors during update from 6.6.0 or 6.6.1 to 6.6.2.
*
Expand All @@ -36,6 +38,8 @@
wp_die();
}

// @codeCoverageIgnoreEnd

/**
* WCPay Dependency Injection Container.
*
Expand Down

0 comments on commit a08754d

Please sign in to comment.