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/query/invalidQueries.php

    r51276 r51462  
    9494        $posts = $query->get_posts();
    9595
    96         $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
    97         $this->assertContains( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
     96        $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
     97        $this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
    9898        $this->assertCount( 0, $posts );
    9999    }
     
    114114        $posts = $query->get_posts();
    115115
    116         $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
     116        $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
    117117        $this->assertCount( 1, $posts, 'the valid `page` post type should still return one post' );
    118118    }
     
    128128        $this->go_to( home_url( '?post_type=unregistered_cpt' ) );
    129129
    130         $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
    131         $this->assertContains( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
     130        $this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
     131        $this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
    132132        // $wp_query recovers to the post type "post" and is expected to return one.
    133133        $this->assertCount( 1, $wp_query->get_posts() );
Note: See TracChangeset for help on using the changeset viewer.