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-block-type-controller.php

    r48173 r48937  
    9292        $response = rest_get_server()->dispatch( $request );
    9393        $data     = $response->get_data();
    94         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    95         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     94        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     95        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    9696        // Single.
    9797        $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-types/fake/test' );
    9898        $response = rest_get_server()->dispatch( $request );
    9999        $data     = $response->get_data();
    100         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    101         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     100        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     101        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    102102    }
    103103
     
    231231        $response = rest_get_server()->dispatch( $request );
    232232        $data     = $response->get_data();
    233         $this->assertEquals( $block_type, $data['name'] );
    234         $this->assertEquals( '1', $data['title'] );
    235         $this->assertEquals( '1', $data['description'] );
    236         $this->assertEquals( null, $data['icon'] );
    237         $this->assertEquals( null, $data['editor_script'] );
    238         $this->assertEquals( null, $data['script'] );
    239         $this->assertEquals( null, $data['editor_style'] );
    240         $this->assertEquals( null, $data['style'] );
     233        $this->assertSame( $block_type, $data['name'] );
     234        $this->assertSame( '1', $data['title'] );
     235        $this->assertSame( '1', $data['description'] );
     236        $this->assertNull( $data['icon'] );
     237        $this->assertNull( $data['editor_script'] );
     238        $this->assertNull( $data['script'] );
     239        $this->assertNull( $data['editor_style'] );
     240        $this->assertNull( $data['style'] );
    241241        $this->assertEqualSets( array(), $data['provides_context'] );
    242242        $this->assertEqualSets( array(), $data['attributes'] );
     
    246246        $this->assertEqualSets( array(), $data['supports'] );
    247247        $this->assertEqualSets( array(), $data['styles'] );
    248         $this->assertEquals( null, $data['example'] );
    249         $this->assertEquals( null, $data['category'] );
    250         $this->assertEquals( null, $data['textdomain'] );
    251         $this->assertFalse( false, $data['is_dynamic'] );
     248        $this->assertNull( $data['example'] );
     249        $this->assertNull( $data['category'] );
     250        $this->assertNull( $data['textdomain'] );
     251        $this->assertFalse( $data['is_dynamic'] );
    252252    }
    253253
     
    282282        $response = rest_get_server()->dispatch( $request );
    283283        $data     = $response->get_data();
    284         $this->assertEquals( $block_type, $data['name'] );
    285         $this->assertEquals( '', $data['title'] );
    286         $this->assertEquals( '', $data['description'] );
    287         $this->assertEquals( null, $data['icon'] );
    288         $this->assertEquals( null, $data['editor_script'] );
    289         $this->assertEquals( null, $data['script'] );
    290         $this->assertEquals( null, $data['editor_style'] );
    291         $this->assertEquals( null, $data['style'] );
     284        $this->assertSame( $block_type, $data['name'] );
     285        $this->assertSame( '', $data['title'] );
     286        $this->assertSame( '', $data['description'] );
     287        $this->assertNull( $data['icon'] );
     288        $this->assertNull( $data['editor_script'] );
     289        $this->assertNull( $data['script'] );
     290        $this->assertNull( $data['editor_style'] );
     291        $this->assertNull( $data['style'] );
    292292        $this->assertEqualSets( array(), $data['attributes'] );
    293293        $this->assertEqualSets( array(), $data['provides_context'] );
     
    297297        $this->assertEqualSets( array(), $data['supports'] );
    298298        $this->assertEqualSets( array(), $data['styles'] );
    299         $this->assertEquals( null, $data['example'] );
    300         $this->assertEquals( null, $data['category'] );
    301         $this->assertEquals( null, $data['example'] );
    302         $this->assertEquals( null, $data['textdomain'] );
    303         $this->assertFalse( false, $data['is_dynamic'] );
     299        $this->assertNull( $data['example'] );
     300        $this->assertNull( $data['category'] );
     301        $this->assertNull( $data['example'] );
     302        $this->assertNull( $data['textdomain'] );
     303        $this->assertFalse( $data['is_dynamic'] );
    304304    }
    305305
     
    409409        $request->set_param( '_fields', 'name' );
    410410        $response = $endpoint->prepare_item_for_response( $block_type, $request );
    411         $this->assertEquals(
     411        $this->assertSame(
    412412            array(
    413413                'name',
     
    428428    protected function check_block_type_object( $block_type, $data, $links ) {
    429429        // Test data.
    430         $this->assertEquals( $data['attributes'], $block_type->get_attributes() );
    431         $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() );
     430        $this->assertSame( $data['attributes'], $block_type->get_attributes() );
     431        $this->assertSame( $data['is_dynamic'], $block_type->is_dynamic() );
    432432
    433433        $extra_fields = array(
     
    453453        foreach ( $extra_fields as $extra_field ) {
    454454            if ( isset( $block_type->$extra_field ) ) {
    455                 $this->assertEquals( $data[ $extra_field ], $block_type->$extra_field );
     455                $this->assertSame( $data[ $extra_field ], $block_type->$extra_field );
    456456            }
    457457        }
    458458
    459459        // Test links.
    460         $this->assertEquals( rest_url( 'wp/v2/block-types' ), $links['collection'][0]['href'] );
    461         $this->assertEquals( rest_url( 'wp/v2/block-types/' . $block_type->name ), $links['self'][0]['href'] );
     460        $this->assertSame( rest_url( 'wp/v2/block-types' ), $links['collection'][0]['href'] );
     461        $this->assertSame( rest_url( 'wp/v2/block-types/' . $block_type->name ), $links['self'][0]['href'] );
    462462        if ( $block_type->is_dynamic() ) {
    463463            $this->assertArrayHasKey( 'https://api.w.org/render-block', $links );
Note: See TracChangeset for help on using the changeset viewer.