WordPress.org

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/theme.php

    r48858 r48937  
    5050        $themes = wp_get_themes();
    5151        $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_slug ] );
    52         $this->assertEquals( $this->theme_name, $themes[ $this->theme_slug ]->get( 'Name' ) );
     52        $this->assertSame( $this->theme_name, $themes[ $this->theme_slug ]->get( 'Name' ) );
    5353
    5454        $single_theme = wp_get_theme( $this->theme_slug );
    55         $this->assertEquals( $single_theme->get( 'Name' ), $themes[ $this->theme_slug ]->get( 'Name' ) );
     55        $this->assertSame( $single_theme->get( 'Name' ), $themes[ $this->theme_slug ]->get( 'Name' ) );
    5656        $this->assertEquals( $themes[ $this->theme_slug ], $single_theme );
    5757    }
     
    6464        $themes = get_themes();
    6565        $this->assertInstanceOf( 'WP_Theme', $themes[ $this->theme_name ] );
    66         $this->assertEquals( $themes[ $this->theme_name ], get_theme( $this->theme_name ) );
    67 
    68         $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]['Name'] );
    69         $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->Name );
    70         $this->assertEquals( $this->theme_name, $themes[ $this->theme_name ]->name );
     66        $this->assertSame( $themes[ $this->theme_name ], get_theme( $this->theme_name ) );
     67
     68        $this->assertSame( $this->theme_name, $themes[ $this->theme_name ]['Name'] );
     69        $this->assertSame( $this->theme_name, $themes[ $this->theme_name ]->Name );
     70        $this->assertSame( $this->theme_name, $themes[ $this->theme_name ]->name );
    7171    }
    7272
     
    8282            $this->assertFalse( is_array( $theme ) );
    8383            $this->assertInstanceOf( 'WP_Theme', $theme );
    84             $this->assertEquals( $theme, $themes[ $name ] );
     84            $this->assertSame( $theme, $themes[ $name ] );
    8585        }
    8686    }
     
    9393            $_theme = wp_get_theme( $theme->get_stylesheet() );
    9494            // This primes internal WP_Theme caches for the next assertion (headers_sanitized, textdomain_loaded).
    95             $this->assertEquals( $theme->get( 'Name' ), $_theme->get( 'Name' ) );
     95            $this->assertSame( $theme->get( 'Name' ), $_theme->get( 'Name' ) );
    9696            $this->assertEquals( $theme, $_theme );
    9797        }
     
    110110            }
    111111
    112             $this->assertEquals( $theme['Name'], $k );
     112            $this->assertSame( $theme['Name'], $k );
    113113            $this->assertNotEmpty( $theme['Title'] );
    114114
     
    163163            $this->assertTrue( is_dir( $dir . $theme['Stylesheet Dir'] ) );
    164164
    165             $this->assertEquals( 'publish', $theme['Status'] );
     165            $this->assertSame( 'publish', $theme['Status'] );
    166166
    167167            $this->assertTrue( is_file( $dir . $theme['Stylesheet Dir'] . '/' . $theme['Screenshot'] ) );
     
    173173        $theme = wp_get_theme( $this->theme_slug );
    174174
    175         $this->assertEquals( $this->theme_name, $theme->get( 'Name' ) );
     175        $this->assertSame( $this->theme_name, $theme->get( 'Name' ) );
    176176        $this->assertNotEmpty( $theme->get( 'Description' ) );
    177177        $this->assertNotEmpty( $theme->get( 'Author' ) );
     
    179179        $this->assertNotEmpty( $theme->get( 'AuthorURI' ) );
    180180        $this->assertNotEmpty( $theme->get( 'ThemeURI' ) );
    181         $this->assertEquals( $this->theme_slug, $theme->get_stylesheet() );
    182         $this->assertEquals( $this->theme_slug, $theme->get_template() );
    183 
    184         $this->assertEquals( 'publish', $theme->get( 'Status' ) );
    185 
    186         $this->assertEquals( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_stylesheet_directory(), 'get_stylesheet_directory' );
    187         $this->assertEquals( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_template_directory(), 'get_template_directory' );
    188         $this->assertEquals( content_url( 'themes/' . $this->theme_slug ), $theme->get_stylesheet_directory_uri(), 'get_stylesheet_directory_uri' );
    189         $this->assertEquals( content_url( 'themes/' . $this->theme_slug ), $theme->get_template_directory_uri(), 'get_template_directory_uri' );
     181        $this->assertSame( $this->theme_slug, $theme->get_stylesheet() );
     182        $this->assertSame( $this->theme_slug, $theme->get_template() );
     183
     184        $this->assertSame( 'publish', $theme->get( 'Status' ) );
     185
     186        $this->assertSame( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_stylesheet_directory(), 'get_stylesheet_directory' );
     187        $this->assertSame( WP_CONTENT_DIR . '/themes/' . $this->theme_slug, $theme->get_template_directory(), 'get_template_directory' );
     188        $this->assertSame( content_url( 'themes/' . $this->theme_slug ), $theme->get_stylesheet_directory_uri(), 'get_stylesheet_directory_uri' );
     189        $this->assertSame( content_url( 'themes/' . $this->theme_slug ), $theme->get_template_directory_uri(), 'get_template_directory_uri' );
    190190    }
    191191
     
    206206        foreach ( $this->default_themes as $theme ) {
    207207            if ( wp_get_theme( $theme )->exists() ) {
    208                 $this->assertEquals( $theme, wp_get_theme( $theme )->get( 'TextDomain' ) );
     208                $this->assertSame( $theme, wp_get_theme( $theme )->get( 'TextDomain' ) );
    209209            }
    210210        }
     
    228228            preg_match( '#Copyright (\d+) WordPress.org#', $readme, $matches );
    229229            if ( $matches ) {
    230                 $this->assertEquals( $this_year, trim( $matches[1] ), "Bundled themes readme.txt's year needs to be updated to $this_year." );
     230                $this->assertSame( $this_year, trim( $matches[1] ), "Bundled themes readme.txt's year needs to be updated to $this_year." );
    231231            }
    232232
    233233            preg_match( '#Copyright 20\d\d-(\d+) WordPress.org#', $readme, $matches );
    234234            if ( $matches ) {
    235                 $this->assertEquals( $this_year, trim( $matches[1] ), "Bundled themes readme.txt's year needs to be updated to $this_year." );
     235                $this->assertSame( $this_year, trim( $matches[1] ), "Bundled themes readme.txt's year needs to be updated to $this_year." );
    236236            }
    237237        }
     
    278278                }
    279279
    280                 $this->assertEquals( $name, get_current_theme() );
     280                $this->assertSame( $name, get_current_theme() );
    281281
    282282                // Make sure the various get_* functions return the correct values.
    283                 $this->assertEquals( $theme['Template'], get_template() );
    284                 $this->assertEquals( $theme['Stylesheet'], get_stylesheet() );
     283                $this->assertSame( $theme['Template'], get_template() );
     284                $this->assertSame( $theme['Stylesheet'], get_stylesheet() );
    285285
    286286                $root_fs = get_theme_root();
     
    290290                $this->assertTrue( ! empty( $root_uri ) );
    291291
    292                 $this->assertEquals( $root_fs . '/' . get_stylesheet(), get_stylesheet_directory() );
    293                 $this->assertEquals( $root_uri . '/' . get_stylesheet(), get_stylesheet_directory_uri() );
    294                 $this->assertEquals( $root_uri . '/' . get_stylesheet() . '/style.css', get_stylesheet_uri() );
    295                 // $this->assertEquals( $root_uri . '/' . get_stylesheet(), get_locale_stylesheet_uri() );
    296 
    297                 $this->assertEquals( $root_fs . '/' . get_template(), get_template_directory() );
    298                 $this->assertEquals( $root_uri . '/' . get_template(), get_template_directory_uri() );
     292                $this->assertSame( $root_fs . '/' . get_stylesheet(), get_stylesheet_directory() );
     293                $this->assertSame( $root_uri . '/' . get_stylesheet(), get_stylesheet_directory_uri() );
     294                $this->assertSame( $root_uri . '/' . get_stylesheet() . '/style.css', get_stylesheet_uri() );
     295                // $this->assertSame( $root_uri . '/' . get_stylesheet(), get_locale_stylesheet_uri() );
     296
     297                $this->assertSame( $root_fs . '/' . get_template(), get_template_directory() );
     298                $this->assertSame( $root_uri . '/' . get_template(), get_template_directory_uri() );
    299299
    300300                // get_query_template()
    301301
    302302                // Template file that doesn't exist.
    303                 $this->assertEquals( '', get_query_template( rand_str() ) );
     303                $this->assertSame( '', get_query_template( rand_str() ) );
    304304
    305305                // Template files that do exist.
     
    308308                    $file = basename($path, '.php');
    309309                    FIXME: untestable because get_query_template() uses TEMPLATEPATH.
    310                     $this->assertEquals('', get_query_template($file));
     310                    $this->assertSame('', get_query_template($file));
    311311                }
    312312                */
    313313
    314314                // These are kind of tautologies but at least exercise the code.
    315                 $this->assertEquals( get_404_template(), get_query_template( '404' ) );
    316                 $this->assertEquals( get_archive_template(), get_query_template( 'archive' ) );
    317                 $this->assertEquals( get_author_template(), get_query_template( 'author' ) );
    318                 $this->assertEquals( get_category_template(), get_query_template( 'category' ) );
    319                 $this->assertEquals( get_date_template(), get_query_template( 'date' ) );
    320                 $this->assertEquals( get_home_template(), get_query_template( 'home', array( 'home.php', 'index.php' ) ) );
    321                 $this->assertEquals( get_privacy_policy_template(), get_query_template( 'privacy_policy', array( 'privacy-policy.php' ) ) );
    322                 $this->assertEquals( get_page_template(), get_query_template( 'page' ) );
    323                 $this->assertEquals( get_search_template(), get_query_template( 'search' ) );
    324                 $this->assertEquals( get_single_template(), get_query_template( 'single' ) );
    325                 $this->assertEquals( get_attachment_template(), get_query_template( 'attachment' ) );
    326 
    327                 $this->assertEquals( get_tag_template(), get_query_template( 'tag' ) );
     315                $this->assertSame( get_404_template(), get_query_template( '404' ) );
     316                $this->assertSame( get_archive_template(), get_query_template( 'archive' ) );
     317                $this->assertSame( get_author_template(), get_query_template( 'author' ) );
     318                $this->assertSame( get_category_template(), get_query_template( 'category' ) );
     319                $this->assertSame( get_date_template(), get_query_template( 'date' ) );
     320                $this->assertSame( get_home_template(), get_query_template( 'home', array( 'home.php', 'index.php' ) ) );
     321                $this->assertSame( get_privacy_policy_template(), get_query_template( 'privacy_policy', array( 'privacy-policy.php' ) ) );
     322                $this->assertSame( get_page_template(), get_query_template( 'page' ) );
     323                $this->assertSame( get_search_template(), get_query_template( 'search' ) );
     324                $this->assertSame( get_single_template(), get_query_template( 'single' ) );
     325                $this->assertSame( get_attachment_template(), get_query_template( 'attachment' ) );
     326
     327                $this->assertSame( get_tag_template(), get_query_template( 'tag' ) );
    328328
    329329                // nb: This probably doesn't run because WP_INSTALLING is defined.
     
    341341
    342342        $theme = wp_get_theme();
    343         $this->assertEquals( $style, (string) $theme );
     343        $this->assertSame( $style, (string) $theme );
    344344        $this->assertNotFalse( $theme->errors() );
    345345        $this->assertFalse( $theme->exists() );
    346346
    347347        // These return the bogus name - perhaps not ideal behaviour?
    348         $this->assertEquals( $template, get_template() );
    349         $this->assertEquals( $style, get_stylesheet() );
     348        $this->assertSame( $template, get_template() );
     349        $this->assertSame( $style, get_stylesheet() );
    350350    }
    351351
     
    380380            )
    381381        );
    382         $this->assertEquals( get_post( $wp_customize->changeset_post_id() )->post_date, get_post( $nav_created_post_ids[0] )->post_date );
    383         $this->assertEquals( get_post( $wp_customize->changeset_post_id() )->post_date, get_post( $nav_created_post_ids[1] )->post_date );
    384         $this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[0] ) );
    385         $this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[1] ) );
     382        $this->assertSame( get_post( $wp_customize->changeset_post_id() )->post_date, get_post( $nav_created_post_ids[0] )->post_date );
     383        $this->assertSame( get_post( $wp_customize->changeset_post_id() )->post_date, get_post( $nav_created_post_ids[1] )->post_date );
     384        $this->assertSame( 'auto-draft', get_post_status( $nav_created_post_ids[0] ) );
     385        $this->assertSame( 'auto-draft', get_post_status( $nav_created_post_ids[1] ) );
    386386
    387387        // Stubs transition to drafts when changeset is saved as a draft.
     
    392392            )
    393393        );
    394         $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
    395         $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[1] ) );
     394        $this->assertSame( 'draft', get_post_status( $nav_created_post_ids[0] ) );
     395        $this->assertSame( 'draft', get_post_status( $nav_created_post_ids[1] ) );
    396396
    397397        // Status remains unchanged for stub that the user broke out of the changeset.
     
    408408            )
    409409        );
    410         $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
    411         $this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
     410        $this->assertSame( 'draft', get_post_status( $nav_created_post_ids[0] ) );
     411        $this->assertSame( 'private', get_post_status( $nav_created_post_ids[1] ) );
    412412
    413413        // Draft stub is trashed when the changeset is trashed.
    414414        $wp_customize->trash_changeset_post( $wp_customize->changeset_post_id() );
    415         $this->assertEquals( 'trash', get_post_status( $nav_created_post_ids[0] ) );
    416         $this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
     415        $this->assertSame( 'trash', get_post_status( $nav_created_post_ids[0] ) );
     416        $this->assertSame( 'private', get_post_status( $nav_created_post_ids[1] ) );
    417417    }
    418418
     
    453453        $actual = get_registered_theme_feature( 'test-feature' );
    454454
    455         $this->assertEquals( 'array', $actual['type'] );
     455        $this->assertSame( 'array', $actual['type'] );
    456456        $this->assertTrue( $actual['variadic'] );
    457         $this->assertEquals( 'My Feature', $actual['description'] );
    458         $this->assertEquals( array( 'type' => 'string' ), $actual['show_in_rest']['schema']['items'] );
     457        $this->assertSame( 'My Feature', $actual['description'] );
     458        $this->assertSame( array( 'type' => 'string' ), $actual['show_in_rest']['schema']['items'] );
    459459    }
    460460
     
    533533
    534534        $actual = get_registered_theme_feature( 'test-feature' )['show_in_rest']['schema']['type'];
    535         $this->assertEquals( 'array', $actual );
     535        $this->assertSame( 'array', $actual );
    536536    }
    537537
     
    633633
    634634        $this->assertWPError( $registered );
    635         $this->assertEquals( $error_code, $registered->get_error_code() );
     635        $this->assertSame( $error_code, $registered->get_error_code() );
    636636    }
    637637
Note: See TracChangeset for help on using the changeset viewer.