diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7dc0c88ef7d679634cf3e4481b57c922c982fe77..194b1c0ba10ec0bac6d6ebe82d802a93f23d2333 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 0000000000000000000000000000000000000000..05d9d9f4410b4e570db1acae1bbc2ebd6a5c8f82 --- /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 c1a97a8d70307a3dcb57102327c4892e7c2b4008..0749a7aa735b2b834b6e87fb43d02e5d94f6afdc 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}." ); } } }