Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 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/actions.php

    r47122 r48937  
    1616
    1717        // Only one event occurred for the hook, with empty args.
    18         $this->assertEquals( 1, $a->get_call_count() );
     18        $this->assertSame( 1, $a->get_call_count() );
    1919        // Only our hook was called.
    20         $this->assertEquals( array( $tag ), $a->get_tags() );
     20        $this->assertSame( array( $tag ), $a->get_tags() );
    2121
    2222        $argsvar = $a->get_args();
    2323        $args    = array_pop( $argsvar );
    24         $this->assertEquals( array( '' ), $args );
     24        $this->assertSame( array( '' ), $args );
    2525    }
    2626
     
    3333
    3434        // Make sure our hook was called correctly.
    35         $this->assertEquals( 1, $a->get_call_count() );
    36         $this->assertEquals( array( $tag ), $a->get_tags() );
     35        $this->assertSame( 1, $a->get_call_count() );
     36        $this->assertSame( array( $tag ), $a->get_tags() );
    3737
    3838        // Now remove the action, do it again, and make sure it's not called this time.
    3939        remove_action( $tag, array( &$a, 'action' ) );
    4040        do_action( $tag );
    41         $this->assertEquals( 1, $a->get_call_count() );
    42         $this->assertEquals( array( $tag ), $a->get_tags() );
     41        $this->assertSame( 1, $a->get_call_count() );
     42        $this->assertSame( array( $tag ), $a->get_tags() );
    4343
    4444    }
     
    5151        $this->assertFalse( has_action( $tag ) );
    5252        add_action( $tag, $func );
    53         $this->assertEquals( 10, has_action( $tag, $func ) );
     53        $this->assertSame( 10, has_action( $tag, $func ) );
    5454        $this->assertTrue( has_action( $tag ) );
    5555        remove_action( $tag, $func );
     
    7171
    7272        // Both actions called once each.
    73         $this->assertEquals( 1, $a1->get_call_count() );
    74         $this->assertEquals( 1, $a2->get_call_count() );
     73        $this->assertSame( 1, $a1->get_call_count() );
     74        $this->assertSame( 1, $a2->get_call_count() );
    7575    }
    7676
     
    8585
    8686        $call_count = $a->get_call_count();
    87         $this->assertEquals( 1, $call_count );
     87        $this->assertSame( 1, $call_count );
    8888        $argsvar = $a->get_args();
    89         $this->assertEquals( array( $val ), array_pop( $argsvar ) );
     89        $this->assertSame( array( $val ), array_pop( $argsvar ) );
    9090    }
    9191
     
    105105        $call_count = $a1->get_call_count();
    106106        // $a1 should be called with both args.
    107         $this->assertEquals( 1, $call_count );
     107        $this->assertSame( 1, $call_count );
    108108        $argsvar1 = $a1->get_args();
    109         $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
     109        $this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
    110110
    111111        // $a2 should be called with one only.
    112         $this->assertEquals( 1, $a2->get_call_count() );
     112        $this->assertSame( 1, $a2->get_call_count() );
    113113        $argsvar2 = $a2->get_args();
    114         $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
     114        $this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
    115115    }
    116116
     
    139139        $call_count = $a1->get_call_count();
    140140        // $a1 should be called with both args.
    141         $this->assertEquals( 1, $call_count );
     141        $this->assertSame( 1, $call_count );
    142142        $argsvar1 = $a1->get_args();
    143         $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) );
     143        $this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
    144144
    145145        // $a2 should be called with one only.
    146         $this->assertEquals( 1, $a2->get_call_count() );
     146        $this->assertSame( 1, $a2->get_call_count() );
    147147        $argsvar2 = $a2->get_args();
    148         $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) );
     148        $this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
    149149
    150150        // $a3 should be called with both args.
    151         $this->assertEquals( 1, $a3->get_call_count() );
     151        $this->assertSame( 1, $a3->get_call_count() );
    152152        $argsvar3 = $a3->get_args();
    153         $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar3 ) );
     153        $this->assertSame( array( $val1, $val2 ), array_pop( $argsvar3 ) );
    154154    }
    155155
     
    182182
    183183        // Two events, one per action.
    184         $this->assertEquals( 2, $a->get_call_count() );
     184        $this->assertSame( 2, $a->get_call_count() );
    185185
    186186        $expected = array(
     
    199199        );
    200200
    201         $this->assertEquals( $expected, $a->get_events() );
     201        $this->assertSame( $expected, $a->get_events() );
    202202    }
    203203
     
    208208        // Do action $tag1 but not $tag2.
    209209        do_action( $tag1 );
    210         $this->assertEquals( 1, did_action( $tag1 ) );
    211         $this->assertEquals( 0, did_action( $tag2 ) );
     210        $this->assertSame( 1, did_action( $tag1 ) );
     211        $this->assertSame( 0, did_action( $tag2 ) );
    212212
    213213        // Do action $tag2 a random number of times.
     
    218218
    219219        // $tag1's count hasn't changed, $tag2 should be correct.
    220         $this->assertEquals( 1, did_action( $tag1 ) );
    221         $this->assertEquals( $count, did_action( $tag2 ) );
     220        $this->assertSame( 1, did_action( $tag1 ) );
     221        $this->assertSame( $count, did_action( $tag2 ) );
    222222
    223223    }
     
    230230        // Add an 'all' action.
    231231        add_action( 'all', array( &$a, 'action' ) );
    232         $this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
     232        $this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
    233233        // Do some actions.
    234234        do_action( $tag1 );
     
    238238
    239239        // Our action should have been called once for each tag.
    240         $this->assertEquals( 4, $a->get_call_count() );
     240        $this->assertSame( 4, $a->get_call_count() );
    241241        // Only our hook was called.
    242         $this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
     242        $this->assertSame( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );
    243243
    244244        remove_action( 'all', array( &$a, 'action' ) );
     
    252252
    253253        add_action( 'all', array( &$a, 'action' ) );
    254         $this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) );
     254        $this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
    255255        do_action( $tag );
    256256
    257257        // Make sure our hook was called correctly.
    258         $this->assertEquals( 1, $a->get_call_count() );
    259         $this->assertEquals( array( $tag ), $a->get_tags() );
     258        $this->assertSame( 1, $a->get_call_count() );
     259        $this->assertSame( array( $tag ), $a->get_tags() );
    260260
    261261        // Now remove the action, do it again, and make sure it's not called this time.
     
    263263        $this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
    264264        do_action( $tag );
    265         $this->assertEquals( 1, $a->get_call_count() );
    266         $this->assertEquals( array( $tag ), $a->get_tags() );
     265        $this->assertSame( 1, $a->get_call_count() );
     266        $this->assertSame( array( $tag ), $a->get_tags() );
    267267    }
    268268
     
    313313        add_action( 'test_action_self_removal', array( $this, 'action_self_removal' ) );
    314314        do_action( 'test_action_self_removal' );
    315         $this->assertEquals( 1, did_action( 'test_action_self_removal' ) );
     315        $this->assertSame( 1, did_action( 'test_action_self_removal' ) );
    316316    }
    317317
     
    333333        do_action( $tag, $tag );
    334334
    335         $this->assertEquals( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
    336         $this->assertEquals( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
     335        $this->assertSame( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
     336        $this->assertSame( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
    337337    }
    338338
     
    365365        do_action( $tag, $tag, array( $a, $b, $c, $d, $e ) );
    366366
    367         $this->assertEquals( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
    368         $this->assertEquals( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
    369         $this->assertEquals( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
    370         $this->assertEquals( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
    371         $this->assertEquals( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
     367        $this->assertSame( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
     368        $this->assertSame( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
     369        $this->assertSame( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
     370        $this->assertSame( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
     371        $this->assertSame( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
    372372    }
    373373
     
    436436            ),
    437437        );
    438         $this->assertEquals( 11, has_action( $tag, '__return_null' ) );
     438        $this->assertSame( 11, has_action( $tag, '__return_null' ) );
    439439    }
    440440
     
    449449        $wp_current_filter[] = 'second'; // Let's say a second action was invoked.
    450450
    451         $this->assertEquals( 'second', current_action() );
     451        $this->assertSame( 'second', current_action() );
    452452    }
    453453
     
    499499        add_filter( 'testing', array( $this, 'apply_testing_filter' ) );
    500500        $this->assertTrue( has_action( 'testing' ) );
    501         $this->assertEquals( 10, has_action( 'testing', array( $this, 'apply_testing_filter' ) ) );
     501        $this->assertSame( 10, has_action( 'testing', array( $this, 'apply_testing_filter' ) ) );
    502502
    503503        apply_filters( 'testing', '' );
     
    520520        add_filter( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) );
    521521        $this->assertTrue( has_action( 'testing_nested' ) );
    522         $this->assertEquals( 10, has_action( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) ) );
     522        $this->assertSame( 10, has_action( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) ) );
    523523
    524524        apply_filters( 'testing_nested', '' );
Note: See TracChangeset for help on using the changeset viewer.