Make WordPress Core

Changeset 60137


Ignore:
Timestamp:
04/07/2025 11:13:03 PM (3 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Simplify the tests for force_ssl_admin().

Includes:

  • The $forced static variable is reset to false before each test, and the function always returns the previous value on the first call, so there is no need to pass the same value from the data provider.
  • Clarifying data provider keys by removing the “first call” references to avoid confusion, as the test method always calls the function twice for each input value.
  • Replacing unnecessarily redundant test method name for consistency with other tests.
  • Moving the data provider after the test method for consistency with other tests.
  • Adjusting DocBlock formatting as per the documentation standards.

Follow-up to [59830].

See #63167.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/forceSslAdmin.php

    r59830 r60137  
    22/**
    33 * Test cases for the `force_ssl_admin()` function.
    4  *
    5  * @package WordPress\UnitTests
    64 *
    75 * @since 6.8.0
     
    1311class Tests_Functions_ForceSslAdmin extends WP_UnitTestCase {
    1412
    15     public function set_up(): void {
     13    public function set_up() {
    1614        parent::set_up();
    17         // Reset the static variable before each test
     15        // Reset the `$forced` static variable before each test.
    1816        force_ssl_admin( false );
    1917    }
    2018
    2119    /**
    22      * Data provider for testing force_ssl_admin.
     20     * Tests that force_ssl_admin() returns expected values based on various inputs.
    2321     *
    24      * Provides various inputs and expected outcomes for the function.
     22     * @dataProvider data_force_ssl_admin
     23     *
     24     * @param mixed $input    The input value to test.
     25     * @param bool  $expected The expected result for subsequent calls.
     26     */
     27    public function test_force_ssl_admin( $input, $expected ) {
     28        // The first call always returns the previous value.
     29        $this->assertFalse( force_ssl_admin( $input ), 'First call did not return the expected value' );
     30
     31        // Call again to check subsequent behavior.
     32        $this->assertSame( $expected, force_ssl_admin( $input ), 'Subsequent call did not return the expected value' );
     33    }
     34
     35    /**
     36     * Data provider for testing force_ssl_admin().
    2537     *
    2638     * @return array[]
    2739     */
    28     public function data_should_return_expected_value_when_various_inputs_are_passed() {
     40    public function data_force_ssl_admin() {
    2941        return array(
    30             'default'                     => array( null, false, false ),
    31             'first_call_true'             => array( true, false, true ),
    32             'first_call_false'            => array( false, false, false ),
    33             'first_call_non_empty_string' => array( 'some string', false, true ),
    34             'empty_string'                => array( '', false, false ),
    35             'first_call_integer_1'        => array( 1, false, true ),
    36             'integer_0'                   => array( 0, false, false ),
     42            'default'          => array( null, false ),
     43            'true'             => array( true, true ),
     44            'false'            => array( false, false ),
     45            'non-empty string' => array( 'some string', true ),
     46            'empty string'     => array( '', false ),
     47            'integer 1'        => array( 1, true ),
     48            'integer 0'        => array( 0, false ),
    3749        );
    3850    }
    39 
    40     /**
    41      * Tests that force_ssl_admin returns expected values based on various inputs.
    42      *
    43      * @dataProvider data_should_return_expected_value_when_various_inputs_are_passed
    44      *
    45      * @param mixed $input                   The input value to test.
    46      * @param bool $expected_first_call      The expected result for the first call.
    47      * @param bool $expected_subsequent_call The expected result for subsequent calls.
    48      */
    49     public function test_should_return_expected_value_when_various_inputs_are_passed( $input, $expected_first_call, $expected_subsequent_call ) {
    50         $this->assertSame( $expected_first_call, force_ssl_admin( $input ), 'First call did not return expected value' );
    51 
    52         // Call again to check subsequent behavior
    53         $this->assertSame( $expected_subsequent_call, force_ssl_admin( $input ), 'Subsequent call did not return expected value' );
    54     }
    5551}
Note: See TracChangeset for help on using the changeset viewer.