Make WordPress Core


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

    r48858 r48937  
    156156        }
    157157
    158         $this->assertEquals( $num, count( $result ) );
     158        $this->assertSame( $num, count( $result ) );
    159159
    160160    }
     
    289289         * the 'dynamic_sidebar_before' hook will be false.
    290290         */
    291         $this->assertSame( false, $this->valid_sidebar );
     291        $this->assertFalse( $this->valid_sidebar );
    292292    }
    293293
     
    350350        $retval = $widget->form( array() );
    351351        $output = ob_get_clean();
    352         $this->assertEquals( 'noform', $retval );
     352        $this->assertSame( 'noform', $retval );
    353353        $this->assertContains( 'no-options-widget', $output );
    354354    }
     
    362362        $foo_widget = new WP_Widget( $id_base, $name );
    363363
    364         $this->assertEquals( $id_base, $foo_widget->id_base );
    365         $this->assertEquals( $name, $foo_widget->name );
    366         $this->assertEquals( "widget_{$id_base}", $foo_widget->option_name );
     364        $this->assertSame( $id_base, $foo_widget->id_base );
     365        $this->assertSame( $name, $foo_widget->name );
     366        $this->assertSame( "widget_{$id_base}", $foo_widget->option_name );
    367367        $this->assertArrayHasKey( 'classname', $foo_widget->widget_options );
    368         $this->assertEquals( "widget_{$id_base}", $foo_widget->widget_options['classname'] );
     368        $this->assertSame( "widget_{$id_base}", $foo_widget->widget_options['classname'] );
    369369        $this->assertArrayHasKey( 'id_base', $foo_widget->control_options );
    370         $this->assertEquals( $id_base, $foo_widget->control_options['id_base'] );
     370        $this->assertSame( $id_base, $foo_widget->control_options['id_base'] );
    371371
    372372        $id_base         = 'bar';
     
    379379        );
    380380        $bar_widget      = new WP_Widget( $id_base, $name, $widget_options, $control_options );
    381         $this->assertEquals( $widget_options['classname'], $bar_widget->widget_options['classname'] );
    382         $this->assertEquals( $control_options['id_base'], $bar_widget->control_options['id_base'] );
     381        $this->assertSame( $widget_options['classname'], $bar_widget->widget_options['classname'] );
     382        $this->assertSame( $control_options['id_base'], $bar_widget->control_options['id_base'] );
    383383    }
    384384
     
    390390        $widget = new WP_Widget( 'foo', 'Foo' );
    391391        $widget->_set( 2 );
    392         $this->assertEquals( $expected, $widget->get_field_name( $value_to_test ) );
     392        $this->assertSame( $expected, $widget->get_field_name( $value_to_test ) );
    393393    }
    394394
     
    440440        $widget = new WP_Widget( 'foo', 'Foo' );
    441441        $widget->_set( 2 );
    442         $this->assertEquals( $expected, $widget->get_field_id( $value_to_test ) );
     442        $this->assertSame( $expected, $widget->get_field_id( $value_to_test ) );
    443443    }
    444444
     
    536536        $option_value = get_option( 'widget_search' );
    537537        $this->assertArrayHasKey( '_multiwidget', $option_value );
    538         $this->assertEquals( 1, $option_value['_multiwidget'] );
     538        $this->assertSame( 1, $option_value['_multiwidget'] );
    539539        $this->assertArrayHasKey( 2, $option_value );
    540540        $instance = $option_value[2];
     
    546546        delete_option( 'widget_nav_menu' );
    547547        $never_used = get_option( 'widget_nav_menu', array() );
    548         $this->assertEquals( array(), (array) $never_used );
     548        $this->assertSame( array(), (array) $never_used );
    549549
    550550        wp_widgets_init();
     
    556556
    557557        foreach ( $option_value as $widget_number => $instance ) {
    558             $this->assertEquals( $settings[ $widget_number ], $option_value[ $widget_number ] );
     558            $this->assertSame( $settings[ $widget_number ], $option_value[ $widget_number ] );
    559559        }
    560560
    561561        // After widgets_init(), get_settings() should create the widget option.
    562562        $never_used = get_option( 'widget_nav_menu' );
    563         $this->assertEquals( 1, $never_used['_multiwidget'] );
     563        $this->assertSame( 1, $never_used['_multiwidget'] );
    564564        $this->assertArrayNotHasKey( 0, $never_used );
    565565    }
     
    593593        $option_value = get_option( $wp_widget_search->option_name );
    594594        $this->assertArrayHasKey( '_multiwidget', $option_value );
    595         $this->assertEquals( $overridden_title, $option_value[2]['title'] );
     595        $this->assertSame( $overridden_title, $option_value[2]['title'] );
    596596    }
    597597
     
    774774
    775775        $this->assertInternalType( 'array', $result );
    776         $this->assertEquals( $result, $sidebars_widgets );
     776        $this->assertSame( $result, $sidebars_widgets );
    777777
    778778        foreach ( $sidebars_widgets as $widgets ) {
     
    799799
    800800        // Sidebar_widgets option was updated.
    801         $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
     801        $this->assertSame( $sidebars_widgets, wp_get_sidebars_widgets() );
    802802    }
    803803
     
    824824        // $sidebars_widgets matches registered sidebars.
    825825        $this->assertInternalType( 'array', $result );
    826         $this->assertEquals( $result, $sidebars_widgets );
     826        $this->assertSame( $result, $sidebars_widgets );
    827827
    828828        foreach ( $sidebars_widgets as $widgets ) {
     
    863863        $_wp_sidebars_widgets = array();
    864864        $this->assertInternalType( 'array', $result );
    865         $this->assertEquals( $result, $sidebars_widgets );
     865        $this->assertSame( $result, $sidebars_widgets );
    866866
    867867        foreach ( $sidebars_widgets as $widgets ) {
     
    891891
    892892        // Sidebar_widgets option was updated.
    893         $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
     893        $this->assertSame( $sidebars_widgets, wp_get_sidebars_widgets() );
    894894
    895895        // Reset.
     
    906906        $_wp_sidebars_widgets = array();
    907907        $this->assertInternalType( 'array', $result );
    908         $this->assertEquals( $result, $sidebars_widgets );
     908        $this->assertSame( $result, $sidebars_widgets );
    909909
    910910        foreach ( $sidebars_widgets as $widgets ) {
     
    931931
    932932        // Sidebar_widgets option was updated.
    933         $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
     933        $this->assertSame( $sidebars_widgets, wp_get_sidebars_widgets() );
    934934    }
    935935
     
    958958        $_wp_sidebars_widgets = array();
    959959        $this->assertInternalType( 'array', $result );
    960         $this->assertEquals( $result, $sidebars_widgets );
     960        $this->assertSame( $result, $sidebars_widgets );
    961961
    962962        foreach ( $sidebars_widgets as $widgets ) {
     
    10381038
    10391039        // Sidebar_widgets option was updated.
    1040         $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
     1040        $this->assertSame( $sidebars_widgets, wp_get_sidebars_widgets() );
    10411041    }
    10421042
     
    10631063        $this->assertEmpty( $filtered_widgets['fantasy'] );
    10641064        $this->assertArrayHasKey( 'array_version', $filtered_widgets );
    1065         $this->assertEquals( 3, $filtered_widgets['array_version'] );
     1065        $this->assertSame( 3, $filtered_widgets['array_version'] );
    10661066        $this->assertInternalType( 'integer', $filtered_widgets['array_version'] );
    10671067    }
Note: See TracChangeset for help on using the changeset viewer.