Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (6 months 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/db.php

    r51331 r51462  
    100100
    101101        // Ensure the float isn't 0,700.
    102         $this->assertContains( '0.700', array_pop( $this->_queries ) );
     102        $this->assertStringContainsString( '0.700', array_pop( $this->_queries ) );
    103103
    104104        // Try a prepare.
    105105        $sql = $wpdb->prepare( 'UPDATE test_table SET float_column = %f AND meta_id = %d', 0.7, 5 );
    106         $this->assertContains( '0.700', $sql );
     106        $this->assertStringContainsString( '0.700', $sql );
    107107
    108108        // Restore locale settings.
     
    271271        global $wpdb;
    272272        $sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' );
    273         $this->assertContains( $wpdb->placeholder_escape(), $sql );
     273        $this->assertStringContainsString( $wpdb->placeholder_escape(), $sql );
    274274
    275275        $sql = $wpdb->remove_placeholder_escape( $sql );
     
    16091609
    16101610        $part = $wpdb->prepare( ' AND meta_value = %s', ' %s ' );
    1611         $this->assertNotContains( '%s', $part );
     1611        $this->assertStringNotContainsString( '%s', $part );
    16121612        // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
    16131613        $query = $wpdb->prepare( 'SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s $part', array( 'foo', 'bar' ) );
     
    16261626
    16271627        /* Floats can be right padded, need to assert differently */
    1628         $this->assertContains( ' first=1.1', $actual );
    1629         $this->assertContains( ' second=2.2', $actual );
     1628        $this->assertStringContainsString( ' first=1.1', $actual );
     1629        $this->assertStringContainsString( ' second=2.2', $actual );
    16301630    }
    16311631
     
    16401640
    16411641        /* Floats can be right padded, need to assert differently */
    1642         $this->assertContains( ' first=1.1', $actual );
    1643         $this->assertContains( ' second=2.2', $actual );
     1642        $this->assertStringContainsString( ' first=1.1', $actual );
     1643        $this->assertStringContainsString( ' second=2.2', $actual );
    16441644    }
    16451645
     
    16591659        $wpdb->query( "DROP TABLE {$wpdb->prefix}test_placeholder" );
    16601660
    1661         $this->assertNotContains( '%s', $sql );
     1661        $this->assertStringNotContainsString( '%s', $sql );
    16621662        $this->assertSame( $value, $actual );
    16631663    }
Note: See TracChangeset for help on using the changeset viewer.