Make WordPress Core


Ignore:
Timestamp:
07/05/2021 05:21:53 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Replace assertInternalType() usage in unit tests.

The assertInternalType() and assertNotInternalType() methods are 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:

  • assertIsArray()
  • assertIsBool()
  • assertIsFloat()
  • assertIsInt()
  • assertIsNumeric()
  • assertIsObject()
  • assertIsResource()
  • assertIsString()
  • assertIsScalar()
  • assertIsCallable()
  • assertIsIterable()
  • assertIsNotArray()
  • assertIsNotBool()
  • assertIsNotFloat()
  • assertIsNotInt()
  • assertIsNotNumeric()
  • assertIsNotObject()
  • assertIsNotResource()
  • assertIsNotString()
  • assertIsNotScalar()
  • assertIsNotCallable()
  • assertIsNotIterable()

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

Props pbearne, jrf, dd32, SergeyBiryukov.
Fixes #53491. See #46149.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r50986 r51331  
    3333        static::assertEquals( $expected, $actual, $message, $delta );
    3434    }
     35
     36    /**
     37     * Asserts that a variable is of type array.
     38     *
     39     * This method has been backported from a more recent PHPUnit version,
     40     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     41     *
     42     * @since 5.9.0
     43     *
     44     * @param mixed  $actual  The value to check.
     45     * @param string $message Optional. Message to display when the assertion fails.
     46     */
     47    public static function assertIsArray( $actual, $message = '' ) {
     48        static::assertInternalType( 'array', $actual, $message );
     49    }
     50
     51    /**
     52     * Asserts that a variable is of type bool.
     53     *
     54     * This method has been backported from a more recent PHPUnit version,
     55     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     56     *
     57     * @since 5.9.0
     58     *
     59     * @param mixed  $actual  The value to check.
     60     * @param string $message Optional. Message to display when the assertion fails.
     61     */
     62    public static function assertIsBool( $actual, $message = '' ) {
     63        static::assertInternalType( 'bool', $actual, $message );
     64    }
     65
     66    /**
     67     * Asserts that a variable is of type float.
     68     *
     69     * This method has been backported from a more recent PHPUnit version,
     70     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     71     *
     72     * @since 5.9.0
     73     *
     74     * @param mixed  $actual  The value to check.
     75     * @param string $message Optional. Message to display when the assertion fails.
     76     */
     77    public static function assertIsFloat( $actual, $message = '' ) {
     78        static::assertInternalType( 'float', $actual, $message );
     79    }
     80
     81    /**
     82     * Asserts that a variable is of type int.
     83     *
     84     * This method has been backported from a more recent PHPUnit version,
     85     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     86     *
     87     * @since 5.9.0
     88     *
     89     * @param mixed  $actual  The value to check.
     90     * @param string $message Optional. Message to display when the assertion fails.
     91     */
     92    public static function assertIsInt( $actual, $message = '' ) {
     93        static::assertInternalType( 'int', $actual, $message );
     94    }
     95
     96    /**
     97     * Asserts that a variable is of type numeric.
     98     *
     99     * This method has been backported from a more recent PHPUnit version,
     100     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     101     *
     102     * @since 5.9.0
     103     *
     104     * @param mixed  $actual  The value to check.
     105     * @param string $message Optional. Message to display when the assertion fails.
     106     */
     107    public static function assertIsNumeric( $actual, $message = '' ) {
     108        static::assertInternalType( 'numeric', $actual, $message );
     109    }
     110
     111    /**
     112     * Asserts that a variable is of type object.
     113     *
     114     * This method has been backported from a more recent PHPUnit version,
     115     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     116     *
     117     * @since 5.9.0
     118     *
     119     * @param mixed  $actual  The value to check.
     120     * @param string $message Optional. Message to display when the assertion fails.
     121     */
     122    public static function assertIsObject( $actual, $message = '' ) {
     123        static::assertInternalType( 'object', $actual, $message );
     124    }
     125
     126    /**
     127     * Asserts that a variable is of type resource.
     128     *
     129     * This method has been backported from a more recent PHPUnit version,
     130     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     131     *
     132     * @since 5.9.0
     133     *
     134     * @param mixed  $actual  The value to check.
     135     * @param string $message Optional. Message to display when the assertion fails.
     136     */
     137    public static function assertIsResource( $actual, $message = '' ) {
     138        static::assertInternalType( 'resource', $actual, $message );
     139    }
     140
     141    /**
     142     * Asserts that a variable is of type string.
     143     *
     144     * This method has been backported from a more recent PHPUnit version,
     145     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     146     *
     147     * @since 5.9.0
     148     *
     149     * @param mixed  $actual  The value to check.
     150     * @param string $message Optional. Message to display when the assertion fails.
     151     */
     152    public static function assertIsString( $actual, $message = '' ) {
     153        static::assertInternalType( 'string', $actual, $message );
     154    }
     155
     156    /**
     157     * Asserts that a variable is of type scalar.
     158     *
     159     * This method has been backported from a more recent PHPUnit version,
     160     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     161     *
     162     * @since 5.9.0
     163     *
     164     * @param mixed  $actual  The value to check.
     165     * @param string $message Optional. Message to display when the assertion fails.
     166     */
     167    public static function assertIsScalar( $actual, $message = '' ) {
     168        static::assertInternalType( 'scalar', $actual, $message );
     169    }
     170
     171    /**
     172     * Asserts that a variable is of type callable.
     173     *
     174     * This method has been backported from a more recent PHPUnit version,
     175     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     176     *
     177     * @since 5.9.0
     178     *
     179     * @param mixed  $actual  The value to check.
     180     * @param string $message Optional. Message to display when the assertion fails.
     181     */
     182    public static function assertIsCallable( $actual, $message = '' ) {
     183        static::assertInternalType( 'callable', $actual, $message );
     184    }
     185
     186    /**
     187     * Asserts that a variable is of type iterable.
     188     *
     189     * This method has been backported from a more recent PHPUnit version,
     190     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     191     *
     192     * Support for `iterable` was only added to `Assert::assertNotInternalType()`
     193     * in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
     194     * to that functionality until WordPress test suite requires PHPUnit 7.1.0
     195     * as the minimum version.
     196     *
     197     * @since 5.9.0
     198     *
     199     * @param mixed  $actual  The value to check.
     200     * @param string $message Optional. Message to display when the assertion fails.
     201     */
     202    public static function assertIsIterable( $actual, $message = '' ) {
     203        static::assertTrue( is_iterable( $actual ), $message );
     204    }
     205
     206    /**
     207     * Asserts that a variable is not of type array.
     208     *
     209     * This method has been backported from a more recent PHPUnit version,
     210     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     211     *
     212     * @since 5.9.0
     213     *
     214     * @param mixed  $actual  The value to check.
     215     * @param string $message Optional. Message to display when the assertion fails.
     216     */
     217    public static function assertIsNotArray( $actual, $message = '' ) {
     218        static::assertNotInternalType( 'array', $actual, $message );
     219    }
     220
     221    /**
     222     * Asserts that a variable is not of type bool.
     223     *
     224     * This method has been backported from a more recent PHPUnit version,
     225     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     226     *
     227     * @since 5.9.0
     228     *
     229     * @param mixed  $actual  The value to check.
     230     * @param string $message Optional. Message to display when the assertion fails.
     231     */
     232    public static function assertIsNotBool( $actual, $message = '' ) {
     233        static::assertNotInternalType( 'bool', $actual, $message );
     234    }
     235
     236    /**
     237     * Asserts that a variable is not of type float.
     238     *
     239     * This method has been backported from a more recent PHPUnit version,
     240     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     241     *
     242     * @since 5.9.0
     243     *
     244     * @param mixed  $actual  The value to check.
     245     * @param string $message Optional. Message to display when the assertion fails.
     246     */
     247    public static function assertIsNotFloat( $actual, $message = '' ) {
     248        static::assertNotInternalType( 'float', $actual, $message );
     249    }
     250
     251    /**
     252     * Asserts that a variable is not of type int.
     253     *
     254     * This method has been backported from a more recent PHPUnit version,
     255     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     256     *
     257     * @since 5.9.0
     258     *
     259     * @param mixed  $actual  The value to check.
     260     * @param string $message Optional. Message to display when the assertion fails.
     261     */
     262    public static function assertIsNotInt( $actual, $message = '' ) {
     263        static::assertNotInternalType( 'int', $actual, $message );
     264    }
     265
     266    /**
     267     * Asserts that a variable is not of type numeric.
     268     *
     269     * This method has been backported from a more recent PHPUnit version,
     270     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     271     *
     272     * @since 5.9.0
     273     *
     274     * @param mixed  $actual  The value to check.
     275     * @param string $message Optional. Message to display when the assertion fails.
     276     */
     277    public static function assertIsNotNumeric( $actual, $message = '' ) {
     278        static::assertNotInternalType( 'numeric', $actual, $message );
     279    }
     280
     281    /**
     282     * Asserts that a variable is not of type object.
     283     *
     284     * This method has been backported from a more recent PHPUnit version,
     285     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     286     *
     287     * @since 5.9.0
     288     *
     289     * @param mixed  $actual  The value to check.
     290     * @param string $message Optional. Message to display when the assertion fails.
     291     */
     292    public static function assertIsNotObject( $actual, $message = '' ) {
     293        static::assertNotInternalType( 'object', $actual, $message );
     294    }
     295
     296    /**
     297     * Asserts that a variable is not of type resource.
     298     *
     299     * This method has been backported from a more recent PHPUnit version,
     300     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     301     *
     302     * @since 5.9.0
     303     *
     304     * @param mixed  $actual  The value to check.
     305     * @param string $message Optional. Message to display when the assertion fails.
     306     */
     307    public static function assertIsNotResource( $actual, $message = '' ) {
     308        static::assertNotInternalType( 'resource', $actual, $message );
     309    }
     310
     311    /**
     312     * Asserts that a variable is not of type string.
     313     *
     314     * This method has been backported from a more recent PHPUnit version,
     315     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     316     *
     317     * @since 5.9.0
     318     *
     319     * @param mixed  $actual  The value to check.
     320     * @param string $message Optional. Message to display when the assertion fails.
     321     */
     322    public static function assertIsNotString( $actual, $message = '' ) {
     323        static::assertNotInternalType( 'string', $actual, $message );
     324    }
     325
     326    /**
     327     * Asserts that a variable is not of type scalar.
     328     *
     329     * This method has been backported from a more recent PHPUnit version,
     330     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     331     *
     332     * @since 5.9.0
     333     *
     334     * @param mixed  $actual  The value to check.
     335     * @param string $message Optional. Message to display when the assertion fails.
     336     */
     337    public static function assertIsNotScalar( $actual, $message = '' ) {
     338        static::assertNotInternalType( 'scalar', $actual, $message );
     339    }
     340
     341    /**
     342     * Asserts that a variable is not of type callable.
     343     *
     344     * This method has been backported from a more recent PHPUnit version,
     345     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     346     *
     347     * @since 5.9.0
     348     *
     349     * @param mixed  $actual  The value to check.
     350     * @param string $message Optional. Message to display when the assertion fails.
     351     */
     352    public static function assertIsNotCallable( $actual, $message = '' ) {
     353        static::assertNotInternalType( 'callable', $actual, $message );
     354    }
     355
     356    /**
     357     * Asserts that a variable is not of type iterable.
     358     *
     359     * This method has been backported from a more recent PHPUnit version,
     360     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     361     *
     362     * Support for `iterable` was only added to `Assert::assertNotInternalType()`
     363     * in PHPUnit 7.1.0, so this polyfill cannot use a direct fall-through
     364     * to that functionality until WordPress test suite requires PHPUnit 7.1.0
     365     * as the minimum version.
     366     *
     367     * @since 5.9.0
     368     *
     369     * @param mixed  $actual  The value to check.
     370     * @param string $message Optional. Message to display when the assertion fails.
     371     */
     372    public static function assertIsNotIterable( $actual, $message = '' ) {
     373        static::assertFalse( is_iterable( $actual ), $message );
     374    }
    35375}
Note: See TracChangeset for help on using the changeset viewer.