Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Replace assertContains() with assertStringContainsString() when used with strings.

Using the assertContains() and assertNotContains() methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

  • assertStringContainsString()
  • assertStringContainsStringIgnoringCase
  • assertStringNotContainsString()
  • assertStringNotContainsStringIgnoringCase

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods were added to the WP_UnitTestCase class for PHPUnit < 7.5.

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r51461 r51462  
    279279        $data        = $response->get_data();
    280280        $first_error = array_shift( $data['data']['params'] );
    281         $this->assertContains( 'page must be greater than or equal to 1', $first_error );
     281        $this->assertStringContainsString( 'page must be greater than or equal to 1', $first_error );
    282282    }
    283283
     
    628628        );
    629629        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    630         $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     630        $this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    631631
    632632        // 3rd page.
     
    647647            rest_url( 'wp/v2/categories' )
    648648        );
    649         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     649        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    650650        $next_link = add_query_arg(
    651651            array(
     
    654654            rest_url( 'wp/v2/categories' )
    655655        );
    656         $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     656        $this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    657657
    658658        // Last page.
     
    670670            rest_url( 'wp/v2/categories' )
    671671        );
    672         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     672        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    673673        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    674674
     
    687687            rest_url( 'wp/v2/categories' )
    688688        );
    689         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     689        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    690690        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    691691    }
     
    811811        $headers = $response->get_headers();
    812812        $data    = $response->get_data();
    813         $this->assertContains( '/wp/v2/categories/' . $data['id'], $headers['Location'] );
     813        $this->assertStringContainsString( '/wp/v2/categories/' . $data['id'], $headers['Location'] );
    814814        $this->assertSame( 'My Awesome Term', $data['name'] );
    815815        $this->assertSame( 'This term is so awesome.', $data['description'] );
     
    12251225
    12261226        $this->assertSameSets( $relations, array_keys( $links ) );
    1227         $this->assertContains( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
     1227        $this->assertStringContainsString( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
    12281228        $this->assertSame( add_query_arg( 'categories', $term->term_id, rest_url( 'wp/v2/posts' ) ), $links['https://api.w.org/post_type'][0]['href'] );
    12291229    }
Note: See TracChangeset for help on using the changeset viewer.