From 3ae54e84daf502eefc88aa788c6d67f3c1e9ef52 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 15 Sep 2019 11:03:45 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@46127 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 12 ++++----- tests/phpunit/includes/utils.php | 27 ++++++++------------ tests/phpunit/tests/customize/manager.php | 5 ++-- tests/phpunit/tests/hooks/doAction.php | 5 ++-- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 0dbcc7f45a..f1c08657cb 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -903,7 +903,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Framework_TestCase { * Checks each of the WP_Query is_* functions/properties against expected boolean value. * * Any properties that are listed by name as parameters will be expected to be true; all others are - * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single() + * expected to be false. For example, assertQueryTrue( 'is_single', 'is_feed' ) means is_single() * and is_feed() must be true and everything else must be false to pass. * * @since 2.5.0 @@ -911,9 +911,10 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Framework_TestCase { * * @param string ...$prop Any number of WP_Query properties that are expected to be true for the current request. */ - public function assertQueryTrue() { + public function assertQueryTrue( ...$prop ) { global $wp_query; - $all = array( + + $all = array( 'is_404', 'is_admin', 'is_archive', @@ -944,9 +945,8 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Framework_TestCase { 'is_trackback', 'is_year', ); - $true = func_get_args(); - foreach ( $true as $true_thing ) { + foreach ( $prop as $true_thing ) { $this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." ); } @@ -956,7 +956,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Framework_TestCase { foreach ( $all as $query_thing ) { $result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing; - if ( in_array( $query_thing, $true, true ) ) { + if ( in_array( $query_thing, $prop, true ) ) { if ( ! $result ) { $message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL; $passed = false; diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php index f2131ac23f..a213bce0d0 100644 --- a/tests/phpunit/includes/utils.php +++ b/tests/phpunit/includes/utils.php @@ -128,17 +128,16 @@ class MockAction { return $arg . '_append'; } - function filterall( $tag, $arg = null ) { + function filterall( $tag, ...$args ) { // this one doesn't return the result, so it's safe to use with the new 'all' filter if ( $this->debug ) { dmp( __FUNCTION__, $this->current_filter() ); } - $args = func_get_args(); $this->events[] = array( 'filter' => __FUNCTION__, 'tag' => $tag, - 'args' => array_slice( $args, 1 ), + 'args' => $args, ); } @@ -244,10 +243,8 @@ function xml_to_array( $in ) { return $p->data; } -function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) { - $a = func_get_args(); - $a = array_slice( $a, 1 ); - $n = count( $a ); +function xml_find( $tree, ...$elements ) { + $n = count( $elements ); $out = array(); if ( $n < 1 ) { @@ -255,17 +252,15 @@ function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) { } for ( $i = 0; $i < count( $tree ); $i++ ) { - # echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n"; - # var_dump($tree[$i]['name'], $a[0]); - if ( $tree[ $i ]['name'] === $a[0] ) { + # echo "checking '{$tree[$i][name]}' == '{$elements[0]}'\n"; + # var_dump( $tree[$i]['name'], $elements[0] ); + if ( $tree[ $i ]['name'] === $elements[0] ) { # echo "n == {$n}\n"; if ( 1 === $n ) { $out[] = $tree[ $i ]; } else { - $subtree =& $tree[ $i ]['child']; - $call_args = array( $subtree ); - $call_args = array_merge( $call_args, array_slice( $a, 1 ) ); - $out = array_merge( $out, call_user_func_array( 'xml_find', $call_args ) ); + $subtree =& $tree[ $i ]['child']; + $out = array_merge( $out, xml_find( $subtree, ...array_slice( $elements, 1 ) ) ); } } } @@ -300,9 +295,7 @@ function xml_array_dumbdown( &$data ) { return $out; } -function dmp() { - $args = func_get_args(); - +function dmp( ...$args ) { foreach ( $args as $thing ) { echo ( is_scalar( $thing ) ? strval( $thing ) : var_export( $thing, true ) ), "\n"; } diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index f081245c6b..1cd3fddf6f 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -2541,10 +2541,11 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { * Capture the actions fired when calling WP_Customize_Manager::set_post_value(). * * @see Tests_WP_Customize_Manager::test_set_post_value() + * + * @param mixed ...$args Optional arguments passed to the action. */ - function capture_customize_post_value_set_actions() { + function capture_customize_post_value_set_actions( ...$args ) { $action = current_action(); - $args = func_get_args(); $this->captured_customize_post_value_set_actions[] = compact( 'action', 'args' ); } diff --git a/tests/phpunit/tests/hooks/doAction.php b/tests/phpunit/tests/hooks/doAction.php index 48ced32e1d..34f90eae3a 100644 --- a/tests/phpunit/tests/hooks/doAction.php +++ b/tests/phpunit/tests/hooks/doAction.php @@ -164,9 +164,10 @@ class Tests_WP_Hook_Do_Action extends WP_UnitTestCase { /** * Use this rather than MockAction so we can test callbacks with no args + * + * @param mixed ...$args Optional arguments passed to the action. */ - public function _action_callback() { - $args = func_get_args(); + public function _action_callback( ...$args ) { $this->events[] = array( 'action' => __FUNCTION__, 'args' => $args,