From a08754d1caf59079a87dc34cd139f399bae57297 Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Fri, 27 Oct 2023 11:57:55 +0300 Subject: [PATCH] RPP / Add 100% unit test coverage requirement for src (#7510) --- .github/workflows/coverage.yml | 6 ++++-- bin/check-test-coverage.sh | 16 ++++++++++++--- bin/phpunit.sh | 6 +++--- bin/run-ci-tests-check-coverage.bash | 13 ++++++++++-- bin/run-ci-tests.bash | 2 +- changelog/patch | 5 +++++ package.json | 1 + phpunit-includes.xml.dist | 30 ++++++++++++++++++++++++++++ phpunit-src.xml.dist | 28 ++++++++++++++++++++++++++ phpunit.xml.dist | 14 ++----------- src/Container.php | 4 ++++ 11 files changed, 102 insertions(+), 23 deletions(-) mode change 100644 => 100755 bin/phpunit.sh create mode 100644 changelog/patch create mode 100644 phpunit-includes.xml.dist create mode 100644 phpunit-src.xml.dist diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index b1401136de9..e3f3dac0043 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -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 diff --git a/bin/check-test-coverage.sh b/bin/check-test-coverage.sh index e4bf6651134..58a370c98b9 100755 --- a/bin/check-test-coverage.sh +++ b/bin/check-test-coverage.sh @@ -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 \ @@ -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 diff --git a/bin/phpunit.sh b/bin/phpunit.sh old mode 100644 new mode 100755 index f7d7c10ad38..c25459aacc6 --- a/bin/phpunit.sh +++ b/bin/phpunit.sh @@ -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 diff --git a/bin/run-ci-tests-check-coverage.bash b/bin/run-ci-tests-check-coverage.bash index 7c2d5d33c24..c14ce1aa913 100644 --- a/bin/run-ci-tests-check-coverage.bash +++ b/bin/run-ci-tests-check-coverage.bash @@ -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 diff --git a/bin/run-ci-tests.bash b/bin/run-ci-tests.bash index e7a9a03e340..8102c8202ad 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 $GUTENBERG_VERSION echo 'Running the tests...' -bash bin/phpunit.sh +bash bin/phpunit.sh -c phpunit.xml.dist diff --git a/changelog/patch b/changelog/patch new file mode 100644 index 00000000000..1be87335e20 --- /dev/null +++ b/changelog/patch @@ -0,0 +1,5 @@ +Significance: patch +Type: dev +Comment: Adding 100% src coverage helpers. + + diff --git a/package.json b/package.json index f2d4c8497af..2f90d762764 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/phpunit-includes.xml.dist b/phpunit-includes.xml.dist new file mode 100644 index 00000000000..c77770a39e4 --- /dev/null +++ b/phpunit-includes.xml.dist @@ -0,0 +1,30 @@ + + + + + + ./tests/unit + ./tests/unit/multi-currency + + ./tests/unit/src + + + ./tests/unit/helpers + ./tests/unit/multi-currency + + + + + + + includes + + + diff --git a/phpunit-src.xml.dist b/phpunit-src.xml.dist new file mode 100644 index 00000000000..5ca1a0c5c1c --- /dev/null +++ b/phpunit-src.xml.dist @@ -0,0 +1,28 @@ + + + + + + ./tests/unit/helpers + ./tests/unit/src + + + + + + + src + + + src/Internal/DependencyManagement/ServiceProvider + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cf5eb5bb5de..00e9a2abd3b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,4 +1,5 @@ + - - - - includes - src - - - src/Internal/DependencyManagement/ServiceProvider - - - - + diff --git a/src/Container.php b/src/Container.php index ff49bc510ca..3465d9f4d8c 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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. * @@ -36,6 +38,8 @@ wp_die(); } +// @codeCoverageIgnoreEnd + /** * WCPay Dependency Injection Container. *