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-term-meta-fields.php

    r48402 r48937  
    215215        $response = rest_get_server()->dispatch( $request );
    216216
    217         $this->assertEquals( 200, $response->get_status() );
     217        $this->assertSame( 200, $response->get_status() );
    218218
    219219        $data = $response->get_data();
     
    222222        $meta = (array) $data['meta'];
    223223        $this->assertArrayHasKey( 'test_single', $meta );
    224         $this->assertEquals( 'testvalue', $meta['test_single'] );
     224        $this->assertSame( 'testvalue', $meta['test_single'] );
    225225    }
    226226
     
    233233
    234234        $response = rest_get_server()->dispatch( $request );
    235         $this->assertEquals( 200, $response->get_status() );
     235        $this->assertSame( 200, $response->get_status() );
    236236
    237237        $data = $response->get_data();
     
    245245
    246246        $response = rest_get_server()->dispatch( $request );
    247         $this->assertEquals( 200, $response->get_status() );
     247        $this->assertSame( 200, $response->get_status() );
    248248        $data = $response->get_data();
    249249        $meta = (array) $data['meta'];
     
    260260
    261261        $response = rest_get_server()->dispatch( $request );
    262         $this->assertEquals( 200, $response->get_status() );
     262        $this->assertSame( 200, $response->get_status() );
    263263
    264264        $data = $response->get_data();
     
    275275
    276276        $response = rest_get_server()->dispatch( $request );
    277         $this->assertEquals( 200, $response->get_status() );
     277        $this->assertSame( 200, $response->get_status() );
    278278
    279279        $data = $response->get_data();
     
    290290
    291291        $response = rest_get_server()->dispatch( $request );
    292         $this->assertEquals( 200, $response->get_status() );
     292        $this->assertSame( 200, $response->get_status() );
    293293
    294294        $data = $response->get_data();
     
    337337        $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
    338338        $response = rest_get_server()->dispatch( $request );
    339         $this->assertEquals( 200, $response->get_status() );
     339        $this->assertSame( 200, $response->get_status() );
    340340
    341341        $data = $response->get_data();
     
    352352        $this->assertArrayHasKey( 'test_bool', $meta );
    353353        $this->assertInternalType( 'boolean', $meta['test_bool'] );
    354         $this->assertSame( true, $meta['test_bool'] );
     354        $this->assertTrue( $meta['test_bool'] );
    355355    }
    356356
     
    361361        $response = rest_get_server()->dispatch( $request );
    362362
    363         $this->assertEquals( 200, $response->get_status() );
     363        $this->assertSame( 200, $response->get_status() );
    364364
    365365        $data = $response->get_data();
     
    368368        $meta = (array) $data['meta'];
    369369        $this->assertArrayHasKey( 'new_name', $meta );
    370         $this->assertEquals( 'janet', $meta['new_name'] );
     370        $this->assertSame( 'janet', $meta['new_name'] );
    371371    }
    372372
     
    390390
    391391        $response = rest_get_server()->dispatch( $request );
    392         $this->assertEquals( 200, $response->get_status() );
     392        $this->assertSame( 200, $response->get_status() );
    393393
    394394        $meta = get_term_meta( self::$category_id, 'test_single', false );
    395395        $this->assertNotEmpty( $meta );
    396396        $this->assertCount( 1, $meta );
    397         $this->assertEquals( 'test_value', $meta[0] );
     397        $this->assertSame( 'test_value', $meta[0] );
    398398
    399399        $data = $response->get_data();
    400400        $meta = (array) $data['meta'];
    401401        $this->assertArrayHasKey( 'test_single', $meta );
    402         $this->assertEquals( 'test_value', $meta['test_single'] );
     402        $this->assertSame( 'test_value', $meta['test_single'] );
    403403    }
    404404
     
    409409        // Start with an existing metakey and value.
    410410        $values = update_term_meta( self::$category_id, 'test_single', 'test_value' );
    411         $this->assertEquals( 'test_value', get_term_meta( self::$category_id, 'test_single', true ) );
     411        $this->assertSame( 'test_value', get_term_meta( self::$category_id, 'test_single', true ) );
    412412
    413413        $this->grant_write_permission();
     
    422422
    423423        $response = rest_get_server()->dispatch( $request );
    424         $this->assertEquals( 200, $response->get_status() );
     424        $this->assertSame( 200, $response->get_status() );
    425425
    426426        $meta = get_term_meta( self::$category_id, 'test_single', true );
    427427        $this->assertNotEmpty( $meta );
    428         $this->assertEquals( 'test_value', $meta );
     428        $this->assertSame( 'test_value', $meta );
    429429
    430430        $data = $response->get_data();
    431431        $meta = (array) $data['meta'];
    432432        $this->assertArrayHasKey( 'test_single', $meta );
    433         $this->assertEquals( 'test_value', $meta['test_single'] );
     433        $this->assertSame( 'test_value', $meta['test_single'] );
    434434    }
    435435
     
    540540
    541541        $response = rest_get_server()->dispatch( $request );
    542         $this->assertEquals( 200, $response->get_status() );
     542        $this->assertSame( 200, $response->get_status() );
    543543
    544544        $meta = get_term_meta( self::$category_id, 'test_multi', false );
    545545        $this->assertNotEmpty( $meta );
    546546        $this->assertCount( 1, $meta );
    547         $this->assertEquals( 'val1', $meta[0] );
     547        $this->assertSame( 'val1', $meta[0] );
    548548
    549549        // Add another value.
     
    556556
    557557        $response = rest_get_server()->dispatch( $request );
    558         $this->assertEquals( 200, $response->get_status() );
     558        $this->assertSame( 200, $response->get_status() );
    559559
    560560        $meta = get_term_meta( self::$category_id, 'test_multi', false );
     
    584584
    585585        $response = rest_get_server()->dispatch( $request );
    586         $this->assertEquals( 200, $response->get_status() );
     586        $this->assertSame( 200, $response->get_status() );
    587587
    588588        $meta = get_term_meta( self::$category_id, 'test_multi', false );
     
    691691        $response = rest_get_server()->dispatch( $request );
    692692        $data     = $response->get_data();
    693         $this->assertEquals( 1, $data['meta']['my_meta_key'] );
     693        $this->assertSame( 1, $data['meta']['my_meta_key'] );
    694694    }
    695695
     
    717717        $response = rest_get_server()->dispatch( $request );
    718718        $data     = $response->get_data();
    719         $this->assertEquals( array( 1, 2, 3 ), $data['meta']['my_meta_key'] );
     719        $this->assertSame( array( 1, 2, 3 ), $data['meta']['my_meta_key'] );
    720720    }
    721721
     
    789789
    790790        $response = rest_get_server()->dispatch( $request );
    791         $this->assertEquals( 200, $response->get_status() );
     791        $this->assertSame( 200, $response->get_status() );
    792792
    793793        $meta = get_term_meta( self::$category_id, 'test_custom_schema', false );
     
    818818
    819819        $response = rest_get_server()->dispatch( $request );
    820         $this->assertEquals( 200, $response->get_status() );
     820        $this->assertSame( 200, $response->get_status() );
    821821
    822822        $meta = get_term_meta( self::$category_id, 'test_custom_schema_multi', false );
     
    834834
    835835        $response = rest_get_server()->dispatch( $request );
    836         $this->assertEquals( 200, $response->get_status() );
     836        $this->assertSame( 200, $response->get_status() );
    837837
    838838        $meta = get_term_meta( self::$category_id, 'test_custom_schema_multi', false );
     
    862862
    863863        $response = rest_get_server()->dispatch( $request );
    864         $this->assertEquals( 200, $response->get_status() );
     864        $this->assertSame( 200, $response->get_status() );
    865865
    866866        $meta = get_term_meta( self::$category_id, 'test_custom_name', false );
    867867        $this->assertNotEmpty( $meta );
    868868        $this->assertCount( 1, $meta );
    869         $this->assertEquals( 'janet', $meta[0] );
     869        $this->assertSame( 'janet', $meta[0] );
    870870
    871871        $data = $response->get_data();
    872872        $meta = (array) $data['meta'];
    873873        $this->assertArrayHasKey( 'new_name', $meta );
    874         $this->assertEquals( 'janet', $meta['new_name'] );
     874        $this->assertSame( 'janet', $meta['new_name'] );
    875875    }
    876876
     
    891891
    892892        $response = rest_get_server()->dispatch( $request );
    893         $this->assertEquals( 200, $response->get_status() );
     893        $this->assertSame( 200, $response->get_status() );
    894894
    895895        $meta = get_term_meta( self::$category_id, 'test_custom_name_multi', false );
    896896        $this->assertNotEmpty( $meta );
    897897        $this->assertCount( 1, $meta );
    898         $this->assertEquals( 'janet', $meta[0] );
     898        $this->assertSame( 'janet', $meta[0] );
    899899
    900900        // Add another value.
     
    907907
    908908        $response = rest_get_server()->dispatch( $request );
    909         $this->assertEquals( 200, $response->get_status() );
     909        $this->assertSame( 200, $response->get_status() );
    910910
    911911        $meta = get_term_meta( self::$category_id, 'test_custom_name_multi', false );
     
    939939        // The meta value should not have changed.
    940940        $current_value = get_term_meta( self::$category_id, 'test_single', true );
    941         $this->assertEquals( 'So I tied an onion to my belt, which was the style at the time.', $current_value );
     941        $this->assertSame( 'So I tied an onion to my belt, which was the style at the time.', $current_value );
    942942
    943943        // Ensure the term name update was not processed.
    944944        $term_updated = get_term( self::$category_id );
    945         $this->assertEquals( $term_original->name, $term_updated->name );
     945        $this->assertSame( $term_original->name, $term_updated->name );
    946946    }
    947947
     
    968968        // The meta value should not have changed.
    969969        $current_value = get_term_meta( self::$category_id, 'test_single', true );
    970         $this->assertEquals( 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.', $current_value );
     970        $this->assertSame( 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.', $current_value );
    971971
    972972        // Ensure the term name update was not processed.
    973973        $term_updated = get_term( self::$category_id );
    974         $this->assertEquals( $term_original->name, $term_updated->name );
     974        $this->assertSame( $term_original->name, $term_updated->name );
    975975    }
    976976
     
    978978        add_term_meta( self::$category_id, 'test_multi', 'val1' );
    979979        $values = get_term_meta( self::$category_id, 'test_multi', false );
    980         $this->assertEquals( array( 'val1' ), $values );
     980        $this->assertSame( array( 'val1' ), $values );
    981981
    982982        $this->grant_write_permission();
     
    10091009        add_term_meta( self::$category_id, 'test_single', 'val1' );
    10101010        $current = get_term_meta( self::$category_id, 'test_single', true );
    1011         $this->assertEquals( 'val1', $current );
     1011        $this->assertSame( 'val1', $current );
    10121012
    10131013        $this->grant_write_permission();
     
    10221022
    10231023        $response = rest_get_server()->dispatch( $request );
    1024         $this->assertEquals( 200, $response->get_status() );
     1024        $this->assertSame( 200, $response->get_status() );
    10251025
    10261026        $meta = get_term_meta( self::$category_id, 'test_single', false );
     
    10341034        add_term_meta( self::$category_id, 'test_bad_auth', 'val1' );
    10351035        $current = get_term_meta( self::$category_id, 'test_bad_auth', true );
    1036         $this->assertEquals( 'val1', $current );
     1036        $this->assertSame( 'val1', $current );
    10371037
    10381038        $this->grant_write_permission();
     
    10501050
    10511051        $meta = get_term_meta( self::$category_id, 'test_bad_auth', true );
    1052         $this->assertEquals( 'val1', $meta );
     1052        $this->assertSame( 'val1', $meta );
    10531053    }
    10541054
     
    10591059        add_term_meta( self::$category_id, 'test_single', 'val1' );
    10601060        $current = get_term_meta( self::$category_id, 'test_single', true );
    1061         $this->assertEquals( 'val1', $current );
     1061        $this->assertSame( 'val1', $current );
    10621062
    10631063        $this->grant_write_permission();
     
    10881088        add_term_meta( self::$category_id, 'test_custom_name', 'janet' );
    10891089        $current = get_term_meta( self::$category_id, 'test_custom_name', true );
    1090         $this->assertEquals( 'janet', $current );
     1090        $this->assertSame( 'janet', $current );
    10911091
    10921092        $this->grant_write_permission();
     
    11011101
    11021102        $response = rest_get_server()->dispatch( $request );
    1103         $this->assertEquals( 200, $response->get_status() );
     1103        $this->assertSame( 200, $response->get_status() );
    11041104
    11051105        $meta = get_term_meta( self::$category_id, 'test_custom_name', false );
     
    11181118
    11191119        $this->assertArrayHasKey( 'test_single', $meta_schema );
    1120         $this->assertEquals( 'string', $meta_schema['test_single']['type'] );
     1120        $this->assertSame( 'string', $meta_schema['test_single']['type'] );
    11211121
    11221122        $this->assertArrayHasKey( 'test_multi', $meta_schema );
    1123         $this->assertEquals( 'array', $meta_schema['test_multi']['type'] );
     1123        $this->assertSame( 'array', $meta_schema['test_multi']['type'] );
    11241124        $this->assertArrayHasKey( 'items', $meta_schema['test_multi'] );
    1125         $this->assertEquals( 'string', $meta_schema['test_multi']['items']['type'] );
     1125        $this->assertSame( 'string', $meta_schema['test_multi']['items']['type'] );
    11261126
    11271127        $this->assertArrayHasKey( 'test_custom_schema', $meta_schema );
    1128         $this->assertEquals( 'number', $meta_schema['test_custom_schema']['type'] );
     1128        $this->assertSame( 'number', $meta_schema['test_custom_schema']['type'] );
    11291129
    11301130        $this->assertArrayNotHasKey( 'test_no_rest', $meta_schema );
     
    11531153        $response = rest_get_server()->dispatch( $request );
    11541154
    1155         $this->assertEquals( 200, $response->get_status() );
     1155        $this->assertSame( 200, $response->get_status() );
    11561156
    11571157        $data = $response->get_data();
     
    11671167
    11681168            $this->assertArrayHasKey( $meta_key, $data['meta'] );
    1169             $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
     1169            $this->assertSame( $expected_value, $data['meta'][ $meta_key ] );
    11701170        } else {
    11711171            $this->assertArrayNotHasKey( $meta_key, $data['meta'] );
     
    12131213        $response = rest_get_server()->dispatch( $request );
    12141214        if ( ! $can_write ) {
    1215             $this->assertEquals( 403, $response->get_status() );
     1215            $this->assertSame( 403, $response->get_status() );
    12161216            $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
    12171217            return;
    12181218        }
    12191219
    1220         $this->assertEquals( 200, $response->get_status() );
     1220        $this->assertSame( 200, $response->get_status() );
    12211221
    12221222        $data = $response->get_data();
     
    12301230            }
    12311231
    1232             $this->assertEquals( $expected_value, get_term_meta( $term_id, $meta_key, $single ) );
     1232            $this->assertSame( $expected_value, get_term_meta( $term_id, $meta_key, $single ) );
    12331233            $this->assertArrayHasKey( $meta_key, $data['meta'] );
    1234             $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] );
     1234            $this->assertSame( $expected_value, $data['meta'][ $meta_key ] );
    12351235        } else {
    12361236            $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) );
     
    12761276        $response = rest_get_server()->dispatch( $request );
    12771277
    1278         $this->assertEquals( 200, $response->get_status() );
     1278        $this->assertSame( 200, $response->get_status() );
    12791279
    12801280        $data = $response->get_data();
Note: See TracChangeset for help on using the changeset viewer.