Make WordPress Core

Changeset 46127


Ignore:
Timestamp:
09/15/2019 11:03:45 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Code Modernisation: Introduce the spread operator in tests/phpunit/*.

Rather than relying func_get_args() to retrieve arbitrary function arguments, we can now use the spread operator to assign them directly to a variable.

Props jrf.
See #47678.

Location:
trunk/tests/phpunit
Files:
4 edited

Legend:

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

    r45588 r46127  
    904904     *
    905905     * Any properties that are listed by name as parameters will be expected to be true; all others are
    906      * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single()
     906     * expected to be false. For example, assertQueryTrue( 'is_single', 'is_feed' ) means is_single()
    907907     * and is_feed() must be true and everything else must be false to pass.
    908908     *
     
    912912     * @param string ...$prop Any number of WP_Query properties that are expected to be true for the current request.
    913913     */
    914     public function assertQueryTrue() {
     914    public function assertQueryTrue( ...$prop ) {
    915915        global $wp_query;
    916         $all  = array(
     916
     917        $all = array(
    917918            'is_404',
    918919            'is_admin',
     
    945946            'is_year',
    946947        );
    947         $true = func_get_args();
    948 
    949         foreach ( $true as $true_thing ) {
     948
     949        foreach ( $prop as $true_thing ) {
    950950            $this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." );
    951951        }
     
    957957            $result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
    958958
    959             if ( in_array( $query_thing, $true, true ) ) {
     959            if ( in_array( $query_thing, $prop, true ) ) {
    960960                if ( ! $result ) {
    961961                    $message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
  • trunk/tests/phpunit/includes/utils.php

    r45607 r46127  
    129129    }
    130130
    131     function filterall( $tag, $arg = null ) {
     131    function filterall( $tag, ...$args ) {
    132132        // this one doesn't return the result, so it's safe to use with the new 'all' filter
    133133        if ( $this->debug ) {
     
    135135        }
    136136
    137         $args           = func_get_args();
    138137        $this->events[] = array(
    139138            'filter' => __FUNCTION__,
    140139            'tag'    => $tag,
    141             'args'   => array_slice( $args, 1 ),
     140            'args'   => $args,
    142141        );
    143142    }
     
    245244}
    246245
    247 function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) {
    248     $a   = func_get_args();
    249     $a   = array_slice( $a, 1 );
    250     $n   = count( $a );
     246function xml_find( $tree, ...$elements ) {
     247    $n   = count( $elements );
    251248    $out = array();
    252249
     
    256253
    257254    for ( $i = 0; $i < count( $tree ); $i++ ) {
    258         #       echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n";
    259         #       var_dump($tree[$i]['name'], $a[0]);
    260         if ( $tree[ $i ]['name'] === $a[0] ) {
     255        #       echo "checking '{$tree[$i][name]}' == '{$elements[0]}'\n";
     256        #       var_dump( $tree[$i]['name'], $elements[0] );
     257        if ( $tree[ $i ]['name'] === $elements[0] ) {
    261258            #           echo "n == {$n}\n";
    262259            if ( 1 === $n ) {
    263260                $out[] = $tree[ $i ];
    264261            } else {
    265                 $subtree   =& $tree[ $i ]['child'];
    266                 $call_args = array( $subtree );
    267                 $call_args = array_merge( $call_args, array_slice( $a, 1 ) );
    268                 $out       = array_merge( $out, call_user_func_array( 'xml_find', $call_args ) );
     262                $subtree =& $tree[ $i ]['child'];
     263                $out     = array_merge( $out, xml_find( $subtree, ...array_slice( $elements, 1 ) ) );
    269264            }
    270265        }
     
    301296}
    302297
    303 function dmp() {
    304     $args = func_get_args();
    305 
     298function dmp( ...$args ) {
    306299    foreach ( $args as $thing ) {
    307300        echo ( is_scalar( $thing ) ? strval( $thing ) : var_export( $thing, true ) ), "\n";
  • trunk/tests/phpunit/tests/customize/manager.php

    r45607 r46127  
    25422542     *
    25432543     * @see Tests_WP_Customize_Manager::test_set_post_value()
    2544      */
    2545     function capture_customize_post_value_set_actions() {
     2544     *
     2545     * @param mixed ...$args Optional arguments passed to the action.
     2546     */
     2547    function capture_customize_post_value_set_actions( ...$args ) {
    25462548        $action = current_action();
    2547         $args   = func_get_args();
    25482549        $this->captured_customize_post_value_set_actions[] = compact( 'action', 'args' );
    25492550    }
  • trunk/tests/phpunit/tests/hooks/doAction.php

    r42343 r46127  
    165165    /**
    166166     * Use this rather than MockAction so we can test callbacks with no args
     167     *
     168     * @param mixed ...$args Optional arguments passed to the action.
    167169     */
    168     public function _action_callback() {
    169         $args           = func_get_args();
     170    public function _action_callback( ...$args ) {
    170171        $this->events[] = array(
    171172            'action' => __FUNCTION__,
Note: See TracChangeset for help on using the changeset viewer.