Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/filters.php

    r47122 r48937  
    1414
    1515        add_filter( $tag, array( $a, 'filter' ) );
    16         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     16        $this->assertSame( $val, apply_filters( $tag, $val ) );
    1717
    1818        // Only one event occurred for the hook, with empty args.
    19         $this->assertEquals( 1, $a->get_call_count() );
     19        $this->assertSame( 1, $a->get_call_count() );
    2020        // Only our hook was called.
    21         $this->assertEquals( array( $tag ), $a->get_tags() );
     21        $this->assertSame( array( $tag ), $a->get_tags() );
    2222
    2323        $argsvar = $a->get_args();
    2424        $args    = array_pop( $argsvar );
    25         $this->assertEquals( array( $val ), $args );
     25        $this->assertSame( array( $val ), $args );
    2626    }
    2727
     
    3232
    3333        add_filter( $tag, array( $a, 'filter' ) );
    34         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     34        $this->assertSame( $val, apply_filters( $tag, $val ) );
    3535
    3636        // Make sure our hook was called correctly.
    37         $this->assertEquals( 1, $a->get_call_count() );
    38         $this->assertEquals( array( $tag ), $a->get_tags() );
     37        $this->assertSame( 1, $a->get_call_count() );
     38        $this->assertSame( array( $tag ), $a->get_tags() );
    3939
    4040        // Now remove the filter, do it again, and make sure it's not called this time.
    4141        remove_filter( $tag, array( $a, 'filter' ) );
    42         $this->assertEquals( $val, apply_filters( $tag, $val ) );
    43         $this->assertEquals( 1, $a->get_call_count() );
    44         $this->assertEquals( array( $tag ), $a->get_tags() );
     42        $this->assertSame( $val, apply_filters( $tag, $val ) );
     43        $this->assertSame( 1, $a->get_call_count() );
     44        $this->assertSame( array( $tag ), $a->get_tags() );
    4545
    4646    }
     
    5353            $this->assertFalse( has_filter( $tag ) );
    5454            add_filter( $tag, $func );
    55             $this->assertEquals( 10, has_filter( $tag, $func ) );
     55            $this->assertSame( 10, has_filter( $tag, $func ) );
    5656            $this->assertTrue( has_filter( $tag ) );
    5757            remove_filter( $tag, $func );
     
    7171        add_filter( $tag, array( $a2, 'filter' ) );
    7272
    73         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     73        $this->assertSame( $val, apply_filters( $tag, $val ) );
    7474
    7575        // Both filters called once each.
    76         $this->assertEquals( 1, $a1->get_call_count() );
    77         $this->assertEquals( 1, $a2->get_call_count() );
     76        $this->assertSame( 1, $a1->get_call_count() );
     77        $this->assertSame( 1, $a2->get_call_count() );
    7878    }
    7979
     
    8686        add_filter( $tag, array( $a, 'filter' ), 10, 2 );
    8787        // Call the filter with a single argument.
    88         $this->assertEquals( $val, apply_filters( $tag, $val, $arg1 ) );
    89 
    90         $this->assertEquals( 1, $a->get_call_count() );
     88        $this->assertSame( $val, apply_filters( $tag, $val, $arg1 ) );
     89
     90        $this->assertSame( 1, $a->get_call_count() );
    9191        $argsvar = $a->get_args();
    92         $this->assertEquals( array( $val, $arg1 ), array_pop( $argsvar ) );
     92        $this->assertSame( array( $val, $arg1 ), array_pop( $argsvar ) );
    9393    }
    9494
     
    105105        add_filter( $tag, array( $a2, 'filter' ) );
    106106        // Call the filter with two arguments.
    107         $this->assertEquals( $val, apply_filters( $tag, $val, $arg1, $arg2 ) );
     107        $this->assertSame( $val, apply_filters( $tag, $val, $arg1, $arg2 ) );
    108108
    109109        // $a1 should be called with both args.
    110         $this->assertEquals( 1, $a1->get_call_count() );
     110        $this->assertSame( 1, $a1->get_call_count() );
    111111        $argsvar1 = $a1->get_args();
    112         $this->assertEquals( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );
     112        $this->assertSame( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );
    113113
    114114        // $a2 should be called with one only.
    115         $this->assertEquals( 1, $a2->get_call_count() );
     115        $this->assertSame( 1, $a2->get_call_count() );
    116116        $argsvar2 = $a2->get_args();
    117         $this->assertEquals( array( $val ), array_pop( $argsvar2 ) );
     117        $this->assertSame( array( $val ), array_pop( $argsvar2 ) );
    118118    }
    119119
     
    126126        add_filter( $tag, array( $a, 'filter' ), 10 );
    127127        add_filter( $tag, array( $a, 'filter2' ), 9 );
    128         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     128        $this->assertSame( $val, apply_filters( $tag, $val ) );
    129129
    130130        // There should be two events, one per filter.
    131         $this->assertEquals( 2, $a->get_call_count() );
     131        $this->assertSame( 2, $a->get_call_count() );
    132132
    133133        $expected = array(
     
    146146        );
    147147
    148         $this->assertEquals( $expected, $a->get_events() );
     148        $this->assertSame( $expected, $a->get_events() );
    149149    }
    150150
     
    158158        add_filter( 'all', array( $a, 'filterall' ) );
    159159        // Apply some filters.
    160         $this->assertEquals( $val, apply_filters( $tag1, $val ) );
    161         $this->assertEquals( $val, apply_filters( $tag2, $val ) );
    162         $this->assertEquals( $val, apply_filters( $tag1, $val ) );
    163         $this->assertEquals( $val, apply_filters( $tag1, $val ) );
     160        $this->assertSame( $val, apply_filters( $tag1, $val ) );
     161        $this->assertSame( $val, apply_filters( $tag2, $val ) );
     162        $this->assertSame( $val, apply_filters( $tag1, $val ) );
     163        $this->assertSame( $val, apply_filters( $tag1, $val ) );
    164164
    165165        // Our filter should have been called once for each apply_filters call.
    166         $this->assertEquals( 4, $a->get_call_count() );
     166        $this->assertSame( 4, $a->get_call_count() );
    167167        // The right hooks should have been called in order.
    168         $this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
     168        $this->assertSame( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
    169169
    170170        remove_filter( 'all', array( $a, 'filterall' ) );
     
    180180        add_filter( 'all', array( $a, 'filterall' ) );
    181181        $this->assertTrue( has_filter( 'all' ) );
    182         $this->assertEquals( 10, has_filter( 'all', array( $a, 'filterall' ) ) );
    183         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     182        $this->assertSame( 10, has_filter( 'all', array( $a, 'filterall' ) ) );
     183        $this->assertSame( $val, apply_filters( $tag, $val ) );
    184184
    185185        // Make sure our hook was called correctly.
    186         $this->assertEquals( 1, $a->get_call_count() );
    187         $this->assertEquals( array( $tag ), $a->get_tags() );
     186        $this->assertSame( 1, $a->get_call_count() );
     187        $this->assertSame( array( $tag ), $a->get_tags() );
    188188
    189189        // Now remove the filter, do it again, and make sure it's not called this time.
     
    191191        $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
    192192        $this->assertFalse( has_filter( 'all' ) );
    193         $this->assertEquals( $val, apply_filters( $tag, $val ) );
     193        $this->assertSame( $val, apply_filters( $tag, $val ) );
    194194        // Call cound should remain at 1.
    195         $this->assertEquals( 1, $a->get_call_count() );
    196         $this->assertEquals( array( $tag ), $a->get_tags() );
     195        $this->assertSame( 1, $a->get_call_count() );
     196        $this->assertSame( array( $tag ), $a->get_tags() );
    197197    }
    198198
     
    249249        $result = apply_filters_ref_array( $tag, array( 'string', &$obj ) );
    250250
    251         $this->assertEquals( $result, 'string_append_append' );
     251        $this->assertSame( $result, 'string_append_append' );
    252252
    253253        $args = $a->get_args();
     
    374374        remove_action( 'test_current_priority', array( $this, '_other_priority_action' ), 99 );
    375375
    376         $this->assertSame( false, $this->current_priority );
     376        $this->assertFalse( $this->current_priority );
    377377    }
    378378
Note: See TracChangeset for help on using the changeset viewer.