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-pages-controller.php

    r47122 r48937  
    5050        $response = rest_get_server()->dispatch( $request );
    5151        $data     = $response->get_data();
    52         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    53         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     52        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     53        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    5454        // Single.
    5555        $page_id  = $this->factory->post->create( array( 'post_type' => 'page' ) );
     
    5757        $response = rest_get_server()->dispatch( $request );
    5858        $data     = $response->get_data();
    59         $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
    60         $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
     59        $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
     60        $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
    6161    }
    6262
     
    6767        $keys     = array_keys( $data['endpoints'][0]['args'] );
    6868        sort( $keys );
    69         $this->assertEquals(
     69        $this->assertSame(
    7070            array(
    7171                'after',
     
    108108        $response = rest_get_server()->dispatch( $request );
    109109        $data     = $response->get_data();
    110         $this->assertEquals( 1, count( $data ) );
    111         $this->assertEquals( $id1, $data[0]['id'] );
     110        $this->assertSame( 1, count( $data ) );
     111        $this->assertSame( $id1, $data[0]['id'] );
    112112    }
    113113
     
    131131        $response = rest_get_server()->dispatch( $request );
    132132        $data     = $response->get_data();
    133         $this->assertEquals( 2, count( $data ) );
     133        $this->assertSame( 2, count( $data ) );
    134134
    135135        // Filter to parent.
     
    137137        $response = rest_get_server()->dispatch( $request );
    138138        $data     = $response->get_data();
    139         $this->assertEquals( 1, count( $data ) );
    140         $this->assertEquals( $id2, $data[0]['id'] );
     139        $this->assertSame( 1, count( $data ) );
     140        $this->assertSame( $id2, $data[0]['id'] );
    141141
    142142        // Invalid 'parent' should error.
     
    178178        $response = rest_get_server()->dispatch( $request );
    179179        $data     = $response->get_data();
    180         $this->assertEquals( 4, count( $data ) );
     180        $this->assertSame( 4, count( $data ) );
    181181
    182182        // Filter to parents.
     
    184184        $response = rest_get_server()->dispatch( $request );
    185185        $data     = $response->get_data();
    186         $this->assertEquals( 2, count( $data ) );
     186        $this->assertSame( 2, count( $data ) );
    187187        $this->assertEqualSets( array( $id2, $id4 ), wp_list_pluck( $data, 'id' ) );
    188188    }
     
    207207        $response = rest_get_server()->dispatch( $request );
    208208        $data     = $response->get_data();
    209         $this->assertEquals( 2, count( $data ) );
     209        $this->assertSame( 2, count( $data ) );
    210210
    211211        // Filter to parent.
     
    213213        $response = rest_get_server()->dispatch( $request );
    214214        $data     = $response->get_data();
    215         $this->assertEquals( 1, count( $data ) );
    216         $this->assertEquals( $id1, $data[0]['id'] );
     215        $this->assertSame( 1, count( $data ) );
     216        $this->assertSame( $id1, $data[0]['id'] );
    217217
    218218        // Invalid 'parent_exclude' should error.
     
    269269        $response = rest_get_server()->dispatch( $request );
    270270        $data     = $response->get_data();
    271         $this->assertEquals( $id1, $data[0]['id'] );
    272         $this->assertEquals( $id4, $data[1]['id'] );
    273         $this->assertEquals( $id2, $data[2]['id'] );
    274         $this->assertEquals( $id3, $data[3]['id'] );
     271        $this->assertSame( $id1, $data[0]['id'] );
     272        $this->assertSame( $id4, $data[1]['id'] );
     273        $this->assertSame( $id2, $data[2]['id'] );
     274        $this->assertSame( $id3, $data[3]['id'] );
    275275
    276276        // Invalid 'menu_order' should error.
     
    323323        $data     = $response->get_data();
    324324        $this->assertCount( 1, $data );
    325         $this->assertEquals( $draft_id, $data[0]['id'] );
     325        $this->assertSame( $draft_id, $data[0]['id'] );
    326326    }
    327327
     
    359359        $data     = $response->get_data();
    360360        $this->assertCount( 1, $data );
    361         $this->assertEquals( $post2, $data[0]['id'] );
     361        $this->assertSame( $post2, $data[0]['id'] );
    362362    }
    363363
     
    370370        $request  = new WP_REST_Request( 'GET', '/wp/v2/pages/' . $post_id );
    371371        $response = rest_get_server()->dispatch( $request );
    372         $this->assertEquals( 404, $response->get_status() );
     372        $this->assertSame( 404, $response->get_status() );
    373373    }
    374374
     
    391391        $data     = $response->get_data();
    392392        $new_post = get_post( $data['id'] );
    393         $this->assertEquals( 'page-my-test-template.php', $data['template'] );
    394         $this->assertEquals( 'page-my-test-template.php', get_page_template_slug( $new_post->ID ) );
     393        $this->assertSame( 'page-my-test-template.php', $data['template'] );
     394        $this->assertSame( 'page-my-test-template.php', get_page_template_slug( $new_post->ID ) );
    395395    }
    396396
     
    412412        $response = rest_get_server()->dispatch( $request );
    413413
    414         $this->assertEquals( 201, $response->get_status() );
     414        $this->assertSame( 201, $response->get_status() );
    415415
    416416        $links = $response->get_links();
     
    419419        $data     = $response->get_data();
    420420        $new_post = get_post( $data['id'] );
    421         $this->assertEquals( $page_id, $data['parent'] );
    422         $this->assertEquals( $page_id, $new_post->post_parent );
     421        $this->assertSame( $page_id, $data['parent'] );
     422        $this->assertSame( $page_id, $new_post->post_parent );
    423423    }
    424424
     
    455455        $response = rest_get_server()->dispatch( $request );
    456456
    457         $this->assertEquals( 200, $response->get_status() );
     457        $this->assertSame( 200, $response->get_status() );
    458458        $data = $response->get_data();
    459         $this->assertEquals( 'Deleted page', $data['title']['raw'] );
    460         $this->assertEquals( 'trash', $data['status'] );
     459        $this->assertSame( 'Deleted page', $data['title']['raw'] );
     460        $this->assertSame( 'trash', $data['status'] );
    461461    }
    462462
     
    479479        $obj      = get_post( $page_id );
    480480        $response = $endpoint->prepare_item_for_response( $obj, $request );
    481         $this->assertEquals(
     481        $this->assertSame(
    482482            array(
    483483                'id',
     
    505505        $response = rest_get_server()->dispatch( $request );
    506506
    507         $this->assertEquals( 200, $response->get_status() );
     507        $this->assertSame( 200, $response->get_status() );
    508508
    509509        $headers = $response->get_headers();
    510         $this->assertEquals( 8, $headers['X-WP-Total'] );
    511         $this->assertEquals( 2, $headers['X-WP-TotalPages'] );
     510        $this->assertSame( 8, $headers['X-WP-Total'] );
     511        $this->assertSame( 2, $headers['X-WP-TotalPages'] );
    512512
    513513        $all_data = $response->get_data();
    514         $this->assertEquals( 4, count( $all_data ) );
     514        $this->assertSame( 4, count( $all_data ) );
    515515        foreach ( $all_data as $post ) {
    516             $this->assertEquals( 'page', $post['type'] );
     516            $this->assertSame( 'page', $post['type'] );
    517517        }
    518518    }
     
    538538
    539539        $new_data = $response->get_data();
    540         $this->assertEquals( 1, $new_data['menu_order'] );
     540        $this->assertSame( 1, $new_data['menu_order'] );
    541541    }
    542542
     
    562562
    563563        $new_data = $response->get_data();
    564         $this->assertEquals( 0, $new_data['menu_order'] );
     564        $this->assertSame( 0, $new_data['menu_order'] );
    565565    }
    566566
     
    585585        $response = rest_get_server()->dispatch( $request );
    586586        $new_data = $response->get_data();
    587         $this->assertEquals( $page_id1, $new_data['parent'] );
     587        $this->assertSame( $page_id1, $new_data['parent'] );
    588588    }
    589589
     
    609609        $response = rest_get_server()->dispatch( $request );
    610610        $new_data = $response->get_data();
    611         $this->assertEquals( 0, $new_data['parent'] );
     611        $this->assertSame( 0, $new_data['parent'] );
    612612    }
    613613
     
    624624
    625625        $data = $response->get_data();
    626         $this->assertEquals( '', $data['content']['rendered'] );
     626        $this->assertSame( '', $data['content']['rendered'] );
    627627        $this->assertTrue( $data['content']['protected'] );
    628         $this->assertEquals( '', $data['excerpt']['rendered'] );
     628        $this->assertSame( '', $data['excerpt']['rendered'] );
    629629        $this->assertTrue( $data['excerpt']['protected'] );
    630630    }
     
    646646
    647647        $data = $response->get_data();
    648         $this->assertEquals( wpautop( $page->post_content ), $data['content']['rendered'] );
     648        $this->assertSame( wpautop( $page->post_content ), $data['content']['rendered'] );
    649649        $this->assertTrue( $data['content']['protected'] );
    650         $this->assertEquals( wpautop( $page->post_excerpt ), $data['excerpt']['rendered'] );
     650        $this->assertSame( wpautop( $page->post_excerpt ), $data['excerpt']['rendered'] );
    651651        $this->assertTrue( $data['excerpt']['protected'] );
    652652    }
     
    680680        $response = rest_get_server()->dispatch( $request );
    681681        $data     = $response->get_data();
    682         $this->assertEquals( '', $data['content']['rendered'] );
     682        $this->assertSame( '', $data['content']['rendered'] );
    683683        $this->assertTrue( $data['content']['protected'] );
    684         $this->assertEquals( '', $data['excerpt']['rendered'] );
     684        $this->assertSame( '', $data['excerpt']['rendered'] );
    685685        $this->assertTrue( $data['excerpt']['protected'] );
    686686    }
     
    691691        $data       = $response->get_data();
    692692        $properties = $data['schema']['properties'];
    693         $this->assertEquals( 24, count( $properties ) );
     693        $this->assertSame( 24, count( $properties ) );
    694694        $this->assertArrayHasKey( 'author', $properties );
    695695        $this->assertArrayHasKey( 'comment_status', $properties );
Note: See TracChangeset for help on using the changeset viewer.