From 47303b1f95d49977719278002e4b029a22380b93 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 6 Aug 2021 00:35:01 +0000 Subject: [PATCH] Build/Test Tools: Unify the PHPUnit adapter TestCases. This commit: * Removes the PHPUnit 7 specific `TestCase`. * Removes all existing polyfills from the PHPUnit 5.x `TestCase`. * Imports all polyfill traits from the PHPUnit Polyfills package into the `WP_UnitTestCase` class and updates the DocBlock to reflect the actual function of the class. * Note: The list of polyfills needs to be verified and updated after each new release of the PHPUnit Polyfills package. Alternatively (recommended), one of the built-in `TestCase` classes from the PHPUnit Polyfills package can be used instead. * Moves the `require` for the WP `abstract-testcase.php` to the `bootstrap.php` file. * Adds a `require_once` for the PHPUnit Polyfills autoloader to the `bootstrap.php` file. * Note: while this isn't _strictly_ necessary when the tests are run via Composer, having the include in the bootstrap allows for the tests to also be run via a PHPUnit Phar, providing contributors with more flexibility. Follow-up to [51559]. Props jrf, hellofromTonya, johnbillion, netweb, SergeyBiryukov. See #46149. git-svn-id: https://develop.svn.wordpress.org/trunk@51560 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/bootstrap.php | 19 +- tests/phpunit/includes/phpunit7/testcase.php | 14 - tests/phpunit/includes/testcase.php | 463 ++----------------- 3 files changed, 49 insertions(+), 447 deletions(-) delete mode 100644 tests/phpunit/includes/phpunit7/testcase.php diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index 160a7f7a4d..b96ed477e7 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -50,6 +50,14 @@ if ( version_compare( $phpunit_version, '5.7', '<' ) || version_compare( $phpuni require_once __DIR__ . '/class-mockobject-autoload.php'; spl_autoload_register( 'MockObject_Autoload::load', true, true ); +// Check that the PHPUnit Polyfills autoloader exists. +$phpunit_polyfills_autoloader = __DIR__ . '/../../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; +if ( ! file_exists( $phpunit_polyfills_autoloader ) ) { + echo "Error: You need to run `composer update` before running the tests.\n"; + echo "You can still use a PHPUnit phar to run them, but the dependencies do need to be installed.\n"; + exit( 1 ); +} + // If running core tests, check if all the required PHP extensions are loaded before running the test suite. if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) { $required_extensions = array( @@ -195,13 +203,12 @@ if ( version_compare( tests_get_phpunit_version(), '6.0', '>=' ) ) { require __DIR__ . '/phpunit6/compat.php'; } -// Load separate WP_UnitTestCase classes for PHPUnit 7.5+ and older versions. -if ( version_compare( tests_get_phpunit_version(), '7.5', '>=' ) ) { - require __DIR__ . '/phpunit7/testcase.php'; -} else { - require __DIR__ . '/testcase.php'; -} +// Load the PHPUnit Polyfills autoloader (check for existence of the file is done earlier in the script). +require_once $phpunit_polyfills_autoloader; +unset( $phpunit_polyfills_autoloader ); +require __DIR__ . '/abstract-testcase.php'; +require __DIR__ . '/testcase.php'; require __DIR__ . '/testcase-rest-api.php'; require __DIR__ . '/testcase-rest-controller.php'; require __DIR__ . '/testcase-rest-post-type-controller.php'; diff --git a/tests/phpunit/includes/phpunit7/testcase.php b/tests/phpunit/includes/phpunit7/testcase.php deleted file mode 100644 index 84ddbd5be1..0000000000 --- a/tests/phpunit/includes/phpunit7/testcase.php +++ /dev/null @@ -1,14 +0,0 @@ -