Make WordPress Core


Ignore:
Timestamp:
01/28/2019 02:10:24 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Add support for PHPUnit 7.x.

  • Create an abstract WP_UnitTestCase_Base class to share between PHPUnit 7.x and older versions.
  • Add a speed-trap loader to determine which SpeedTrapListener class needs to be loaded for the current PHPUnit version.
  • Remove unnecessary PHPUnit\Util\Test and PHPUnit_Util_Getopt inheritances.
  • Update Travis CI config to use PHPUnit 7.x for PHP 7.1, 7.2, and nightly PHP versions.

Props jipmoors, netweb, desrosj, ayeshrajans, soulseekah, SergeyBiryukov.
See #43218.

File:
1 edited

Legend:

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

    r44536 r44701  
    88 */
    99if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
    10     require_once dirname( __FILE__ ) . '/phpunit6-compat.php';
     10    require_once dirname( __FILE__ ) . '/phpunit6/compat.php';
    1111}
    1212
     
    119119_delete_all_posts();
    120120
    121 require dirname( __FILE__ ) . '/testcase.php';
     121if ( version_compare( tests_get_phpunit_version(), '7.0', '>=' ) ) {
     122    require dirname( __FILE__ ) . '/phpunit7/testcase.php';
     123} else {
     124    require dirname( __FILE__ ) . '/testcase.php';
     125}
     126
    122127require dirname( __FILE__ ) . '/testcase-rest-api.php';
    123128require dirname( __FILE__ ) . '/testcase-rest-controller.php';
     
    145150 * how you call phpunit has no effect.
    146151 */
    147 class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
     152class WP_PHPUnit_Util_Getopt {
    148153    protected $longOptions = array(
    149154        'exclude-group=',
    150155        'group=',
     156        'verbose=',
    151157    );
    152158    function __construct( $argv ) {
     
    158164            try {
    159165                if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
    160                     PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
     166                    self::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
    161167                }
    162168            } catch ( PHPUnit_Framework_Exception $e ) {
     
    209215        }
    210216    }
     217
     218    /**
     219     * Copied from https://raw.githubusercontent.com/sebastianbergmann/phpunit/6.5.7/src/Util/Getopt.php
     220     *
     221     * @param $arg
     222     * @param $long_options
     223     * @param $opts
     224     * @param $args
     225     */
     226    protected static function parseLongOption( $arg, $long_options, &$opts, &$args ) {
     227        $count   = count( $long_options );
     228        $list    = explode( '=', $arg );
     229        $opt     = $list[0];
     230        $opt_arg = null;
     231
     232        if ( count( $list ) > 1 ) {
     233            $opt_arg = $list[1];
     234        }
     235
     236        $opt_len = strlen( $opt );
     237
     238        for ( $i = 0; $i < $count; $i++ ) {
     239            $long_opt  = $long_options[ $i ];
     240            $opt_start = substr( $long_opt, 0, $opt_len );
     241
     242            if ( $opt_start != $opt ) {
     243                continue;
     244            }
     245
     246            $opt_rest = substr( $long_opt, $opt_len );
     247
     248            if ( $opt_rest != '' && $opt[0] != '=' && $i + 1 < $count &&
     249                $opt == substr( $long_options[ $i + 1 ], 0, $opt_len ) ) {
     250                throw new Exception(
     251                    "option --$opt is ambiguous"
     252                );
     253            }
     254
     255            if ( substr( $long_opt, -1 ) == '=' ) {
     256                if ( substr( $long_opt, -2 ) != '==' ) {
     257                    if ( ! strlen( $opt_arg ) ) {
     258                        if ( false === $opt_arg = current( $args ) ) {
     259                            throw new Exception(
     260                                "option --$opt requires an argument"
     261                            );
     262                        }
     263                        next( $args );
     264                    }
     265                }
     266            } elseif ( $opt_arg ) {
     267                throw new Exception(
     268                    "option --$opt doesn't allow an argument"
     269                );
     270            }
     271
     272            $full_option = '--' . preg_replace( '/={1,2}$/', '', $long_opt );
     273            $opts[]      = array( $full_option, $opt_arg );
     274
     275            return;
     276        }
     277
     278        throw new Exception( "unrecognized option --$opt" );
     279    }
    211280}
    212281new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
Note: See TracChangeset for help on using the changeset viewer.