Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 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/rest-api/rest-settings-controller.php

    r48791 r48937  
    6868        $request  = new WP_REST_Request( 'GET', '/wp/v2/settings/title' );
    6969        $response = rest_get_server()->dispatch( $request );
    70         $this->assertEquals( 404, $response->get_status() );
     70        $this->assertSame( 404, $response->get_status() );
    7171    }
    7272
     
    7777        $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    7878        $response = rest_get_server()->dispatch( $request );
    79         $this->assertEquals( 401, $response->get_status() );
     79        $this->assertSame( 401, $response->get_status() );
    8080    }
    8181
     
    8484        $request  = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    8585        $response = rest_get_server()->dispatch( $request );
    86         $this->assertEquals( 403, $response->get_status() );
     86        $this->assertSame( 403, $response->get_status() );
    8787    }
    8888
     
    118118        sort( $actual );
    119119
    120         $this->assertEquals( 200, $response->get_status() );
    121         $this->assertEquals( $expected, $actual );
     120        $this->assertSame( 200, $response->get_status() );
     121        $this->assertSame( $expected, $actual );
    122122    }
    123123
     
    129129        $data     = $response->get_data();
    130130
    131         $this->assertEquals( 200, $response->get_status() );
    132         $this->assertEquals( 1, $data['posts_per_page'] );
     131        $this->assertSame( 200, $response->get_status() );
     132        $this->assertSame( 1, $data['posts_per_page'] );
    133133    }
    134134
     
    155155        $data     = $response->get_data();
    156156
    157         $this->assertEquals( 200, $response->get_status() );
     157        $this->assertSame( 200, $response->get_status() );
    158158        $this->assertArrayHasKey( 'mycustomsettinginrest', $data );
    159         $this->assertEquals( 'validvalue1', $data['mycustomsettinginrest'] );
     159        $this->assertSame( 'validvalue1', $data['mycustomsettinginrest'] );
    160160
    161161        update_option( 'mycustomsetting', 'validvalue2' );
     
    164164        $response = rest_get_server()->dispatch( $request );
    165165        $data     = $response->get_data();
    166         $this->assertEquals( 'validvalue2', $data['mycustomsettinginrest'] );
     166        $this->assertSame( 'validvalue2', $data['mycustomsettinginrest'] );
    167167    }
    168168
     
    191191        $response = rest_get_server()->dispatch( $request );
    192192        $data     = $response->get_data();
    193         $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
     193        $this->assertSame( array( 1, 2 ), $data['mycustomsetting'] );
    194194
    195195        // Empty array works as expected.
     
    198198        $response = rest_get_server()->dispatch( $request );
    199199        $data     = $response->get_data();
    200         $this->assertEquals( array(), $data['mycustomsetting'] );
     200        $this->assertSame( array(), $data['mycustomsetting'] );
    201201
    202202        // Invalid value.
     
    205205        $response = rest_get_server()->dispatch( $request );
    206206        $data     = $response->get_data();
    207         $this->assertEquals( null, $data['mycustomsetting'] );
     207        $this->assertNull( $data['mycustomsetting'] );
    208208
    209209        // No option value.
     
    212212        $response = rest_get_server()->dispatch( $request );
    213213        $data     = $response->get_data();
    214         $this->assertEquals( null, $data['mycustomsetting'] );
     214        $this->assertNull( $data['mycustomsetting'] );
    215215    }
    216216
     
    245245        $response = rest_get_server()->dispatch( $request );
    246246        $data     = $response->get_data();
    247         $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
     247        $this->assertSame( array( 'a' => 1 ), $data['mycustomsetting'] );
    248248
    249249        // Empty array works as expected.
     
    252252        $response = rest_get_server()->dispatch( $request );
    253253        $data     = $response->get_data();
    254         $this->assertEquals( array(), $data['mycustomsetting'] );
     254        $this->assertSame( array(), $data['mycustomsetting'] );
    255255
    256256        // Invalid value.
     
    265265        $response = rest_get_server()->dispatch( $request );
    266266        $data     = $response->get_data();
    267         $this->assertEquals( null, $data['mycustomsetting'] );
     267        $this->assertNull( $data['mycustomsetting'] );
    268268    }
    269269
     
    310310        $data     = $response->get_data();
    311311
    312         $this->assertEquals( 200, $response->get_status() );
     312        $this->assertSame( 200, $response->get_status() );
    313313
    314314        $this->assertArrayHasKey( 'mycustomsettinginrest1', $data );
    315         $this->assertEquals( 'unfiltered1', $data['mycustomsettinginrest1'] );
     315        $this->assertSame( 'unfiltered1', $data['mycustomsettinginrest1'] );
    316316
    317317        $this->assertArrayHasKey( 'mycustomsettinginrest2', $data );
    318         $this->assertEquals( 'unfiltered2', $data['mycustomsettinginrest2'] );
     318        $this->assertSame( 'unfiltered2', $data['mycustomsettinginrest2'] );
    319319
    320320        remove_all_filters( 'rest_pre_get_setting' );
     
    344344        $response = rest_get_server()->dispatch( $request );
    345345        $data     = $response->get_data();
    346         $this->assertEquals( null, $data['mycustomsettinginrest'] );
     346        $this->assertNull( $data['mycustomsettinginrest'] );
    347347    }
    348348
     
    370370        $response = rest_get_server()->dispatch( $request );
    371371        $data     = $response->get_data();
    372         $this->assertEquals( null, $data['mycustomsettinginrest'] );
     372        $this->assertNull( $data['mycustomsettinginrest'] );
    373373    }
    374374
     
    384384        $data     = $response->get_data();
    385385
    386         $this->assertEquals( 200, $response->get_status() );
    387         $this->assertEquals( 'The new title!', $data['title'] );
    388         $this->assertEquals( get_option( 'blogname' ), $data['title'] );
     386        $this->assertSame( 200, $response->get_status() );
     387        $this->assertSame( 'The new title!', $data['title'] );
     388        $this->assertSame( get_option( 'blogname' ), $data['title'] );
    389389    }
    390390
     
    424424        $response = rest_get_server()->dispatch( $request );
    425425        $data     = $response->get_data();
    426         $this->assertEquals( array( 1, 2 ), $data['mycustomsetting'] );
    427         $this->assertEquals( array( 1, 2 ), get_option( 'mycustomsetting' ) );
     426        $this->assertSame( array( 1, 2 ), $data['mycustomsetting'] );
     427        $this->assertSame( array( 1, 2 ), get_option( 'mycustomsetting' ) );
    428428
    429429        // Setting an empty array.
     
    432432        $response = rest_get_server()->dispatch( $request );
    433433        $data     = $response->get_data();
    434         $this->assertEquals( array(), $data['mycustomsetting'] );
    435         $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
     434        $this->assertSame( array(), $data['mycustomsetting'] );
     435        $this->assertSame( array(), get_option( 'mycustomsetting' ) );
    436436
    437437        // Setting an invalid array.
     
    514514        $response = rest_get_server()->dispatch( $request );
    515515        $data     = $response->get_data();
    516         $this->assertEquals( array( 'a' => 1 ), $data['mycustomsetting'] );
    517         $this->assertEquals( array( 'a' => 1 ), get_option( 'mycustomsetting' ) );
     516        $this->assertSame( array( 'a' => 1 ), $data['mycustomsetting'] );
     517        $this->assertSame( array( 'a' => 1 ), get_option( 'mycustomsetting' ) );
    518518
    519519        // Setting an empty object.
     
    522522        $response = rest_get_server()->dispatch( $request );
    523523        $data     = $response->get_data();
    524         $this->assertEquals( array(), $data['mycustomsetting'] );
    525         $this->assertEquals( array(), get_option( 'mycustomsetting' ) );
     524        $this->assertSame( array(), $data['mycustomsetting'] );
     525        $this->assertSame( array(), get_option( 'mycustomsetting' ) );
    526526
    527527        // Provide more keys.
     
    553553        $response = rest_get_server()->dispatch( $request );
    554554        $data     = $response->get_data();
    555         $this->assertEquals( 200, $response->get_status() );
    556         $this->assertEquals( 'The old title!', $data['title'] );
    557         $this->assertEquals( 'The old description!', $data['description'] );
    558         $this->assertEquals( get_option( 'blogname' ), $data['title'] );
    559         $this->assertEquals( get_option( 'blogdescription' ), $data['description'] );
     555        $this->assertSame( 200, $response->get_status() );
     556        $this->assertSame( 'The old title!', $data['title'] );
     557        $this->assertSame( 'The old description!', $data['description'] );
     558        $this->assertSame( get_option( 'blogname' ), $data['title'] );
     559        $this->assertSame( get_option( 'blogdescription' ), $data['description'] );
    560560
    561561        add_filter( 'rest_pre_update_setting', array( $this, 'update_setting_custom_callback' ), 10, 4 );
     
    567567        $data     = $response->get_data();
    568568
    569         $this->assertEquals( 200, $response->get_status() );
    570         $this->assertEquals( 'The old title!', $data['title'] );
    571         $this->assertEquals( 'The new description!', $data['description'] );
    572         $this->assertEquals( get_option( 'blogname' ), $data['title'] );
    573         $this->assertEquals( get_option( 'blogdescription' ), $data['description'] );
     569        $this->assertSame( 200, $response->get_status() );
     570        $this->assertSame( 'The old title!', $data['title'] );
     571        $this->assertSame( 'The new description!', $data['description'] );
     572        $this->assertSame( get_option( 'blogname' ), $data['title'] );
     573        $this->assertSame( get_option( 'blogdescription' ), $data['description'] );
    574574
    575575        remove_all_filters( 'rest_pre_update_setting' );
     
    589589        $request->set_param( 'posts_per_page', 11 );
    590590        $response = rest_get_server()->dispatch( $request );
    591         $this->assertEquals( 200, $response->get_status() );
     591        $this->assertSame( 200, $response->get_status() );
    592592    }
    593593
     
    612612        $data     = $response->get_data();
    613613
    614         $this->assertEquals( 200, $response->get_status() );
    615         $this->assertEquals( 10, $data['posts_per_page'] );
     614        $this->assertSame( 200, $response->get_status() );
     615        $this->assertSame( 10, $data['posts_per_page'] );
    616616    }
    617617
     
    651651        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/settings/title' );
    652652        $response = rest_get_server()->dispatch( $request );
    653         $this->assertEquals( 404, $response->get_status() );
     653        $this->assertSame( 404, $response->get_status() );
    654654    }
    655655
Note: See TracChangeset for help on using the changeset viewer.