Making WordPress.org

Changeset 11103


Ignore:
Timestamp:
07/07/2021 10:14:09 PM (5 years ago)
Author:
iandunn
Message:

Patterns API: Test for valid response before accessing body.

Previously tests would fail with unclear error message when the response was a 500 error. This makes it obvious what the problem is.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/patterns/1.0/tests/test-index.php

    r11082 r11103  
    1515     */
    1616    public function assertResponseHasPattern( $response ) {
     17        $this->assertSame( 200, $response->status_code );
     18
    1719        $patterns = json_decode( $response->body );
    18 
    19         $this->assertSame( 200, $response->status_code );
    2020        $this->assertGreaterThan( 0, count( $patterns ) );
    2121        $this->assertIsString( $patterns[0]->title->rendered );
     
    5151     */
    5252    public function test_browse_all_patterns() : void {
    53         $response   = send_request( '/patterns/1.0/' );
     53        $response = send_request( '/patterns/1.0/' );
     54        $this->assertResponseHasPattern( $response );
     55
    5456        $patterns   = json_decode( $response->body );
    5557        $term_slugs = $this->get_term_slugs( $patterns );
    56 
    57         $this->assertResponseHasPattern( $response );
    5858        $this->assertGreaterThan( 1, count( $term_slugs ) );
    5959    }
     
    6767        $button_term_id = 2;
    6868        $response       = send_request( '/patterns/1.0/?pattern-categories=' . $button_term_id );
    69         $patterns       = json_decode( $response->body );
    70         $term_slugs     = $this->get_term_slugs( $patterns );
    71 
    7269        $this->assertResponseHasPattern( $response );
     70
     71        $patterns   = json_decode( $response->body );
     72        $term_slugs = $this->get_term_slugs( $patterns );
    7373        $this->assertSame( array( 'buttons' ), $term_slugs );
    7474    }
     
    9898
    9999        $response = send_request( $query_args );
    100 
    101100        $this->assertResponseHasPattern( $response );
    102101
     
    104103        $found_locales = array_column( $patterns, 'meta' );
    105104        $found_locales = array_column( $found_locales, 'wpop_locale' );
    106 
    107         $this->assertSame( 200, $response->status_code );
    108105
    109106        if ( $expected_locale ) {
     
    198195     */
    199196    public function test_browse_all_categories() : void {
    200         $response   = send_request( '/patterns/1.0/?categories' );
     197        $response = send_request( '/patterns/1.0/?categories' );
     198        $this->assertSame( 200, $response->status_code );
     199
    201200        $categories = json_decode( $response->body );
    202 
    203         $this->assertSame( 200, $response->status_code );
    204201        $this->assertGreaterThan( 0, count( $categories ) );
    205 
    206202        $this->assertIsInt( $categories[0]->id );
    207203        $this->assertIsString( $categories[0]->name );
Note: See TracChangeset for help on using the changeset viewer.