From e4f559a390c46c1bc548c47aa0c710c4ab26ef59 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 1 Oct 2024 14:34:24 +0200 Subject: [PATCH 1/4] UtilsTest: fix the test failure Tests on PHP 8.1 and higher (which are running PHPUnit 10+) are failing with the below message: ``` An error occurred inside PHPUnit. Message: Interface "WpOrg\Requests\Transport" not found Location: /home/runner/work/wp-cli/wp-cli/tests/mock-requests-transport.php:6 ``` This may be due to the test classes being loaded (to count the number of tests) before the test bootstrap is being run (which includes the autoloaders). If that "guess" is correct, this patch should fix this. --- tests/UtilsTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index db3bc74062..75f9a4110f 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -5,11 +5,13 @@ use WP_CLI\Tests\TestCase; use WP_CLI\Utils; -require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; -require_once __DIR__ . '/mock-requests-transport.php'; - class UtilsTest extends TestCase { + public static function set_up_before_class() { + require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; + require_once __DIR__ . '/mock-requests-transport.php'; + } + public function testIncrementVersion() { // Keyword increments. $this->assertEquals( From 440d013e389c364d587a006598d4657c92afe1ff Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 1 Oct 2024 14:40:23 +0200 Subject: [PATCH 2/4] Tests: apply the same change to other test files While these tests are not showing errors at this time, it is still best practice to include files in the test `set_up_before_class()` method, not when the file is being read. This applies this change to all other test files which were using the anti-pattern. --- tests/CommandFactoryTest.php | 6 ++++-- tests/FileCacheTest.php | 6 ++++-- tests/HelpTest.php | 10 ++++++---- tests/WP_CLI/WpOrgApiTest.php | 6 ++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/CommandFactoryTest.php b/tests/CommandFactoryTest.php index afc95a5509..cdf33fdf0b 100644 --- a/tests/CommandFactoryTest.php +++ b/tests/CommandFactoryTest.php @@ -2,10 +2,12 @@ use WP_CLI\Tests\TestCase; -require_once dirname( __DIR__ ) . '/php/class-wp-cli-command.php'; - class CommandFactoryTest extends TestCase { + public static function set_up_before_class() { + require_once dirname( __DIR__ ) . '/php/class-wp-cli-command.php'; + } + /** * @dataProvider dataProviderExtractLastDocComment */ diff --git a/tests/FileCacheTest.php b/tests/FileCacheTest.php index 301397fe0c..2e8fce5a1b 100644 --- a/tests/FileCacheTest.php +++ b/tests/FileCacheTest.php @@ -5,10 +5,12 @@ use WP_CLI\Tests\TestCase; use WP_CLI\Utils; -require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; - class FileCacheTest extends TestCase { + public static function set_up_before_class() { + require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; + } + /** * Test get_root() deals with backslashed directory. */ diff --git a/tests/HelpTest.php b/tests/HelpTest.php index accaad7073..6be0c6eb2f 100644 --- a/tests/HelpTest.php +++ b/tests/HelpTest.php @@ -2,12 +2,14 @@ use WP_CLI\Tests\TestCase; -require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; -require_once dirname( __DIR__ ) . '/php/class-wp-cli-command.php'; -require_once dirname( __DIR__ ) . '/php/commands/help.php'; - class HelpTest extends TestCase { + public static function set_up_before_class() { + require_once dirname( __DIR__ ) . '/php/class-wp-cli.php'; + require_once dirname( __DIR__ ) . '/php/class-wp-cli-command.php'; + require_once dirname( __DIR__ ) . '/php/commands/help.php'; + } + public function test_parse_reference_links() { $test_class = new ReflectionClass( 'Help_Command' ); $method = $test_class->getMethod( 'parse_reference_links' ); diff --git a/tests/WP_CLI/WpOrgApiTest.php b/tests/WP_CLI/WpOrgApiTest.php index efea426318..f6f3a5daa4 100644 --- a/tests/WP_CLI/WpOrgApiTest.php +++ b/tests/WP_CLI/WpOrgApiTest.php @@ -3,10 +3,12 @@ use WP_CLI\Tests\TestCase; use WP_CLI\WpOrgApi; -require_once dirname( __DIR__ ) . '/mock-requests-transport.php'; - class WpOrgApiTest extends TestCase { + public static function set_up_before_class() { + require_once dirname( __DIR__ ) . '/mock-requests-transport.php'; + } + public static function data_http_request_verify() { return [ 'can retrieve core checksums' => [ From 976c63d70dcec83ccc749cf7683508af279d7280 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 1 Oct 2024 18:01:23 +0200 Subject: [PATCH 3/4] Use `file_exists` --- php/WP_CLI/Extractor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/WP_CLI/Extractor.php b/php/WP_CLI/Extractor.php index b17b501f26..53ee26b6c7 100644 --- a/php/WP_CLI/Extractor.php +++ b/php/WP_CLI/Extractor.php @@ -48,7 +48,7 @@ private static function extract_zip( $zipfile, $dest ) { throw new Exception( "Could not create folder '{$dest}'." ); } - if ( ! file( $zipfile ) + if ( ! file_exists( $zipfile ) || ! is_readable( $zipfile ) || filesize( $zipfile ) <= 0 ) { throw new Exception( "Invalid zip file '{$zipfile}'." ); @@ -127,7 +127,7 @@ private static function extract_tarball( $tarball, $dest ) { $tarball = "./{$tarball}"; } - if ( ! file( $tarball ) + if (! file_exists( $tarball ) || ! is_readable( $tarball ) || filesize( $tarball ) <= 0 ) { throw new Exception( "Invalid zip file '{$tarball}'." ); From 4b35a49b4bced7b1dbeb4b9ef85fe0348580bc03 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 1 Oct 2024 18:03:13 +0200 Subject: [PATCH 4/4] Add missing space --- php/WP_CLI/Extractor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/WP_CLI/Extractor.php b/php/WP_CLI/Extractor.php index 53ee26b6c7..d8138412f8 100644 --- a/php/WP_CLI/Extractor.php +++ b/php/WP_CLI/Extractor.php @@ -127,7 +127,7 @@ private static function extract_tarball( $tarball, $dest ) { $tarball = "./{$tarball}"; } - if (! file_exists( $tarball ) + if ( ! file_exists( $tarball ) || ! is_readable( $tarball ) || filesize( $tarball ) <= 0 ) { throw new Exception( "Invalid zip file '{$tarball}'." );