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

    r46586 r48937  
    9797        $data     = $response->get_data();
    9898
    99         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    100         $this->assertEquals( array( 'view', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
     99        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     100        $this->assertSame( array( 'view', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    101101    }
    102102
     
    111111        );
    112112
    113         $this->assertEquals( 200, $response->get_status() );
     113        $this->assertSame( 200, $response->get_status() );
    114114        $this->assertEqualSets(
    115115            array_merge(
     
    132132        );
    133133
    134         $this->assertEquals( 200, $response->get_status() );
    135         $this->assertEquals( 3, count( $response->get_data() ) );
     134        $this->assertSame( 200, $response->get_status() );
     135        $this->assertSame( 3, count( $response->get_data() ) );
    136136    }
    137137
     
    147147        );
    148148
    149         $this->assertEquals( 200, $response->get_status() );
     149        $this->assertSame( 200, $response->get_status() );
    150150        $this->assertEqualSets(
    151151            array_merge(
     
    170170        );
    171171
    172         $this->assertEquals( 200, $response->get_status() );
     172        $this->assertSame( 200, $response->get_status() );
    173173        $this->assertEqualSets(
    174174            array_merge(
     
    192192        );
    193193
    194         $this->assertEquals( 200, $response->get_status() );
     194        $this->assertSame( 200, $response->get_status() );
    195195        $this->assertEqualSets(
    196196            self::$my_title_page_ids,
     
    240240        );
    241241
    242         $this->assertEquals( 200, $response->get_status() );
     242        $this->assertSame( 200, $response->get_status() );
    243243        $this->assertEqualSets(
    244244            array_merge(
     
    262262        );
    263263
    264         $this->assertEquals( 200, $response->get_status() );
     264        $this->assertSame( 200, $response->get_status() );
    265265        $this->assertEqualSets(
    266266            array_merge(
     
    283283        );
    284284
    285         $this->assertEquals( 200, $response->get_status() );
     285        $this->assertSame( 200, $response->get_status() );
    286286        $this->assertEqualSets(
    287287            self::$my_content_post_ids,
     
    297297        $request  = new WP_REST_Request( 'GET', '/wp/v2/search' . self::$my_title_post_ids[0] );
    298298        $response = rest_get_server()->dispatch( $request );
    299         $this->assertEquals( 404, $response->get_status() );
     299        $this->assertSame( 404, $response->get_status() );
    300300    }
    301301
     
    307307        $request  = new WP_REST_Request( 'POST', '/wp/v2/search' );
    308308        $response = rest_get_server()->dispatch( $request );
    309         $this->assertEquals( 404, $response->get_status() );
     309        $this->assertSame( 404, $response->get_status() );
    310310    }
    311311
     
    317317        $request  = new WP_REST_Request( 'POST', '/wp/v2/search' . self::$my_title_post_ids[0] );
    318318        $response = rest_get_server()->dispatch( $request );
    319         $this->assertEquals( 404, $response->get_status() );
     319        $this->assertSame( 404, $response->get_status() );
    320320    }
    321321
     
    327327        $request  = new WP_REST_Request( 'DELETE', '/wp/v2/search' . self::$my_title_post_ids[0] );
    328328        $response = rest_get_server()->dispatch( $request );
    329         $this->assertEquals( 404, $response->get_status() );
     329        $this->assertSame( 404, $response->get_status() );
    330330    }
    331331
     
    335335    public function test_prepare_item() {
    336336        $response = $this->do_request_with_params();
    337         $this->assertEquals( 200, $response->get_status() );
     337        $this->assertSame( 200, $response->get_status() );
    338338
    339339        $data = $response->get_data();
    340         $this->assertEquals(
     340        $this->assertSame(
    341341            array(
    342342                'id',
     
    364364            )
    365365        );
    366         $this->assertEquals( 200, $response->get_status() );
     366        $this->assertSame( 200, $response->get_status() );
    367367
    368368        $data = $response->get_data();
    369         $this->assertEquals(
     369        $this->assertSame(
    370370            array(
    371371                'id',
     
    449449        $response = $controller->prepare_item_for_response( 1, $request );
    450450        $data     = $response->get_data();
    451         $this->assertEquals(
     451        $this->assertSame(
    452452            array(
    453453                'id',
     
    480480        $response = $controller->prepare_item_for_response( 1, $request );
    481481        $data     = $response->get_data();
    482         $this->assertEquals(
     482        $this->assertSame(
    483483            array(
    484484                'id',
     
    496496
    497497        $params = $controller->get_collection_params();
    498         $this->assertEquals( 'test', $params[ WP_REST_Search_Controller::PROP_TYPE ]['default'] );
     498        $this->assertSame( 'test', $params[ WP_REST_Search_Controller::PROP_TYPE ]['default'] );
    499499        $this->assertEqualSets( array( 'test' ), $params[ WP_REST_Search_Controller::PROP_TYPE ]['enum'] );
    500500        $this->assertEqualSets( array( 'test_first_type', 'test_second_type', WP_REST_Search_Controller::TYPE_ANY ), $params[ WP_REST_Search_Controller::PROP_SUBTYPE ]['items']['enum'] );
     
    512512        $this->assertCount( 1, $data['_embedded']['self'] );
    513513        $this->assertArrayHasKey( WP_REST_Search_Controller::PROP_ID, $data['_embedded']['self'][0] );
    514         $this->assertEquals( $data[ WP_REST_Search_Controller::PROP_ID ], $data['_embedded']['self'][0][ WP_REST_Search_Controller::PROP_ID ] );
     514        $this->assertSame( $data[ WP_REST_Search_Controller::PROP_ID ], $data['_embedded']['self'][0][ WP_REST_Search_Controller::PROP_ID ] );
    515515    }
    516516
Note: See TracChangeset for help on using the changeset viewer.