Make WordPress Core

Changeset 26005


Ignore:
Timestamp:
11/04/2013 09:55:12 PM (10 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.

Location:
trunk/tests/phpunit
Files:
2 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}
  • trunk/tests/phpunit/tests/query/conditionals.php

    r25352 r26005  
    3535    }
    3636
    37     /**
    38      * Check each of the WP_Query is_* functions/properties against expected boolean value.
    39      *
    40      * Any properties that are listed by name as parameters will be expected to be true; any others are
    41      * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single()
    42      * and is_feed() must be true and everything else must be false to pass.
    43      *
    44      * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
    45      */
    46     function assertQueryTrue(/* ... */) {
    47         global $wp_query;
    48         $all = array(
    49             'is_single', 'is_preview', 'is_page', 'is_archive', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
    50             'is_author', 'is_category', 'is_tag', 'is_tax', 'is_search', 'is_feed', 'is_comment_feed', 'is_trackback',
    51             'is_home', 'is_404', 'is_comments_popup', 'is_paged', 'is_admin', 'is_attachment', 'is_singular', 'is_robots',
    52             'is_posts_page', 'is_post_type_archive',
    53         );
    54         $true = func_get_args();
    55 
    56         $passed = true;
    57         $not_false = $not_true = array(); // properties that were not set to expected values
    58 
    59         foreach ( $all as $query_thing ) {
    60             $result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
    61 
    62             if ( in_array( $query_thing, $true ) ) {
    63                 if ( ! $result ) {
    64                     array_push( $not_true, $query_thing );
    65                     $passed = false;
    66                 }
    67             } else if ( $result ) {
    68                 array_push( $not_false, $query_thing );
    69                 $passed = false;
    70             }
    71         }
    72 
    73         $message = '';
    74         if ( count($not_true) )
    75             $message .= implode( $not_true, ', ' ) . ' should be true. ';
    76         if ( count($not_false) )
    77             $message .= implode( $not_false, ', ' ) . ' should be false.';
    78         $this->assertTrue( $passed, $message );
    79     }
    80 
    8137    function test_home() {
    8238        $this->go_to('/');
Note: See TracChangeset for help on using the changeset viewer.