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/admin/includesScreen.php

    r47122 r48937  
    204204            set_current_screen();
    205205
    206             $this->assertEquals( $screen->id, $current_screen->id, $hook_name );
    207             $this->assertEquals( $screen->base, $current_screen->base, $hook_name );
     206            $this->assertSame( $screen->id, $current_screen->id, $hook_name );
     207            $this->assertSame( $screen->base, $current_screen->base, $hook_name );
    208208            if ( isset( $screen->action ) ) {
    209                 $this->assertEquals( $screen->action, $current_screen->action, $hook_name );
     209                $this->assertSame( $screen->action, $current_screen->action, $hook_name );
    210210            }
    211211            if ( isset( $screen->post_type ) ) {
    212                 $this->assertEquals( $screen->post_type, $current_screen->post_type, $hook_name );
     212                $this->assertSame( $screen->post_type, $current_screen->post_type, $hook_name );
    213213            } else {
    214214                $this->assertEmpty( $current_screen->post_type, $hook_name );
    215215            }
    216216            if ( isset( $screen->taxonomy ) ) {
    217                 $this->assertEquals( $screen->taxonomy, $current_screen->taxonomy, $hook_name );
     217                $this->assertSame( $screen->taxonomy, $current_screen->taxonomy, $hook_name );
    218218            }
    219219
     
    238238    function test_post_type_as_hookname() {
    239239        $screen = convert_to_screen( 'page' );
    240         $this->assertEquals( $screen->post_type, 'page' );
    241         $this->assertEquals( $screen->base, 'post' );
    242         $this->assertEquals( $screen->id, 'page' );
     240        $this->assertSame( $screen->post_type, 'page' );
     241        $this->assertSame( $screen->base, 'post' );
     242        $this->assertSame( $screen->id, 'page' );
    243243        $this->assertTrue( $screen->is_block_editor );
    244244    }
     
    247247        register_post_type( 'value-add' );
    248248        $screen = convert_to_screen( 'value-add' ); // The '-add' part is key.
    249         $this->assertEquals( $screen->post_type, 'value-add' );
    250         $this->assertEquals( $screen->base, 'post' );
    251         $this->assertEquals( $screen->id, 'value-add' );
     249        $this->assertSame( $screen->post_type, 'value-add' );
     250        $this->assertSame( $screen->base, 'post' );
     251        $this->assertSame( $screen->id, 'value-add' );
    252252        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    253253
    254254        $screen = convert_to_screen( 'edit-value-add' ); // The '-add' part is key.
    255         $this->assertEquals( $screen->post_type, 'value-add' );
    256         $this->assertEquals( $screen->base, 'edit' );
    257         $this->assertEquals( $screen->id, 'edit-value-add' );
     255        $this->assertSame( $screen->post_type, 'value-add' );
     256        $this->assertSame( $screen->base, 'edit' );
     257        $this->assertSame( $screen->id, 'edit-value-add' );
    258258        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    259259    }
     
    262262        register_taxonomy( 'old-or-new', 'post' );
    263263        $screen = convert_to_screen( 'edit-old-or-new' ); // The '-new' part is key.
    264         $this->assertEquals( $screen->taxonomy, 'old-or-new' );
    265         $this->assertEquals( $screen->base, 'edit-tags' );
    266         $this->assertEquals( $screen->id, 'edit-old-or-new' );
     264        $this->assertSame( $screen->taxonomy, 'old-or-new' );
     265        $this->assertSame( $screen->base, 'edit-tags' );
     266        $this->assertSame( $screen->id, 'edit-old-or-new' );
    267267        $this->assertFalse( $screen->is_block_editor );
    268268    }
     
    271271        register_post_type( 'edit-some-thing' );
    272272        $screen = convert_to_screen( 'edit-some-thing' );
    273         $this->assertEquals( $screen->post_type, 'edit-some-thing' );
    274         $this->assertEquals( $screen->base, 'post' );
    275         $this->assertEquals( $screen->id, 'edit-some-thing' );
     273        $this->assertSame( $screen->post_type, 'edit-some-thing' );
     274        $this->assertSame( $screen->base, 'post' );
     275        $this->assertSame( $screen->id, 'edit-some-thing' );
    276276        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    277277
    278278        $screen = convert_to_screen( 'edit-edit-some-thing' );
    279         $this->assertEquals( $screen->post_type, 'edit-some-thing' );
    280         $this->assertEquals( $screen->base, 'edit' );
    281         $this->assertEquals( $screen->id, 'edit-edit-some-thing' );
     279        $this->assertSame( $screen->post_type, 'edit-some-thing' );
     280        $this->assertSame( $screen->base, 'edit' );
     281        $this->assertSame( $screen->id, 'edit-edit-some-thing' );
    282282        $this->assertFalse( $screen->is_block_editor ); // Post types do not support `show_in_rest` by default.
    283283    }
     
    289289        // Sorry, core wins here.
    290290        $screen = convert_to_screen( 'edit-comments' );
    291         $this->assertEquals( $screen->base, 'edit-comments' );
     291        $this->assertSame( $screen->base, 'edit-comments' );
    292292
    293293        // The post type wins here. convert_to_screen( $post_type ) is only relevant for meta boxes anyway.
    294294        $screen = convert_to_screen( 'comments' );
    295         $this->assertEquals( $screen->base, 'post' );
     295        $this->assertSame( $screen->base, 'post' );
    296296
    297297        // Core wins.
    298298        $screen = convert_to_screen( 'edit-tags' );
    299         $this->assertEquals( $screen->base, 'edit-tags' );
     299        $this->assertSame( $screen->base, 'edit-tags' );
    300300
    301301        $screen = convert_to_screen( 'tags' );
    302         $this->assertEquals( $screen->base, 'post' );
     302        $this->assertSame( $screen->base, 'post' );
    303303    }
    304304
     
    306306        $tab      = __FUNCTION__;
    307307        $tab_args = array(
     308            'title'    => 'Help!',
    308309            'id'       => $tab,
    309             'title'    => 'Help!',
    310310            'content'  => 'Some content',
    311311            'callback' => false,
     
    314314        $screen = get_current_screen();
    315315        $screen->add_help_tab( $tab_args );
    316         $this->assertEquals(
     316        $this->assertSame(
    317317            $screen->get_help_tab( $tab ),
    318318            array(
     319                'title'    => 'Help!',
    319320                'id'       => $tab,
    320                 'title'    => 'Help!',
    321321                'content'  => 'Some content',
    322322                'callback' => false,
     
    332332
    333333        $screen->remove_help_tabs();
    334         $this->assertEquals( $screen->get_help_tabs(), array() );
     334        $this->assertSame( $screen->get_help_tabs(), array() );
    335335    }
    336336
     
    381381
    382382        $screen->add_help_tab( $tab_2_args );
    383         $this->assertEquals( $screen->get_help_tab( $tab_2 ), $tab_2_args );
     383        $this->assertSame( $screen->get_help_tab( $tab_2 ), $tab_2_args );
    384384
    385385        $screen->add_help_tab( $tab_3_args );
    386         $this->assertEquals( $screen->get_help_tab( $tab_3 ), $tab_3_args );
     386        $this->assertSame( $screen->get_help_tab( $tab_3 ), $tab_3_args );
    387387
    388388        $screen->add_help_tab( $tab_4_args );
    389389        // Priority is added with the default for future calls.
    390390        $tab_4_args['priority'] = 10;
    391         $this->assertEquals( $screen->get_help_tab( $tab_4 ), $tab_4_args );
     391        $this->assertSame( $screen->get_help_tab( $tab_4 ), $tab_4_args );
    392392
    393393        $tabs = $screen->get_help_tabs();
    394         $this->assertEquals( 4, count( $tabs ) );
     394        $this->assertSame( 4, count( $tabs ) );
    395395        $this->assertArrayHasKey( $tab_1, $tabs );
    396396        $this->assertArrayHasKey( $tab_2, $tabs );
     
    427427
    428428        $screen->remove_help_tabs();
    429         $this->assertEquals( array(), $screen->get_help_tabs() );
     429        $this->assertSame( array(), $screen->get_help_tabs() );
    430430    }
    431431
     
    444444
    445445        $screen->add_option( $option, $option_args );
    446         $this->assertEquals( $screen->get_option( $option ), $option_args );
     446        $this->assertSame( $screen->get_option( $option ), $option_args );
    447447
    448448        $options = $screen->get_options();
     
    453453
    454454        $screen->remove_options();
    455         $this->assertEquals( $screen->get_options(), array() );
     455        $this->assertSame( $screen->get_options(), array() );
    456456    }
    457457
Note: See TracChangeset for help on using the changeset viewer.