WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (15 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/customize/nav-menu-setting.php

    r46586 r48937  
    6565
    6666        $setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'nav_menu[123]' );
    67         $this->assertEquals( 'nav_menu', $setting->type );
    68         $this->assertEquals( 'postMessage', $setting->transport );
    69         $this->assertEquals( 123, $setting->term_id );
     67        $this->assertSame( 'nav_menu', $setting->type );
     68        $this->assertSame( 'postMessage', $setting->transport );
     69        $this->assertSame( 123, $setting->term_id );
    7070        $this->assertNull( $setting->previous_term_id );
    7171        $this->assertNull( $setting->update_status );
     
    7575            $this->assertArrayHasKey( $key, $setting->default );
    7676        }
    77         $this->assertEquals( '', $setting->default['name'] );
    78         $this->assertEquals( '', $setting->default['description'] );
    79         $this->assertEquals( 0, $setting->default['parent'] );
     77        $this->assertSame( '', $setting->default['name'] );
     78        $this->assertSame( '', $setting->default['description'] );
     79        $this->assertSame( 0, $setting->default['parent'] );
    8080
    8181        $exception = null;
     
    120120        );
    121121        $setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, 'nav_menu[-5]', compact( 'default' ) );
    122         $this->assertEquals( -5, $setting->term_id );
    123         $this->assertEquals( $default, $setting->default );
     122        $this->assertSame( -5, $setting->term_id );
     123        $this->assertSame( $default, $setting->default );
    124124    }
    125125
     
    154154            $this->assertArrayHasKey( $key, $value );
    155155        }
    156         $this->assertEquals( $menu_name, $value['name'] );
    157         $this->assertEquals( $description, $value['description'] );
    158         $this->assertEquals( $parent_menu_id, $value['parent'] );
     156        $this->assertSame( $menu_name, $value['name'] );
     157        $this->assertSame( $description, $value['description'] );
     158        $this->assertSame( $parent_menu_id, $value['parent'] );
    159159
    160160        $new_menu_name = 'Foo';
    161161        wp_update_nav_menu_object( $menu_id, wp_slash( array( 'menu-name' => $new_menu_name ) ) );
    162162        $updated_value = $setting->value();
    163         $this->assertEquals( $new_menu_name, $updated_value['name'] );
     163        $this->assertSame( $new_menu_name, $updated_value['name'] );
    164164    }
    165165
     
    197197
    198198        $value = $setting->value();
    199         $this->assertEquals( 'Name 1 \\o/', $value['name'] );
    200         $this->assertEquals( 'Description 1 \\o/', $value['description'] );
    201         $this->assertEquals( 0, $value['parent'] );
     199        $this->assertSame( 'Name 1 \\o/', $value['name'] );
     200        $this->assertSame( 'Description 1 \\o/', $value['description'] );
     201        $this->assertSame( 0, $value['parent'] );
    202202
    203203        $term = (array) wp_get_nav_menu_object( $menu_id );
     
    210210        $setting->preview();
    211211        $value = $setting->value();
    212         $this->assertEquals( 'Name 2 \\o/', $value['name'] );
    213         $this->assertEquals( 'Description 2 \\o/', $value['description'] );
    214         $this->assertEquals( 1, $value['parent'] );
     212        $this->assertSame( 'Name 2 \\o/', $value['name'] );
     213        $this->assertSame( 'Description 2 \\o/', $value['description'] );
     214        $this->assertSame( 1, $value['parent'] );
    215215        $term = (array) wp_get_nav_menu_object( $menu_id );
    216216        $this->assertEqualSets( $value, wp_array_slice_assoc( $term, array_keys( $value ) ) );
     
    218218        $menu_object = wp_get_nav_menu_object( $menu_id );
    219219        $this->assertEquals( (object) $term, $menu_object );
    220         $this->assertEquals( $post_value['name'], $menu_object->name );
     220        $this->assertSame( $post_value['name'], $menu_object->name );
    221221
    222222        $nav_menu_options = get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
     
    228228        $this->assertInternalType( 'int', $i, 'Update-previewed menu does not appear in wp_get_nav_menus()' );
    229229        $filtered_menu = $menus[ $i ];
    230         $this->assertEquals( 'Name 2 \\o/', $filtered_menu->name );
     230        $this->assertSame( 'Name 2 \\o/', $filtered_menu->name );
    231231    }
    232232
     
    252252        $setting->preview();
    253253        $value = $setting->value();
    254         $this->assertEquals( $post_value, $value );
     254        $this->assertSame( $post_value, $value );
    255255
    256256        $term = (array) wp_get_nav_menu_object( $menu_id );
     
    258258        $this->assertNotWPError( $term );
    259259        $this->assertEqualSets( $post_value, wp_array_slice_assoc( $term, array_keys( $value ) ) );
    260         $this->assertEquals( $menu_id, $term['term_id'] );
    261         $this->assertEquals( $menu_id, $term['term_taxonomy_id'] );
     260        $this->assertSame( $menu_id, $term['term_id'] );
     261        $this->assertSame( $menu_id, $term['term_taxonomy_id'] );
    262262
    263263        $menu_object = wp_get_nav_menu_object( $menu_id );
    264264        $this->assertEquals( (object) $term, $menu_object );
    265         $this->assertEquals( $post_value['name'], $menu_object->name );
     265        $this->assertSame( $post_value['name'], $menu_object->name );
    266266
    267267        $nav_menu_options = $this->get_nav_menu_items_option();
     
    273273        $this->assertInternalType( 'int', $i, 'Insert-previewed menu was not injected into wp_get_nav_menus()' );
    274274        $filtered_menu = $menus[ $i ];
    275         $this->assertEquals( 'New Menu Name 1 \\o/', $filtered_menu->name );
     275        $this->assertSame( 'New Menu Name 1 \\o/', $filtered_menu->name );
    276276    }
    277277
     
    335335        );
    336336        $sanitized = $setting->sanitize( $value );
    337         $this->assertEquals( 'Hello \\o/ <b>world</b>', $sanitized['name'] );
    338         $this->assertEquals( 'New line \\o/', $sanitized['description'] );
    339         $this->assertEquals( 0, $sanitized['parent'] );
    340         $this->assertEquals( true, $sanitized['auto_add'] );
     337        $this->assertSame( 'Hello \\o/ <b>world</b>', $sanitized['name'] );
     338        $this->assertSame( 'New line \\o/', $sanitized['description'] );
     339        $this->assertSame( 0, $sanitized['parent'] );
     340        $this->assertTrue( $sanitized['auto_add'] );
    341341        $this->assertEqualSets( array( 'name', 'description', 'parent', 'auto_add' ), array_keys( $sanitized ) );
    342342
    343343        $value['name'] = '    '; // Blank spaces.
    344344        $sanitized     = $setting->sanitize( $value );
    345         $this->assertEquals( '(unnamed)', $sanitized['name'] );
     345        $this->assertSame( '(unnamed)', $sanitized['name'] );
    346346    }
    347347
     
    384384        $menu_object = wp_get_nav_menu_object( $menu_id );
    385385        foreach ( array( 'name', 'description', 'parent' ) as $key ) {
    386             $this->assertEquals( $new_value[ $key ], $menu_object->$key );
     386            $this->assertSame( $new_value[ $key ], $menu_object->$key );
    387387        }
    388388        $this->assertEqualSets(
     
    390390            wp_array_slice_assoc( (array) $menu_object, array( 'name', 'description', 'parent' ) )
    391391        );
    392         $this->assertEquals( $new_value, $setting->value() );
     392        $this->assertSame( $new_value, $setting->value() );
    393393
    394394        $save_response = apply_filters( 'customize_save_response', array() );
     
    400400        $this->assertArrayHasKey( 'status', $update_result );
    401401        $this->assertArrayHasKey( 'saved_value', $update_result );
    402         $this->assertEquals( $new_value, $update_result['saved_value'] );
    403 
    404         $this->assertEquals( $menu_id, $update_result['term_id'] );
     402        $this->assertSame( $new_value, $update_result['saved_value'] );
     403
     404        $this->assertSame( $menu_id, $update_result['term_id'] );
    405405        $this->assertNull( $update_result['previous_term_id'] );
    406406        $this->assertNull( $update_result['error'] );
    407         $this->assertEquals( 'updated', $update_result['status'] );
     407        $this->assertSame( 'updated', $update_result['status'] );
    408408
    409409        $nav_menu_options = $this->get_nav_menu_items_option();
     
    434434        $this->assertLessThan( 0, $setting->term_id );
    435435        $setting->save();
    436         $this->assertEquals( $menu_id, $setting->previous_term_id );
     436        $this->assertSame( $menu_id, $setting->previous_term_id );
    437437        $this->assertGreaterThan( 0, $setting->term_id );
    438438
     
    452452        $this->assertArrayHasKey( 'status', $update_result );
    453453        $this->assertArrayHasKey( 'saved_value', $update_result );
    454         $this->assertEquals( $setting->value(), $update_result['saved_value'] );
    455 
    456         $this->assertEquals( $menu->term_id, $update_result['term_id'] );
    457         $this->assertEquals( $menu_id, $update_result['previous_term_id'] );
     454        $this->assertSame( $setting->value(), $update_result['saved_value'] );
     455
     456        $this->assertSame( $menu->term_id, $update_result['term_id'] );
     457        $this->assertSame( $menu_id, $update_result['previous_term_id'] );
    458458        $this->assertNull( $update_result['error'] );
    459         $this->assertEquals( 'inserted', $update_result['status'] );
     459        $this->assertSame( 'inserted', $update_result['status'] );
    460460    }
    461461
     
    479479        $expected_resolved_menu_name = "$menu_name (2)";
    480480        $new_menu                    = wp_get_nav_menu_object( $setting->term_id );
    481         $this->assertEquals( $expected_resolved_menu_name, $new_menu->name );
     481        $this->assertSame( $expected_resolved_menu_name, $new_menu->name );
    482482
    483483        $save_response = apply_filters( 'customize_save_response', array() );
    484         $this->assertEquals( $expected_resolved_menu_name, $save_response['nav_menu_updates'][0]['saved_value']['name'] );
     484        $this->assertSame( $expected_resolved_menu_name, $save_response['nav_menu_updates'][0]['saved_value']['name'] );
    485485    }
    486486
     
    502502
    503503        $menu = wp_get_nav_menu_object( $menu_id );
    504         $this->assertEquals( $menu_name, $menu->name );
     504        $this->assertSame( $menu_name, $menu->name );
    505505
    506506        $this->wp_customize->set_post_value( $setting_id, false );
     
    519519        $this->assertNull( $update_result['saved_value'] );
    520520
    521         $this->assertEquals( $menu_id, $update_result['term_id'] );
     521        $this->assertSame( $menu_id, $update_result['term_id'] );
    522522        $this->assertNull( $update_result['previous_term_id'] );
    523523        $this->assertNull( $update_result['error'] );
    524         $this->assertEquals( 'deleted', $update_result['status'] );
     524        $this->assertSame( 'deleted', $update_result['status'] );
    525525
    526526        $nav_menu_options = $this->get_nav_menu_items_option();
Note: See TracChangeset for help on using the changeset viewer.