From cfc71d580f6915404b5cc6e2a58af2589e341a91 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 16 May 2022 14:09:41 +0000 Subject: [PATCH] Tests: Use a data provider in the test for conditional tags returning early if `$wp_query` is not set. Follow-up to [53395], [53396]. Props peterwilsoncc. See #55104. git-svn-id: https://develop.svn.wordpress.org/trunk@53400 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/conditionals.php | 54 ++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 1bed9d0782..cc191fa1f2 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -1618,28 +1618,46 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { /** * @ticket 55104 + * + * @dataProvider data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set + * + * @param string $function_name The name of the function to test. */ - public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() { + public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set( $function_name ) { unset( $GLOBALS['wp_query'] ); - $functions = get_class_methods( 'WP_Query' ); - - foreach ( $functions as $function_name ) { - // Only test `is_*()` conditional tags. - if ( ! str_starts_with( $function_name, 'is_' ) ) { - continue; - } - - if ( 'is_comments_popup' === $function_name ) { - // `is_comments_popup()` is deprecated as of WP 4.5. - $this->setExpectedDeprecated( $function_name ); - } else { - // All the other functions should throw a `_doing_it_wrong()` notice. - $this->setExpectedIncorrectUsage( $function_name ); - } - - $this->assertFalse( call_user_func( $function_name ) ); + if ( 'is_comments_popup' === $function_name ) { + // `is_comments_popup()` is deprecated as of WP 4.5. + $this->setExpectedDeprecated( $function_name ); + } else { + // All the other functions should throw a `_doing_it_wrong()` notice. + $this->setExpectedIncorrectUsage( $function_name ); } + + $this->assertFalse( call_user_func( $function_name ) ); + } + + /** + * Data provider. + */ + public function data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() { + // Get the list of `is_*()` conditional tags. + $functions = array_filter( + get_class_methods( 'WP_Query' ), + static function( $function_name ) { + return str_starts_with( $function_name, 'is_' ); + } + ); + + // Wrap each function name in an array. + $functions = array_map( + static function( $function_name ) { + return array( $function_name ); + }, + $functions + ); + + return $functions; } }