Make WordPress Core


Ignore:
Timestamp:
05/16/2022 02:09:41 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/conditionals.php

    r53396 r53400  
    16191619        /**
    16201620         * @ticket 55104
    1621          */
    1622         public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() {
     1621         *
     1622         * @dataProvider data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set
     1623         *
     1624         * @param string $function_name The name of the function to test.
     1625         */
     1626        public function test_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set( $function_name ) {
    16231627                unset( $GLOBALS['wp_query'] );
    16241628
    1625                 $functions = get_class_methods( 'WP_Query' );
    1626 
    1627                 foreach ( $functions as $function_name ) {
    1628                         // Only test `is_*()` conditional tags.
    1629                         if ( ! str_starts_with( $function_name, 'is_' ) ) {
    1630                                 continue;
     1629                if ( 'is_comments_popup' === $function_name ) {
     1630                        // `is_comments_popup()` is deprecated as of WP 4.5.
     1631                        $this->setExpectedDeprecated( $function_name );
     1632                } else {
     1633                        // All the other functions should throw a `_doing_it_wrong()` notice.
     1634                        $this->setExpectedIncorrectUsage( $function_name );
     1635                }
     1636
     1637                $this->assertFalse( call_user_func( $function_name ) );
     1638        }
     1639
     1640        /**
     1641         * Data provider.
     1642         */
     1643        public function data_conditional_tags_trigger_doing_it_wrong_and_return_false_if_wp_query_is_not_set() {
     1644                // Get the list of `is_*()` conditional tags.
     1645                $functions = array_filter(
     1646                        get_class_methods( 'WP_Query' ),
     1647                        static function( $function_name ) {
     1648                                return str_starts_with( $function_name, 'is_' );
    16311649                        }
    1632 
    1633                         if ( 'is_comments_popup' === $function_name ) {
    1634                                 // `is_comments_popup()` is deprecated as of WP 4.5.
    1635                                 $this->setExpectedDeprecated( $function_name );
    1636                         } else {
    1637                                 // All the other functions should throw a `_doing_it_wrong()` notice.
    1638                                 $this->setExpectedIncorrectUsage( $function_name );
    1639                         }
    1640 
    1641                         $this->assertFalse( call_user_func( $function_name ) );
    1642                 }
     1650                );
     1651
     1652                // Wrap each function name in an array.
     1653                $functions = array_map(
     1654                        static function( $function_name ) {
     1655                                return array( $function_name );
     1656                        },
     1657                        $functions
     1658                );
     1659
     1660                return $functions;
    16431661        }
    16441662
Note: See TracChangeset for help on using the changeset viewer.