Make WordPress Core


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

    r47122 r48937  
    3030        // Validate length is 1 + created due to uncategorized.
    3131        $cat_ids = get_all_category_ids();
    32         $this->assertEquals( 3, count( $cat_ids ) );
     32        $this->assertSame( 3, count( $cat_ids ) );
    3333    }
    3434
     
    5454        // Validate category is returned by slug.
    5555        $ret_testcat = get_category_by_slug( 'testcat' );
    56         $this->assertEquals( $testcat->term_id, $ret_testcat->term_id );
     56        $this->assertSame( $testcat->term_id, $ret_testcat->term_id );
    5757        $ret_testcat = get_category_by_slug( 'TeStCaT' );
    58         $this->assertEquals( $testcat->term_id, $ret_testcat->term_id );
     58        $this->assertSame( $testcat->term_id, $ret_testcat->term_id );
    5959
    6060        // Validate unknown category returns false.
     
    108108
    109109        // Validate compatibility object.
    110         $this->assertEquals( $testcat->cat_ID, $testcat->term_id );
    111         $this->assertEquals( $testcat->category_count, $testcat->count );
    112         $this->assertEquals( $testcat->category_description, $testcat->description );
    113         $this->assertEquals( $testcat->cat_name, $testcat->name );
    114         $this->assertEquals( $testcat->category_nicename, $testcat->slug );
    115         $this->assertEquals( $testcat->category_parent, $testcat->parent );
     110        $this->assertSame( $testcat->cat_ID, $testcat->term_id );
     111        $this->assertSame( $testcat->category_count, $testcat->count );
     112        $this->assertSame( $testcat->category_description, $testcat->description );
     113        $this->assertSame( $testcat->cat_name, $testcat->name );
     114        $this->assertSame( $testcat->category_nicename, $testcat->slug );
     115        $this->assertSame( $testcat->category_parent, $testcat->parent );
    116116
    117117        // Validate compatibility object with parent.
    118         $this->assertEquals( $testcat->cat_ID, $testcat->term_id );
    119         $this->assertEquals( $testcat->category_count, $testcat->count );
    120         $this->assertEquals( $testcat->category_description, $testcat->description );
    121         $this->assertEquals( $testcat->cat_name, $testcat->name );
    122         $this->assertEquals( $testcat->category_nicename, $testcat->slug );
    123         $this->assertEquals( $testcat->category_parent, $testcat->parent );
     118        $this->assertSame( $testcat->cat_ID, $testcat->term_id );
     119        $this->assertSame( $testcat->category_count, $testcat->count );
     120        $this->assertSame( $testcat->category_description, $testcat->description );
     121        $this->assertSame( $testcat->cat_name, $testcat->name );
     122        $this->assertSame( $testcat->category_nicename, $testcat->slug );
     123        $this->assertSame( $testcat->category_parent, $testcat->parent );
    124124
    125125        // Validate compatibility array.
    126         $this->assertEquals( $testcat_array['cat_ID'], $testcat_array['term_id'] );
    127         $this->assertEquals( $testcat_array['category_count'], $testcat_array['count'] );
    128         $this->assertEquals( $testcat_array['category_description'], $testcat_array['description'] );
    129         $this->assertEquals( $testcat_array['cat_name'], $testcat_array['name'] );
    130         $this->assertEquals( $testcat_array['category_nicename'], $testcat_array['slug'] );
    131         $this->assertEquals( $testcat_array['category_parent'], $testcat_array['parent'] );
     126        $this->assertSame( $testcat_array['cat_ID'], $testcat_array['term_id'] );
     127        $this->assertSame( $testcat_array['category_count'], $testcat_array['count'] );
     128        $this->assertSame( $testcat_array['category_description'], $testcat_array['description'] );
     129        $this->assertSame( $testcat_array['cat_name'], $testcat_array['name'] );
     130        $this->assertSame( $testcat_array['category_nicename'], $testcat_array['slug'] );
     131        $this->assertSame( $testcat_array['category_parent'], $testcat_array['parent'] );
    132132
    133133        // Validate compatibility array with parent.
    134         $this->assertEquals( $testcat_array['cat_ID'], $testcat_array['term_id'] );
    135         $this->assertEquals( $testcat_array['category_count'], $testcat_array['count'] );
    136         $this->assertEquals( $testcat_array['category_description'], $testcat_array['description'] );
    137         $this->assertEquals( $testcat_array['cat_name'], $testcat_array['name'] );
    138         $this->assertEquals( $testcat_array['category_nicename'], $testcat_array['slug'] );
    139         $this->assertEquals( $testcat_array['category_parent'], $testcat_array['parent'] );
     134        $this->assertSame( $testcat_array['cat_ID'], $testcat_array['term_id'] );
     135        $this->assertSame( $testcat_array['category_count'], $testcat_array['count'] );
     136        $this->assertSame( $testcat_array['category_description'], $testcat_array['description'] );
     137        $this->assertSame( $testcat_array['cat_name'], $testcat_array['name'] );
     138        $this->assertSame( $testcat_array['category_nicename'], $testcat_array['slug'] );
     139        $this->assertSame( $testcat_array['category_parent'], $testcat_array['parent'] );
    140140    }
    141141
     
    154154
    155155        // Validate.
    156         $this->assertEquals( $testcat->name, get_cat_name( $testcat->term_id ) );
    157         $this->assertEquals( '', get_cat_name( -1 ) );
    158         $this->assertEquals( '', get_cat_name( $testcat->term_id + 100 ) );
     156        $this->assertSame( $testcat->name, get_cat_name( $testcat->term_id ) );
     157        $this->assertSame( '', get_cat_name( -1 ) );
     158        $this->assertSame( '', get_cat_name( $testcat->term_id + 100 ) );
    159159
    160160    }
     
    174174
    175175        // Validate.
    176         $this->assertEquals( $testcat->term_id, get_cat_ID( $testcat->name ) );
    177         $this->assertEquals( 0, get_cat_ID( 'NO CAT' ) );
    178         $this->assertEquals( 0, get_cat_ID( 12 ) );
     176        $this->assertSame( $testcat->term_id, get_cat_ID( $testcat->name ) );
     177        $this->assertSame( 0, get_cat_ID( 'NO CAT' ) );
     178        $this->assertSame( 0, get_cat_ID( 12 ) );
    179179
    180180    }
     
    230230        // Validate full match.
    231231        $ret_cat = get_category_by_path( '/root/level-1', true );
    232         $this->assertEquals( $root_level_id, $ret_cat->term_id );
     232        $this->assertSame( $root_level_id, $ret_cat->term_id );
    233233        $this->assertNull( get_category_by_path( 'level-1', true ) );
    234234        $this->assertNull( get_category_by_path( 'nocat/nocat/', true ) );
     
    236236        // Validate partial match.
    237237        $ret_cat = get_category_by_path( 'level-1', false );
    238         $this->assertEquals( $root_level_id, $ret_cat->term_id );
     238        $this->assertSame( $root_level_id, $ret_cat->term_id );
    239239        $ret_cat = get_category_by_path( 'root/cat/level-1', false );
    240         $this->assertEquals( $root_level_id, $ret_cat->term_id );
     240        $this->assertSame( $root_level_id, $ret_cat->term_id );
    241241        $ret_cat = get_category_by_path( 'root$2Fcat%20%2Flevel-1', false );
    242         $this->assertEquals( $root_level_id, $ret_cat->term_id );
     242        $this->assertSame( $root_level_id, $ret_cat->term_id );
    243243        $this->assertNull( get_category_by_path( 'nocat/nocat/', false ) );
    244244    }
Note: See TracChangeset for help on using the changeset viewer.