From e27454feb173f848a4a01f29a5495935aa731bba Mon Sep 17 00:00:00 2001 From: fetachino Date: Mon, 10 Aug 2026 17:05:32 -0400 Subject: [PATCH] Fix PHP minimum version readiness check --- Tests/Unit/testBootstrap.php | 21 +++++++++++++++++++++ bootstrap-functions.php | 10 +++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Tests/Unit/testBootstrap.php b/Tests/Unit/testBootstrap.php index 0f3362f..df16b7e 100644 --- a/Tests/Unit/testBootstrap.php +++ b/Tests/Unit/testBootstrap.php @@ -3,6 +3,7 @@ namespace WPMedia\PHPUnit\Tests\Unit; use WPMedia\PHPUnit\Unit\TestCase; +use function WPMedia\PHPUnit\check_readiness; class Test_Bootstrap extends TestCase { @@ -16,4 +17,24 @@ function testShouldReturnTrueWhenRootBootstrapFileIsInMemory() { $this->assertTrue( function_exists( __NAMESPACE__ . '\is_unit_test_bootstrap' ) ); $this->assertTrue( is_unit_test_bootstrap() ); } + + function testShouldAcceptMinimumSupportedPhpVersion() { + $this->assertNull( check_readiness( '7.4.0' ) ); + } + + function testShouldRejectPhpVersionBelowSupportedMinimum() { + $error = null; + set_error_handler( + function ( $errno, $errstr ) use ( &$error ) { + $error = compact( 'errno', 'errstr' ); + return true; + } + ); + + check_readiness( '7.3.99' ); + restore_error_handler(); + + $this->assertSame( E_USER_ERROR, $error['errno'] ); + $this->assertSame( 'Test Suite requires PHP 7.4 or higher.', $error['errstr'] ); + } } diff --git a/bootstrap-functions.php b/bootstrap-functions.php index cca6e40..7b12e48 100644 --- a/bootstrap-functions.php +++ b/bootstrap-functions.php @@ -18,10 +18,14 @@ function init_test_suite() { /** * Check the system's readiness to run the tests. + * + * @param string|null $php_version PHP version to validate. Defaults to the running version. */ -function check_readiness() { - if ( version_compare( phpversion(), '7.1.0', '<' ) ) { - trigger_error( 'Test Suite requires PHP 7.1 or higher.', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Valid use case for our testing suite. +function check_readiness( $php_version = null ) { + $php_version = $php_version ?: phpversion(); + + if ( version_compare( $php_version, '7.4.0', '<' ) ) { + trigger_error( 'Test Suite requires PHP 7.4 or higher.', E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Valid use case for our testing suite. } if ( ! file_exists( WPMEDIA_PHPUNIT_ROOT_DIR . '/vendor/autoload.php' ) ) {