Skip to content

Commit

Permalink
Update PHPUnit setup to remove WP < 5.9 oddities
Browse files Browse the repository at this point in the history
See #21175, #21157

Primary issue: #24082
  • Loading branch information
jeherve committed Apr 26, 2022
1 parent 869efe6 commit cde8ba3
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 144 deletions.
35 changes: 1 addition & 34 deletions docs/examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php
/**
* PHPUnit bootstrap file to apply WordPress core's monkey-patching to versions
* of PHPUnit that don't natively support PHP 8.0 for PHP 8.0.
*
* WARNING: This is outdated as of September 2021. WordPress is in the midst of
* updating their stuff to stop being hacky, but have left WP 5.7 and 5.8 in a
* strange in-between state. Once we only support 5.9+ we should update this
* for whatever it turns out to be.
* PHPUnit bootstrap file.
*
* @package automattic/jetpack
*/
Expand Down Expand Up @@ -52,33 +46,6 @@
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo "Using test root $_tests_dir\n";

// WordPress requires 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.
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( "$_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';

Expand Down
1 change: 0 additions & 1 deletion projects/plugins/jetpack/tests/action-phpunit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh
exec phpunit "$@"
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

PLUGINDIR="$PWD"

cd "$MONOREPO_BASE/projects/plugins/jetpack"
Expand Down
2 changes: 0 additions & 2 deletions projects/plugins/jetpack/tests/action-test-php.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eo pipefail

. tests/maybe-downgrade-phpunit.sh

echo "::group::Jetpack tests"
phpunit
echo "::endgroup::"
Expand Down
24 changes: 0 additions & 24 deletions projects/plugins/jetpack/tests/maybe-downgrade-phpunit.sh

This file was deleted.

69 changes: 40 additions & 29 deletions projects/plugins/jetpack/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
48 changes: 0 additions & 48 deletions projects/plugins/jetpack/tests/php/find-test-root.php

This file was deleted.

4 changes: 0 additions & 4 deletions projects/plugins/jetpack/tests/php/test_deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class WP_Test_Jetpack_Deprecation extends WP_UnitTestCase {
* @dataProvider provider_deprecated_file_paths
*/
public function test_deprecated_file_paths( $file_path, $replacement_path ) {
// WordPress 5.9 starts adding this hook. Simplest way to make the tests work
// is to add it for earlier versions too.
// @todo Remove the add_action once we drop support for WordPress < 5.9.
add_action( 'deprecated_file_included', array( $this, 'deprecated_function_run' ) );
$this->setExpectedDeprecated( $file_path );

$mock = $this->getMockBuilder( stdClass::class )
Expand Down

0 comments on commit cde8ba3

Please sign in to comment.