Skip to content
Snippets Groups Projects
Commit 066959ea authored by Erick Hitter's avatar Erick Hitter
Browse files

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
```
parent 19660d31
Branches
No related tags found
1 merge request!18Fix fatal in test run under PHP 8
Pipeline #4783 failed
......@@ -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';
<?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();
}
}
......@@ -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}." );
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment