Build/Test Tools: Improve test coverage for `wp_validate_boolean()`.

This change also reworks the test class to use a data provider.

Props pbearne, desrosj
Fixes #39868

git-svn-id: https://develop.svn.wordpress.org/trunk@46159 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2019-09-17 18:48:27 +00:00
parent 5a200bdb7a
commit 5e38f8219b
1 changed files with 45 additions and 61 deletions

View File

@ -1,75 +1,59 @@
<?php <?php
/** /**
* Tests the wp_validate_boolean function.
*
* @covers ::wp_validate_boolean
* @group functions.php * @group functions.php
*/ */
class Tests_Functions_WpValidateBoolean extends WP_UnitTestCase { class Tests_Functions_WpValidateBoolean extends WP_UnitTestCase {
public function test_bool_true() { /**
$this->assertTrue( wp_validate_boolean( true ) ); * Provides test scenarios for all possible scenarios in wp_validate_boolean().
} *
* @return array
*/
function data_provider() {
$std = new \stdClass();
public function test_int_1() { return array(
$this->assertTrue( wp_validate_boolean( 1 ) ); array( null, false ),
} array( true, true ),
array( false, false ),
public function test_string_true_lowercase() { array( 'true', true ),
$this->assertTrue( wp_validate_boolean( 'true' ) ); array( 'false', false ),
} array( 'FalSE', false ), // @ticket 30238
array( 'FALSE', false ), // @ticket 30238
public function test_string_true_uppercase() { array( 'TRUE', true ),
$this->assertTrue( wp_validate_boolean( 'TRUE' ) ); array( ' FALSE ', true ),
} array( 'yes', true ),
array( 'no', true ),
public function test_arbitrary_string_should_return_true() { array( 'string', true ),
$this->assertTrue( wp_validate_boolean( 'foobar' ) ); array( '', false ),
} array( array(), false ),
array( 1, true ),
public function test_bool_false() { array( 0, false ),
$this->assertFalse( wp_validate_boolean( false ) ); array( -1, true ),
} array( 99, true ),
array( 0.1, true ),
public function test_int_0() { array( 0.0, false ),
$this->assertFalse( wp_validate_boolean( 0 ) ); array( '1', true ),
} array( '0', false ),
array( $std, true ),
public function test_float_0() { );
$this->assertFalse( wp_validate_boolean( 0.0 ) );
}
public function test_empty_string() {
$this->assertFalse( wp_validate_boolean( '' ) );
}
public function test_string_0() {
$this->assertFalse( wp_validate_boolean( '0' ) );
}
public function test_empty_array() {
$this->assertFalse( wp_validate_boolean( array() ) );
}
public function test_null() {
$this->assertFalse( wp_validate_boolean( null ) );
}
public function test_string_false_lowercase() {
// Differs from (bool) conversion.
$this->assertFalse( wp_validate_boolean( 'false' ) );
} }
/** /**
* Test wp_validate_boolean().
*
* @dataProvider data_provider
*
* @param mixed $test_value.
* @param bool $expected.
*
* @ticket 30238 * @ticket 30238
* @ticket 39868
*/ */
public function test_string_false_uppercase() { public function test_wp_validate_boolean( $test_value, $expected ) {
// Differs from (bool) conversion. $this->assertEquals( wp_validate_boolean( $test_value ), $expected );
$this->assertFalse( wp_validate_boolean( 'FALSE' ) );
}
/**
* @ticket 30238
*/
public function test_string_false_mixedcase() {
// Differs from (bool) conversion.
$this->assertFalse( wp_validate_boolean( 'FaLsE' ) );
} }
} }