From 066959ea5e5933ce31f5ce31fc163d383088418a Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sat, 11 Jun 2022 13:04:37 -0700 Subject: [PATCH] Fix fatal in test run under PHP 8 ``` Fatal error: Declaration of PostFilters::setUp() must be compatible with Yoast\PHPUnitPolyfills\TestCases\TestCase::setUp(): void in /builds/wp-plugins/view-all-posts-pages/tests/test-post-filters.php on line 40 ``` --- tests/bootstrap.php | 3 ++ tests/class-test-case.php | 55 +++++++++++++++++++++++++++++++++++++ tests/test-post-filters.php | 16 ++++------- 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 tests/class-test-case.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7dc0c88..194b1c0 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -27,5 +27,8 @@ function _manually_load_plugin() { } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); +// Load base class for compatibility. +require __DIR__ . '/class-test-case.php'; + // Start up the WP testing environment. require $_tests_dir . '/includes/bootstrap.php'; diff --git a/tests/class-test-case.php b/tests/class-test-case.php new file mode 100644 index 0000000..05d9d9f --- /dev/null +++ b/tests/class-test-case.php @@ -0,0 +1,55 @@ +<?php +/** + * Compatibility shim for PHP 8 tests. + * + * Yoast polyfills add return type declaration to `setUp` that isn't supported + * before PHP 7.1, hence this workaround. + */ + +namespace View_All_Posts_Pages\Tests; + +if ( version_compare( phpversion(), '8.0.0', '<' ) ) { + /** + * Class TestCase. + */ + abstract class TestCase extends WP_UnitTestCase { + /** + * Set up the test. + * + * @return void + */ + protected function setUp() { + parent::setUp(); + $this->_do_set_up(); + } + + /** + * Set up the test. + * + * @return void + */ + abstract function _do_set_up(); + } +} else { + abstract class TestCase extends WP_UnitTestCase { + /** + * Set up the test. + * + * @return void + */ + protected function setUp(): void { + parent::setUp(); + $this->_do_set_up(); + } + + /** + * Set up the test. + * + * Not setting a return type as implementing methods cannot always do + * so. + * + * @return void + */ + abstract protected function _do_set_up(); + } +} diff --git a/tests/test-post-filters.php b/tests/test-post-filters.php index c1a97a8..0749a7a 100755 --- a/tests/test-post-filters.php +++ b/tests/test-post-filters.php @@ -5,10 +5,12 @@ * @package View_All_Posts_Pages */ +use View_All_Posts_Pages\Tests\TestCase; + /** * Content-filter test case. */ -class PostFilters extends WP_UnitTestCase { +class PostFilters extends TestCase { /** * Text for each page of multipage post. * @@ -40,7 +42,7 @@ class PostFilters extends WP_UnitTestCase { * Not using `setUp` because Yoast polyfills add a return type for PHP 8 * that isn't supported before PHP 7.1. */ - protected function _create_post() { + protected function _do_set_up() { static::$post_id = $this->factory->post->create( array( 'post_title' => 'Pagination Test', @@ -55,8 +57,6 @@ class PostFilters extends WP_UnitTestCase { * Test retrieving page 1 content. */ public function test_view_page_1() { - $this->_create_post(); - query_posts( array( 'p' => static::$post_id, @@ -76,8 +76,6 @@ class PostFilters extends WP_UnitTestCase { * Test retrieving page 2 content. */ public function test_view_page_2() { - $this->_create_post(); - query_posts( array( 'p' => static::$post_id, @@ -98,8 +96,6 @@ class PostFilters extends WP_UnitTestCase { * Test retrieving page 3 content. */ public function test_view_page_3() { - $this->_create_post(); - query_posts( array( 'p' => static::$post_id, @@ -120,8 +116,6 @@ class PostFilters extends WP_UnitTestCase { * Test retrieving "view all" contents. */ public function test_view_all() { - $this->_create_post(); - query_posts( array( 'p' => static::$post_id, @@ -137,7 +131,7 @@ class PostFilters extends WP_UnitTestCase { $content = get_the_content(); foreach ( static::$pages_content as $page => $text ) { - $this->assertContains( $text, $content, "Failed to assert that content contained page {$page}." ); + $this->assertStringContainsString( $text, $content, "Failed to assert that content contained page {$page}." ); } } } -- GitLab