From b9b66b159b5d918cc76fe1592562ab9e6ce2cb79 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 30 Jan 2019 00:53:52 +0000 Subject: [PATCH] Build/Test Tools: Simplify `WP_PHPUnit_Util_Getopt` and update documentation. Introduced in [918/tests] and updated in [26871], the class is there to make sure that known bugs are not skipped when `phpunit` is called with a `--group` that corresponds to an `@ticket` annotation (such as `phpunit --group 12345`). Enforcing recognized arguments or correctly formed arguments is not the concern here, so reusing `PHPUnit_Util_Getopt::parseLongOption()` was superfluous. See #43218. git-svn-id: https://develop.svn.wordpress.org/trunk@44715 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/bootstrap.php | 95 +++------------------------- 1 file changed, 8 insertions(+), 87 deletions(-) diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index 441c029c93..2ec40603ac 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -137,10 +137,7 @@ require dirname( __FILE__ ) . '/class-wp-rest-test-search-handler.php'; require dirname( __FILE__ ) . '/class-wp-fake-block-type.php'; /** - * A child class of the PHP test runner. - * - * Used to access the protected longOptions property, to parse the arguments - * passed to the script. + * A class to handle additional command line arguments passed to the script. * * If it is determined that phpunit was called with a --group that corresponds * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked @@ -150,42 +147,26 @@ require dirname( __FILE__ ) . '/class-wp-fake-block-type.php'; * how you call phpunit has no effect. */ class WP_PHPUnit_Util_Getopt { - protected $longOptions = array( - 'exclude-group=', - 'group=', - ); - function __construct( $argv ) { - array_shift( $argv ); - $options = array(); - while ( current( $argv ) ) { - $arg = current( $argv ); - next( $argv ); - try { - if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) { - self::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv ); - } - } catch ( PHPUnit_Framework_Exception $e ) { - // Enforcing recognized arguments or correctly formed arguments is - // not really the concern here. - continue; - } - } + function __construct( $argv ) { $skipped_groups = array( 'ajax' => true, 'ms-files' => true, 'external-http' => true, ); - foreach ( $options as $option ) { - switch ( $option[0] ) { + while ( current( $argv ) ) { + $option = current( $argv ); + $value = next( $argv ); + + switch ( $option ) { case '--exclude-group': foreach ( $skipped_groups as $group_name => $skipped ) { $skipped_groups[ $group_name ] = false; } continue 2; case '--group': - $groups = explode( ',', $option[1] ); + $groups = explode( ',', $value ); foreach ( $groups as $group ) { if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) { WP_UnitTestCase::forceTicket( $group ); @@ -214,65 +195,5 @@ class WP_PHPUnit_Util_Getopt { } } - /** - * Copied from https://raw.githubusercontent.com/sebastianbergmann/phpunit/6.5.7/src/Util/Getopt.php - * - * @param $arg - * @param $long_options - * @param $opts - * @param $args - */ - protected static function parseLongOption( $arg, $long_options, &$opts, &$args ) { - $count = count( $long_options ); - $list = explode( '=', $arg ); - $opt = $list[0]; - $opt_arg = null; - - if ( count( $list ) > 1 ) { - $opt_arg = $list[1]; - } - - $opt_len = strlen( $opt ); - - for ( $i = 0; $i < $count; $i++ ) { - $long_opt = $long_options[ $i ]; - $opt_start = substr( $long_opt, 0, $opt_len ); - - if ( $opt_start != $opt ) { - continue; - } - - $opt_rest = substr( $long_opt, $opt_len ); - - if ( $opt_rest != '' && $opt[0] != '=' && $i + 1 < $count && - $opt == substr( $long_options[ $i + 1 ], 0, $opt_len ) ) { - throw new Exception( - "option --$opt is ambiguous" - ); - } - - if ( substr( $long_opt, -1 ) == '=' ) { - if ( substr( $long_opt, -2 ) != '==' ) { - if ( ! strlen( $opt_arg ) ) { - if ( false === $opt_arg = current( $args ) ) { - throw new Exception( - "option --$opt requires an argument" - ); - } - next( $args ); - } - } - } elseif ( $opt_arg ) { - throw new Exception( - "option --$opt doesn't allow an argument" - ); - } - - $full_option = '--' . preg_replace( '/={1,2}$/', '', $long_opt ); - $opts[] = array( $full_option, $opt_arg ); - - return; - } - } } new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );