Make WordPress Core


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

    r47942 r48937  
    3636
    3737        $this->cache->add( $key, $val );
    38         $this->assertEquals( $val, $this->cache->get( $key ) );
     38        $this->assertSame( $val, $this->cache->get( $key ) );
    3939    }
    4040
     
    4545        // You can store zero in the cache.
    4646        $this->cache->add( $key, $val );
    47         $this->assertEquals( $val, $this->cache->get( $key ) );
     47        $this->assertSame( $val, $this->cache->get( $key ) );
    4848    }
    4949
     
    6464        // Add $key to the cache.
    6565        $this->assertTrue( $this->cache->add( $key, $val1 ) );
    66         $this->assertEquals( $val1, $this->cache->get( $key ) );
     66        $this->assertSame( $val1, $this->cache->get( $key ) );
    6767        // $key is in the cache, so reject new calls to add().
    6868        $this->assertFalse( $this->cache->add( $key, $val2 ) );
    69         $this->assertEquals( $val1, $this->cache->get( $key ) );
     69        $this->assertSame( $val1, $this->cache->get( $key ) );
    7070    }
    7171
     
    7979        $this->assertFalse( $this->cache->get( $key ) );
    8080        $this->assertTrue( $this->cache->add( $key, $val ) );
    81         $this->assertEquals( $val, $this->cache->get( $key ) );
     81        $this->assertSame( $val, $this->cache->get( $key ) );
    8282        $this->assertTrue( $this->cache->replace( $key, $val2 ) );
    83         $this->assertEquals( $val2, $this->cache->get( $key ) );
     83        $this->assertSame( $val2, $this->cache->get( $key ) );
    8484    }
    8585
     
    9191        // memcached accepts set() if the key does not exist.
    9292        $this->assertTrue( $this->cache->set( $key, $val1 ) );
    93         $this->assertEquals( $val1, $this->cache->get( $key ) );
     93        $this->assertSame( $val1, $this->cache->get( $key ) );
    9494        // Second set() with same key should be allowed.
    9595        $this->assertTrue( $this->cache->set( $key, $val2 ) );
    96         $this->assertEquals( $val2, $this->cache->get( $key ) );
     96        $this->assertSame( $val2, $this->cache->get( $key ) );
    9797    }
    9898
     
    109109        $this->cache->add( $key, $val );
    110110        // Item is visible to both cache objects.
    111         $this->assertEquals( $val, $this->cache->get( $key ) );
     111        $this->assertSame( $val, $this->cache->get( $key ) );
    112112        $this->cache->flush();
    113113        // If there is no value get returns false.
     
    123123        $object_a->foo = 'bravo';
    124124        $object_b      = $this->cache->get( $key );
    125         $this->assertEquals( 'alpha', $object_b->foo );
     125        $this->assertSame( 'alpha', $object_b->foo );
    126126        $object_b->foo = 'charlie';
    127         $this->assertEquals( 'bravo', $object_a->foo );
     127        $this->assertSame( 'bravo', $object_a->foo );
    128128
    129129        $key           = __FUNCTION__ . '_2';
     
    133133        $object_a->foo = 'bravo';
    134134        $object_b      = $this->cache->get( $key );
    135         $this->assertEquals( 'alpha', $object_b->foo );
     135        $this->assertSame( 'alpha', $object_b->foo );
    136136        $object_b->foo = 'charlie';
    137         $this->assertEquals( 'bravo', $object_a->foo );
     137        $this->assertSame( 'bravo', $object_a->foo );
    138138    }
    139139
     
    145145        $this->cache->set( $key, 0 );
    146146        $this->cache->incr( $key );
    147         $this->assertEquals( 1, $this->cache->get( $key ) );
     147        $this->assertSame( 1, $this->cache->get( $key ) );
    148148
    149149        $this->cache->incr( $key, 2 );
    150         $this->assertEquals( 3, $this->cache->get( $key ) );
     150        $this->assertSame( 3, $this->cache->get( $key ) );
    151151    }
    152152
     
    158158        wp_cache_set( $key, 0 );
    159159        wp_cache_incr( $key );
    160         $this->assertEquals( 1, wp_cache_get( $key ) );
     160        $this->assertSame( 1, wp_cache_get( $key ) );
    161161
    162162        wp_cache_incr( $key, 2 );
    163         $this->assertEquals( 3, wp_cache_get( $key ) );
     163        $this->assertSame( 3, wp_cache_get( $key ) );
    164164    }
    165165
     
    171171        $this->cache->set( $key, 0 );
    172172        $this->cache->decr( $key );
    173         $this->assertEquals( 0, $this->cache->get( $key ) );
     173        $this->assertSame( 0, $this->cache->get( $key ) );
    174174
    175175        $this->cache->set( $key, 3 );
    176176        $this->cache->decr( $key );
    177         $this->assertEquals( 2, $this->cache->get( $key ) );
     177        $this->assertSame( 2, $this->cache->get( $key ) );
    178178
    179179        $this->cache->decr( $key, 2 );
    180         $this->assertEquals( 0, $this->cache->get( $key ) );
     180        $this->assertSame( 0, $this->cache->get( $key ) );
    181181    }
    182182
     
    191191        wp_cache_set( $key, 0 );
    192192        wp_cache_decr( $key );
    193         $this->assertEquals( 0, wp_cache_get( $key ) );
     193        $this->assertSame( 0, wp_cache_get( $key ) );
    194194
    195195        wp_cache_set( $key, 3 );
    196196        wp_cache_decr( $key );
    197         $this->assertEquals( 2, wp_cache_get( $key ) );
     197        $this->assertSame( 2, wp_cache_get( $key ) );
    198198
    199199        wp_cache_decr( $key, 2 );
    200         $this->assertEquals( 0, wp_cache_get( $key ) );
     200        $this->assertSame( 0, wp_cache_get( $key ) );
    201201    }
    202202
     
    207207        // Verify set.
    208208        $this->assertTrue( $this->cache->set( $key, $val ) );
    209         $this->assertEquals( $val, $this->cache->get( $key ) );
     209        $this->assertSame( $val, $this->cache->get( $key ) );
    210210
    211211        // Verify successful delete.
     
    222222        // Verify set.
    223223        $this->assertTrue( wp_cache_set( $key, $val ) );
    224         $this->assertEquals( $val, wp_cache_get( $key ) );
     224        $this->assertSame( $val, wp_cache_get( $key ) );
    225225
    226226        // Verify successful delete.
     
    247247            // Single site ingnores switch_to_blog().
    248248            $this->assertTrue( $this->cache->set( $key, $val ) );
    249             $this->assertEquals( $val, $this->cache->get( $key ) );
     249            $this->assertSame( $val, $this->cache->get( $key ) );
    250250            $this->cache->switch_to_blog( 999 );
    251             $this->assertEquals( $val, $this->cache->get( $key ) );
     251            $this->assertSame( $val, $this->cache->get( $key ) );
    252252            $this->assertTrue( $this->cache->set( $key, $val2 ) );
    253             $this->assertEquals( $val2, $this->cache->get( $key ) );
     253            $this->assertSame( $val2, $this->cache->get( $key ) );
    254254            $this->cache->switch_to_blog( get_current_blog_id() );
    255             $this->assertEquals( $val2, $this->cache->get( $key ) );
     255            $this->assertSame( $val2, $this->cache->get( $key ) );
    256256        } else {
    257257            // Multisite should have separate per-blog caches.
    258258            $this->assertTrue( $this->cache->set( $key, $val ) );
    259             $this->assertEquals( $val, $this->cache->get( $key ) );
     259            $this->assertSame( $val, $this->cache->get( $key ) );
    260260            $this->cache->switch_to_blog( 999 );
    261261            $this->assertFalse( $this->cache->get( $key ) );
    262262            $this->assertTrue( $this->cache->set( $key, $val2 ) );
    263             $this->assertEquals( $val2, $this->cache->get( $key ) );
     263            $this->assertSame( $val2, $this->cache->get( $key ) );
    264264            $this->cache->switch_to_blog( get_current_blog_id() );
    265             $this->assertEquals( $val, $this->cache->get( $key ) );
     265            $this->assertSame( $val, $this->cache->get( $key ) );
    266266            $this->cache->switch_to_blog( 999 );
    267             $this->assertEquals( $val2, $this->cache->get( $key ) );
     267            $this->assertSame( $val2, $this->cache->get( $key ) );
    268268            $this->cache->switch_to_blog( get_current_blog_id() );
    269             $this->assertEquals( $val, $this->cache->get( $key ) );
     269            $this->assertSame( $val, $this->cache->get( $key ) );
    270270        }
    271271
    272272        // Global group.
    273273        $this->assertTrue( $this->cache->set( $key, $val, 'global-cache-test' ) );
    274         $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) );
     274        $this->assertSame( $val, $this->cache->get( $key, 'global-cache-test' ) );
    275275        $this->cache->switch_to_blog( 999 );
    276         $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) );
     276        $this->assertSame( $val, $this->cache->get( $key, 'global-cache-test' ) );
    277277        $this->assertTrue( $this->cache->set( $key, $val2, 'global-cache-test' ) );
    278         $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) );
     278        $this->assertSame( $val2, $this->cache->get( $key, 'global-cache-test' ) );
    279279        $this->cache->switch_to_blog( get_current_blog_id() );
    280         $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) );
     280        $this->assertSame( $val2, $this->cache->get( $key, 'global-cache-test' ) );
    281281    }
    282282
     
    304304        // Save the first value to cache and verify.
    305305        wp_cache_set( $key, $val1 );
    306         $this->assertEquals( $val1, wp_cache_get( $key ) );
     306        $this->assertSame( $val1, wp_cache_get( $key ) );
    307307
    308308        // Replace the value and verify.
    309309        wp_cache_replace( $key, $val2 );
    310         $this->assertEquals( $val2, wp_cache_get( $key ) );
     310        $this->assertSame( $val2, wp_cache_get( $key ) );
    311311
    312312        // Non-existant key should fail.
Note: See TracChangeset for help on using the changeset viewer.