Make WordPress Core


Ignore:
Timestamp:
11/04/2013 09:55:12 PM (13 years ago)
Author:
wonderboymusic
Message:

Move Tests_Query_Conditionals::assertQueryTrue() to WP_UnitTestCase. It should be available to all unit test classes. The conditionals class is huge, other classes are necessary for better coverage.

File:
1 edited

Legend:

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

    r25785 r26005  
    279279                return tempnam( $tmp_dir, 'wpunit' );
    280280        }
     281
     282        /**
     283         * Check each of the WP_Query is_* functions/properties against expected boolean value.
     284         *
     285         * Any properties that are listed by name as parameters will be expected to be true; any others are
     286         * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single()
     287         * and is_feed() must be true and everything else must be false to pass.
     288         *
     289         * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
     290         */
     291        function assertQueryTrue(/* ... */) {
     292                global $wp_query;
     293                $all = array(
     294                        'is_single', 'is_preview', 'is_page', 'is_archive', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
     295                        'is_author', 'is_category', 'is_tag', 'is_tax', 'is_search', 'is_feed', 'is_comment_feed', 'is_trackback',
     296                        'is_home', 'is_404', 'is_comments_popup', 'is_paged', 'is_admin', 'is_attachment', 'is_singular', 'is_robots',
     297                        'is_posts_page', 'is_post_type_archive',
     298                );
     299                $true = func_get_args();
     300
     301                $passed = true;
     302                $not_false = $not_true = array(); // properties that were not set to expected values
     303
     304                foreach ( $all as $query_thing ) {
     305                        $result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
     306
     307                        if ( in_array( $query_thing, $true ) ) {
     308                                if ( ! $result ) {
     309                                        array_push( $not_true, $query_thing );
     310                                        $passed = false;
     311                                }
     312                        } else if ( $result ) {
     313                                array_push( $not_false, $query_thing );
     314                                $passed = false;
     315                        }
     316                }
     317
     318                $message = '';
     319                if ( count($not_true) )
     320                        $message .= implode( $not_true, ', ' ) . ' should be true. ';
     321                if ( count($not_false) )
     322                        $message .= implode( $not_false, ', ' ) . ' should be false.';
     323                $this->assertTrue( $passed, $message );
     324        }
    281325}
Note: See TracChangeset for help on using the changeset viewer.