From cde8ba3af128db2daf061c9a20ac4e0df3e9666c Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Tue, 26 Apr 2022 16:15:06 +0200 Subject: [PATCH] Update PHPUnit setup to remove WP < 5.9 oddities See #21175, #21157 Primary issue: #24082 --- docs/examples/bootstrap.php | 35 +--------- .../plugins/jetpack/tests/action-phpunit.sh | 1 - .../jetpack/tests/action-test-coverage.sh | 2 - .../plugins/jetpack/tests/action-test-php.sh | 2 - .../jetpack/tests/maybe-downgrade-phpunit.sh | 24 ------- .../plugins/jetpack/tests/php/bootstrap.php | 69 +++++++++++-------- .../jetpack/tests/php/find-test-root.php | 48 ------------- .../jetpack/tests/php/test_deprecation.php | 4 -- 8 files changed, 41 insertions(+), 144 deletions(-) delete mode 100644 projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh delete mode 100644 projects/plugins/jetpack/tests/php/find-test-root.php diff --git a/docs/examples/bootstrap.php b/docs/examples/bootstrap.php index ac0abb95220a1..28d190a4e2a7c 100644 --- a/docs/examples/bootstrap.php +++ b/docs/examples/bootstrap.php @@ -1,12 +1,6 @@ =' ) && - ( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) ) -) { - if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) && - file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" ) - ) { - // phpcs:disable WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath - require "$_tests_dir/includes/phpunit7/MockObject/Builder/NamespaceMatch.php"; - require "$_tests_dir/includes/phpunit7/MockObject/Builder/ParametersMatch.php"; - require "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php"; - require "$_tests_dir/includes/phpunit7/MockObject/MockMethod.php"; - // phpcs:enable - } else { - fprintf( - STDOUT, - "Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n Class %s exists: %s\n File %s exists: %s\n", - PHPUnit\Framework\MockObject\InvocationMocker::class, - class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)', - "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php", - file_exists( "$_tests_dir/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)' - ); - } -} - // Give access to tests_add_filter() function. require_once $_tests_dir . '/includes/functions.php'; diff --git a/projects/plugins/jetpack/tests/action-phpunit.sh b/projects/plugins/jetpack/tests/action-phpunit.sh index cc4ba9853a081..a349522c8b5d0 100755 --- a/projects/plugins/jetpack/tests/action-phpunit.sh +++ b/projects/plugins/jetpack/tests/action-phpunit.sh @@ -2,5 +2,4 @@ set -eo pipefail -. tests/maybe-downgrade-phpunit.sh exec phpunit "$@" diff --git a/projects/plugins/jetpack/tests/action-test-coverage.sh b/projects/plugins/jetpack/tests/action-test-coverage.sh index 57116de3d8d54..4c272ee362a6a 100755 --- a/projects/plugins/jetpack/tests/action-test-coverage.sh +++ b/projects/plugins/jetpack/tests/action-test-coverage.sh @@ -2,8 +2,6 @@ set -eo pipefail -. tests/maybe-downgrade-phpunit.sh - PLUGINDIR="$PWD" cd "$MONOREPO_BASE/projects/plugins/jetpack" diff --git a/projects/plugins/jetpack/tests/action-test-php.sh b/projects/plugins/jetpack/tests/action-test-php.sh index 33789244ce591..bc3ee23ff869f 100755 --- a/projects/plugins/jetpack/tests/action-test-php.sh +++ b/projects/plugins/jetpack/tests/action-test-php.sh @@ -2,8 +2,6 @@ set -eo pipefail -. tests/maybe-downgrade-phpunit.sh - echo "::group::Jetpack tests" phpunit echo "::endgroup::" diff --git a/projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh b/projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh deleted file mode 100644 index e51a8d397b2f4..0000000000000 --- a/projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -# Detect versions of WordPress that still require an ancient version of PHPUnit, and -# if necessary downgrade to a version that will work. -# @todo: Remove this once we drop support for WordPress 5.8. Also probably merge find-test-root.php back into bootstrap.php. -TEST_ROOT=$(php -r 'echo require "tests/php/find-test-root.php";') -if grep -q --fixed $'version_compare( $phpunit_version, \'8.0\', \'>=\' )' "$TEST_ROOT/includes/bootstrap.php" && - ! phpunit --version | grep -q '^PHPUnit [567]\.' -then - echo "The version of WordPress under test requires PHPUnit < 8.0, while the installed version is" - phpunit --version - - TMP1="$( composer.json - echo "$TMP2" > composer.lock - - printf 'Now using ' - phpunit --version -else - printf 'Using ' - phpunit --version -fi diff --git a/projects/plugins/jetpack/tests/php/bootstrap.php b/projects/plugins/jetpack/tests/php/bootstrap.php index 2f3b87ee8d03d..319cf416bfc8d 100644 --- a/projects/plugins/jetpack/tests/php/bootstrap.php +++ b/projects/plugins/jetpack/tests/php/bootstrap.php @@ -17,7 +17,46 @@ */ define( 'TESTING_IN_JETPACK', true ); -$test_root = require __DIR__ . '/find-test-root.php'; +// Support for: +// 1. `WP_DEVELOP_DIR` environment variable. +// 2. Plugin installed inside of WordPress.org developer checkout. +// 3. Tests checked out to /tmp. +if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) { + // Defined on command line. + $test_root = getenv( 'WP_DEVELOP_DIR' ); + if ( file_exists( "$test_root/tests/phpunit/" ) ) { + $test_root .= '/tests/phpunit/'; + } +} elseif ( file_exists( '../../../../tests/phpunit/includes/bootstrap.php' ) ) { + // Installed inside wordpress-develop. + $test_root = '../../../../tests/phpunit'; +} elseif ( file_exists( '/vagrant/www/wordpress-develop/public_html/tests/phpunit/includes/bootstrap.php' ) ) { + // VVV. + $test_root = '/vagrant/www/wordpress-develop/public_html/tests/phpunit'; +} elseif ( file_exists( '/srv/www/wordpress-trunk/public_html/tests/phpunit/includes/bootstrap.php' ) ) { + // VVV 3.0. + $test_root = '/srv/www/wordpress-trunk/public_html/tests/phpunit'; +} elseif ( file_exists( '/tmp/wordpress-develop/tests/phpunit/includes/bootstrap.php' ) ) { + // Manual checkout & Jetpack's docker environment. + $test_root = '/tmp/wordpress-develop/tests/phpunit'; +} elseif ( file_exists( '/tmp/wordpress-tests-lib/includes/bootstrap.php' ) ) { + // Legacy tests. + $test_root = '/tmp/wordpress-tests-lib'; +} + +if ( ! isset( $test_root ) || ! file_exists( $test_root . '/includes/bootstrap.php' ) ) { + fprintf( + STDERR, + <<<'EOF' +Failed to automatically locate WordPress or wordpress-develop to run tests. + +Set the WP_DEVELOP_DIR environment variable to point to a copy of WordPress +or wordpress-develop. +EOF + ); + exit( 1 ); +} + echo "Using test root $test_root\n"; $jp_autoloader = __DIR__ . '/../../vendor/autoload.php'; @@ -32,34 +71,6 @@ require $jp_autoloader; -// WordPress until recently required PHPUnit 7.5 or earlier and hacks around a few things to -// make it work with PHP 8. Unfortunately for MockObjects they do it via -// composer.json rather than bootstrap.php, so we have to manually do it here. -// @todo: Remove this once either WP backports their bootstrap changes to 5.8.1 or they release 5.8.2. -if ( version_compare( PHP_VERSION, '8.0', '>=' ) && - ( ! class_exists( PHPUnit\Runner\Version::class ) || version_compare( PHPUnit\Runner\Version::id(), '9.3', '<' ) ) -) { - if ( ! class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) && - file_exists( "$test_root/includes/phpunit7/MockObject/InvocationMocker.php" ) - ) { - // phpcs:disable WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath - require "$test_root/includes/phpunit7/MockObject/Builder/NamespaceMatch.php"; - require "$test_root/includes/phpunit7/MockObject/Builder/ParametersMatch.php"; - require "$test_root/includes/phpunit7/MockObject/InvocationMocker.php"; - require "$test_root/includes/phpunit7/MockObject/MockMethod.php"; - // phpcs:enable - } else { - fprintf( - STDOUT, - "Warning: PHPUnit <9.3 is not compatible with PHP 8.0+, and the hack could not be loaded.\n Class %s exists: %s\n File %s exists: %s\n", - PHPUnit\Framework\MockObject\InvocationMocker::class, - class_exists( PHPUnit\Framework\MockObject\InvocationMocker::class, false ) ? 'yes (bad)' : 'no (good)', - "$test_root/includes/phpunit7/MockObject/InvocationMocker.php", - file_exists( "$test_root/includes/phpunit7/MockObject/InvocationMocker.php" ) ? 'yes (good)' : 'no (bad)' - ); - } -} - if ( '1' !== getenv( 'WP_MULTISITE' ) && ( ! defined( 'WP_TESTS_MULTISITE' ) || ! WP_TESTS_MULTISITE ) ) { echo 'To run Jetpack multisite, use -c tests/php.multisite.xml' . PHP_EOL; echo "Disregard Core's -c tests/phpunit/multisite.xml notice below." . PHP_EOL; diff --git a/projects/plugins/jetpack/tests/php/find-test-root.php b/projects/plugins/jetpack/tests/php/find-test-root.php deleted file mode 100644 index 232f36b9fae2c..0000000000000 --- a/projects/plugins/jetpack/tests/php/find-test-root.php +++ /dev/null @@ -1,48 +0,0 @@ -setExpectedDeprecated( $file_path ); $mock = $this->getMockBuilder( stdClass::class )