wordpress-develop/tests/phpunit/tests/query/noFoundRows.php
Sergey Biryukov c70fe62ed1 Tests: Replace assertContains() with assertStringContainsString() when used with strings.
Using the `assertContains()` and `assertNotContains()` methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

* `assertStringContainsString()`
* `assertStringContainsStringIgnoringCase`
* `assertStringNotContainsString()`
* `assertStringNotContainsStringIgnoringCase`

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods were added to the `WP_UnitTestCase` class for PHPUnit < 7.5.

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51462 602fd350-edb4-49c9-b593-d223f7449a82
2021-07-19 14:00:11 +00:00

106 lines
2.1 KiB
PHP

<?php
/**
* @group query
*/
class Tests_Query_NoFoundRows extends WP_UnitTestCase {
public function test_no_found_rows_default() {
$q = new WP_Query(
array(
'post_type' => 'post',
)
);
$this->assertStringContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
public function test_no_found_rows_false() {
$q = new WP_Query(
array(
'post_type' => 'post',
'no_found_rows' => false,
)
);
$this->assertStringContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
public function test_no_found_rows_0() {
$q = new WP_Query(
array(
'post_type' => 'post',
'no_found_rows' => 0,
)
);
$this->assertStringContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
public function test_no_found_rows_empty_string() {
$q = new WP_Query(
array(
'post_type' => 'post',
'no_found_rows' => '',
)
);
$this->assertStringContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
public function test_no_found_rows_true() {
$q = new WP_Query(
array(
'post_type' => 'post',
'no_found_rows' => true,
)
);
$this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
public function test_no_found_rows_non_bool_cast_to_true() {
$q = new WP_Query(
array(
'post_type' => 'post',
'no_found_rows' => 'foo',
)
);
$this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
}
/**
* @ticket 29552
*/
public function test_no_found_rows_default_with_nopaging_true() {
$p = $this->factory->post->create();
$q = new WP_Query(
array(
'post_type' => 'post',
'nopaging' => true,
)
);
$this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
$this->assertSame( 1, $q->found_posts );
}
/**
* @ticket 29552
*/
public function test_no_found_rows_default_with_postsperpage_minus1() {
$p = $this->factory->post->create();
$q = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => -1,
)
);
$this->assertStringNotContainsString( 'SQL_CALC_FOUND_ROWS', $q->request );
$this->assertSame( 1, $q->found_posts );
}
}