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/rest-api/rest-plugins-controller.php

    r48627 r48937  
    108108        $response = rest_get_server()->dispatch( $request );
    109109        $data     = $response->get_data();
    110         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    111         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     110        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     111        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    112112        // Single.
    113113        $request  = new WP_REST_Request( 'OPTIONS', self::BASE . '/' . self::PLUGIN );
    114114        $response = rest_get_server()->dispatch( $request );
    115115        $data     = $response->get_data();
    116         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    117         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     116        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     117        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    118118    }
    119119
     
    126126
    127127        $response = rest_do_request( self::BASE );
    128         $this->assertEquals( 200, $response->get_status() );
     128        $this->assertSame( 200, $response->get_status() );
    129129
    130130        $items = wp_list_filter( $response->get_data(), array( 'plugin' => self::PLUGIN ) );
     
    210210    public function test_get_items_logged_out() {
    211211        $response = rest_do_request( self::BASE );
    212         $this->assertEquals( 401, $response->get_status() );
     212        $this->assertSame( 401, $response->get_status() );
    213213    }
    214214
     
    246246
    247247        $response = rest_do_request( self::BASE );
    248         $this->assertEquals( 200, $response->get_status() );
     248        $this->assertSame( 200, $response->get_status() );
    249249    }
    250250
     
    259259
    260260        $response = rest_do_request( self::BASE );
    261         $this->assertEquals( 200, $response->get_status() );
     261        $this->assertSame( 200, $response->get_status() );
    262262
    263263        $items = wp_list_filter( $response->get_data(), array( 'plugin' => self::PLUGIN ) );
     
    274274
    275275        $response = rest_do_request( self::BASE );
    276         $this->assertEquals( 200, $response->get_status() );
     276        $this->assertSame( 200, $response->get_status() );
    277277
    278278        $items = wp_list_filter( $response->get_data(), array( 'plugin' => self::PLUGIN ) );
     
    291291
    292292        $response = rest_do_request( self::BASE );
    293         $this->assertEquals( 200, $response->get_status() );
     293        $this->assertSame( 200, $response->get_status() );
    294294
    295295        $items = wp_list_filter( $response->get_data(), array( 'plugin' => self::PLUGIN ) );
     
    306306
    307307        $response = rest_do_request( self::BASE . '/' . self::PLUGIN );
    308         $this->assertEquals( 200, $response->get_status() );
     308        $this->assertSame( 200, $response->get_status() );
    309309        $this->check_get_plugin_data( $response->get_data() );
    310310    }
     
    315315    public function test_get_item_logged_out() {
    316316        $response = rest_do_request( self::BASE . '/' . self::PLUGIN );
    317         $this->assertEquals( 401, $response->get_status() );
     317        $this->assertSame( 401, $response->get_status() );
    318318    }
    319319
     
    324324        wp_set_current_user( self::$subscriber_id );
    325325        $response = rest_do_request( self::BASE . '/' . self::PLUGIN );
    326         $this->assertEquals( 403, $response->get_status() );
     326        $this->assertSame( 403, $response->get_status() );
    327327    }
    328328
     
    351351
    352352        $response = rest_do_request( self::BASE . '/' . self::PLUGIN );
    353         $this->assertEquals( 200, $response->get_status() );
     353        $this->assertSame( 200, $response->get_status() );
    354354    }
    355355
     
    360360        wp_set_current_user( self::$super_admin );
    361361        $response = rest_do_request( self::BASE . '/' . self::PLUGIN );
    362         $this->assertEquals( 404, $response->get_status() );
     362        $this->assertSame( 404, $response->get_status() );
    363363    }
    364364
     
    379379        $response = rest_do_request( $request );
    380380        $this->assertNotWPError( $response->as_error() );
    381         $this->assertEquals( 201, $response->get_status() );
    382         $this->assertEquals( 'Link Manager', $response->get_data()['name'] );
     381        $this->assertSame( 201, $response->get_status() );
     382        $this->assertSame( 'Link Manager', $response->get_data()['name'] );
    383383    }
    384384
     
    404404        $response = rest_do_request( $request );
    405405        $this->assertNotWPError( $response->as_error() );
    406         $this->assertEquals( 201, $response->get_status() );
    407         $this->assertEquals( 'Link Manager', $response->get_data()['name'] );
     406        $this->assertSame( 201, $response->get_status() );
     407        $this->assertSame( 'Link Manager', $response->get_data()['name'] );
    408408        $this->assertTrue( is_plugin_active( 'link-manager/link-manager.php' ) );
    409409    }
     
    478478        $response = rest_do_request( $request );
    479479        $this->assertNotWPError( $response->as_error() );
    480         $this->assertEquals( 201, $response->get_status() );
    481         $this->assertEquals( 'Link Manager', $response->get_data()['name'] );
     480        $this->assertSame( 201, $response->get_status() );
     481        $this->assertSame( 'Link Manager', $response->get_data()['name'] );
    482482        $this->assertTrue( is_plugin_active_for_network( 'link-manager/link-manager.php' ) );
    483483    }
     
    491491
    492492        $response = rest_do_request( $request );
    493         $this->assertEquals( 401, $response->get_status() );
     493        $this->assertSame( 401, $response->get_status() );
    494494    }
    495495
     
    503503
    504504        $response = rest_do_request( $request );
    505         $this->assertEquals( 403, $response->get_status() );
     505        $this->assertSame( 403, $response->get_status() );
    506506    }
    507507
     
    562562        $response = rest_do_request( $request );
    563563
    564         $this->assertEquals( 200, $response->get_status() );
     564        $this->assertSame( 200, $response->get_status() );
    565565    }
    566566
     
    572572        $response = rest_do_request( $request );
    573573
    574         $this->assertEquals( 401, $response->get_status() );
     574        $this->assertSame( 401, $response->get_status() );
    575575    }
    576576
     
    584584        $response = rest_do_request( $request );
    585585
    586         $this->assertEquals( 403, $response->get_status() );
     586        $this->assertSame( 403, $response->get_status() );
    587587    }
    588588
     
    612612        $response = rest_do_request( $request );
    613613
    614         $this->assertEquals( 200, $response->get_status() );
     614        $this->assertSame( 200, $response->get_status() );
    615615        $this->assertTrue( is_plugin_active( self::PLUGIN_FILE ) );
    616616    }
     
    658658        $response = rest_do_request( $request );
    659659
    660         $this->assertEquals( 200, $response->get_status() );
     660        $this->assertSame( 200, $response->get_status() );
    661661        $this->assertTrue( is_plugin_active_for_network( self::PLUGIN_FILE ) );
    662662    }
     
    675675        $response = rest_do_request( $request );
    676676
    677         $this->assertEquals( 200, $response->get_status() );
     677        $this->assertSame( 200, $response->get_status() );
    678678        $this->assertTrue( is_plugin_active_for_network( self::PLUGIN_FILE ) );
    679679    }
     
    706706        $response = rest_do_request( $request );
    707707
    708         $this->assertEquals( 200, $response->get_status() );
     708        $this->assertSame( 200, $response->get_status() );
    709709        $this->assertTrue( is_plugin_active_for_network( self::PLUGIN_FILE ) );
    710710    }
     
    722722        $response = rest_do_request( $request );
    723723
    724         $this->assertEquals( 200, $response->get_status() );
     724        $this->assertSame( 200, $response->get_status() );
    725725        $this->assertTrue( is_plugin_active( self::PLUGIN_FILE ) );
    726726    }
     
    740740
    741741        $this->assertNotWPError( $response->as_error() );
    742         $this->assertEquals( 200, $response->get_status() );
     742        $this->assertSame( 200, $response->get_status() );
    743743        $this->assertTrue( is_plugin_active( self::PLUGIN_FILE ) );
    744744    }
     
    772772        $response = rest_do_request( $request );
    773773
    774         $this->assertEquals( 200, $response->get_status() );
     774        $this->assertSame( 200, $response->get_status() );
    775775        $this->assertTrue( is_plugin_inactive( self::PLUGIN_FILE ) );
    776776    }
     
    805805        $response = rest_do_request( $request );
    806806
    807         $this->assertEquals( 200, $response->get_status() );
     807        $this->assertSame( 200, $response->get_status() );
    808808        $this->assertTrue( is_plugin_inactive( self::PLUGIN_FILE ) );
    809809    }
     
    837837
    838838        $this->assertNotWPError( $response->as_error() );
    839         $this->assertEquals( 200, $response->get_status() );
     839        $this->assertSame( 200, $response->get_status() );
    840840        $this->assertTrue( $response->get_data()['deleted'] );
    841         $this->assertEquals( self::PLUGIN, $response->get_data()['previous']['plugin'] );
     841        $this->assertSame( self::PLUGIN, $response->get_data()['previous']['plugin'] );
    842842        $this->assertFileNotExists( WP_PLUGIN_DIR . '/' . self::PLUGIN_FILE );
    843843    }
     
    850850        $response = rest_do_request( $request );
    851851
    852         $this->assertEquals( 401, $response->get_status() );
     852        $this->assertSame( 401, $response->get_status() );
    853853    }
    854854
     
    862862        $response = rest_do_request( $request );
    863863
    864         $this->assertEquals( 403, $response->get_status() );
     864        $this->assertSame( 403, $response->get_status() );
    865865    }
    866866
     
    921921        $links = $response->get_links();
    922922        $this->assertArrayHasKey( 'self', $links );
    923         $this->assertEquals( rest_url( self::BASE . '/' . self::PLUGIN ), $links['self'][0]['href'] );
     923        $this->assertSame( rest_url( self::BASE . '/' . self::PLUGIN ), $links['self'][0]['href'] );
    924924    }
    925925
     
    938938        $response = $endpoint->prepare_item_for_response( $item, new WP_REST_Request( 'GET', self::BASE . '/' . self::PLUGIN ) );
    939939
    940         $this->assertEquals( 'network-active', $response->get_data()['status'] );
     940        $this->assertSame( 'network-active', $response->get_data()['status'] );
    941941    }
    942942
     
    990990     */
    991991    protected function check_get_plugin_data( $data, $network_only = false ) {
    992         $this->assertEquals( 'test-plugin/test-plugin', $data['plugin'] );
    993         $this->assertEquals( '1.5.4', $data['version'] );
    994         $this->assertEquals( 'inactive', $data['status'] );
    995         $this->assertEquals( 'Test Plugin', $data['name'] );
    996         $this->assertEquals( 'https://wordpress.org/plugins/test-plugin/', $data['plugin_uri'] );
    997         $this->assertEquals( 'WordPress.org', $data['author'] );
    998         $this->assertEquals( 'https://wordpress.org/', $data['author_uri'] );
    999         $this->assertEquals( "My 'Cool' Plugin", $data['description']['raw'] );
    1000         $this->assertEquals( 'My &#8216;Cool&#8217; Plugin <cite>By <a href="https://wordpress.org/">WordPress.org</a>.</cite>', $data['description']['rendered'] );
    1001         $this->assertEquals( $network_only, $data['network_only'] );
    1002         $this->assertEquals( '5.6.0', $data['requires_php'] );
    1003         $this->assertEquals( '5.4.0', $data['requires_wp'] );
    1004         $this->assertEquals( 'test-plugin', $data['textdomain'] );
     992        $this->assertSame( 'test-plugin/test-plugin', $data['plugin'] );
     993        $this->assertSame( '1.5.4', $data['version'] );
     994        $this->assertSame( 'inactive', $data['status'] );
     995        $this->assertSame( 'Test Plugin', $data['name'] );
     996        $this->assertSame( 'https://wordpress.org/plugins/test-plugin/', $data['plugin_uri'] );
     997        $this->assertSame( 'WordPress.org', $data['author'] );
     998        $this->assertSame( 'https://wordpress.org/', $data['author_uri'] );
     999        $this->assertSame( "My 'Cool' Plugin", $data['description']['raw'] );
     1000        $this->assertSame( 'My &#8216;Cool&#8217; Plugin <cite>By <a href="https://wordpress.org/">WordPress.org</a>.</cite>', $data['description']['rendered'] );
     1001        $this->assertSame( $network_only, $data['network_only'] );
     1002        $this->assertSame( '5.6.0', $data['requires_php'] );
     1003        $this->assertSame( '5.4.0', $data['requires_wp'] );
     1004        $this->assertSame( 'test-plugin', $data['textdomain'] );
    10051005    }
    10061006
Note: See TracChangeset for help on using the changeset viewer.