WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/06/2021 12:35:01 AM (4 months ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Unify the PHPUnit adapter TestCases.

This commit:

  • Removes the PHPUnit 7 specific TestCase.
  • Removes all existing polyfills from the PHPUnit 5.x TestCase.
  • Imports all polyfill traits from the PHPUnit Polyfills package into the WP_UnitTestCase class and updates the DocBlock to reflect the actual function of the class.
    • Note: The list of polyfills needs to be verified and updated after each new release of the PHPUnit Polyfills package. Alternatively (recommended), one of the built-in TestCase classes from the PHPUnit Polyfills package can be used instead.
  • Moves the require for the WP abstract-testcase.php to the bootstrap.php file.
  • Adds a require_once for the PHPUnit Polyfills autoloader to the bootstrap.php file.
    • Note: while this isn't _strictly_ necessary when the tests are run via Composer, having the include in the bootstrap allows for the tests to also be run via a PHPUnit Phar, providing contributors with more flexibility.

Follow-up to [51559].

Props jrf, hellofromTonya, johnbillion, netweb, SergeyBiryukov.
See #46149.

File:
1 edited

Legend:

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

    r51461 r51560  
    11<?php
    22
    3 require_once __DIR__ . '/abstract-testcase.php';
     3use Yoast\PHPUnitPolyfills\Helpers\AssertAttributeHelper;
     4use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
     5use Yoast\PHPUnitPolyfills\Polyfills\AssertEqualsSpecializations;
     6use Yoast\PHPUnitPolyfills\Polyfills\AssertFileDirectory;
     7use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations;
     8use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames;
     9use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType;
     10use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType;
     11use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals;
     12use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
     13use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
     14use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;
     15use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
     16use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;
     17use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException;
    418
    519/**
    6  * Defines a basic fixture to run multiple tests.
     20 * Basic abstract test class with PHPUnit cross-version adapter layer.
    721 *
    8  * Resets the state of the WordPress installation before and after every test.
     22 * This adapter layer polyfills all PHPUnit assertion and expectation
     23 * methods which were added between PHPUnit 4.8 - 9.5 to allow tests
     24 * to benefit from the full range of available PHPUnit methods.
    925 *
    10  * Includes utility functions and assertions useful for testing WordPress.
     26 * See {@link https://github.com/Yoast/PHPUnit-Polyfills} for full
     27 * documentation on the available polyfills.
    1128 *
    1229 * All WordPress unit tests should inherit from this class.
     
    1431class WP_UnitTestCase extends WP_UnitTestCase_Base {
    1532
    16     /**
    17      * Asserts that two variables are equal (with delta).
    18      *
    19      * This method has been backported from a more recent PHPUnit version,
    20      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    21      *
    22      * @since 5.6.0
    23      *
    24      * @param mixed  $expected First value to compare.
    25      * @param mixed  $actual   Second value to compare.
    26      * @param float  $delta    Allowed numerical distance between two values to consider them equal.
    27      * @param string $message  Optional. Message to display when the assertion fails.
    28      *
    29      * @throws ExpectationFailedException
    30      * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
    31      */
    32     public static function assertEqualsWithDelta( $expected, $actual, $delta, $message = '' ) {
    33         static::assertEquals( $expected, $actual, $message, $delta );
    34     }
    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     }
    375 
    376     /**
    377      * Asserts that a string haystack contains a needle.
    378      *
    379      * This method has been backported from a more recent PHPUnit version,
    380      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    381      *
    382      * @since 5.9.0
    383      *
    384      * @param string $needle   The string to search for.
    385      * @param string $haystack The string to treat as the haystack.
    386      * @param string $message  Optional. Message to display when the assertion fails.
    387      */
    388     public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
    389         static::assertContains( $needle, $haystack, $message );
    390     }
    391 
    392     /**
    393      * Asserts that a string haystack contains a needle (case-insensitive).
    394      *
    395      * This method has been backported from a more recent PHPUnit version,
    396      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    397      *
    398      * @since 5.9.0
    399      *
    400      * @param string $needle   The string to search for.
    401      * @param string $haystack The string to treat as the haystack.
    402      * @param string $message  Optional. Message to display when the assertion fails.
    403      */
    404     public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
    405         static::assertContains( $needle, $haystack, $message, true );
    406     }
    407 
    408     /**
    409      * Asserts that a string haystack does not contain a needle.
    410      *
    411      * This method has been backported from a more recent PHPUnit version,
    412      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    413      *
    414      * @since 5.9.0
    415      *
    416      * @param string $needle   The string to search for.
    417      * @param string $haystack The string to treat as the haystack.
    418      * @param string $message  Optional. Message to display when the assertion fails.
    419      */
    420     public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
    421         static::assertNotContains( $needle, $haystack, $message );
    422     }
    423 
    424     /**
    425      * Asserts that a string haystack does not contain a needle (case-insensitive).
    426      *
    427      * This method has been backported from a more recent PHPUnit version,
    428      * as tests running on PHP 5.6 use PHPUnit 5.7.x.
    429      *
    430      * @since 5.9.0
    431      *
    432      * @param string $needle   The string to search for.
    433      * @param string $haystack The string to treat as the haystack.
    434      * @param string $message  Optional. Message to display when the assertion fails.
    435      */
    436     public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
    437         static::assertNotContains( $needle, $haystack, $message, true );
    438     }
     33    use AssertAttributeHelper;
     34    use AssertClosedResource;
     35    use AssertEqualsSpecializations;
     36    use AssertFileDirectory;
     37    use AssertFileEqualsSpecializations;
     38    use AssertionRenames;
     39    use AssertIsType;
     40    use AssertNumericType;
     41    use AssertObjectEquals;
     42    use AssertStringContains;
     43    use EqualToSpecializations;
     44    use ExpectException;
     45    use ExpectExceptionMessageMatches;
     46    use ExpectExceptionObject;
     47    use ExpectPHPException;
    43948}
Note: See TracChangeset for help on using the changeset viewer.