Make WordPress Core


Ignore:
Timestamp:
05/26/2025 12:03:18 PM (9 months ago)
Author:
johnbillion
Message:

Build/Test Tools: Add assertions that test the tests.

Several tests perform assertions conditionally or iterate dynamic arrays without ensuring they're populated. If the test is faulty and the condition never evaluates to true, or the array being iterated is unexpectedly empty, this will now correctly cause the test to fail.

Props johnbillion, jrf.

See #63167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/meta.php

    r56549 r60251  
    133133        remove_action( 'pre_get_posts', array( $this, 'set_cache_results' ) );
    134134
    135         if ( have_posts() ) {
    136             while ( have_posts() ) {
    137                 the_post();
    138 
    139                 // First request will hit the database.
    140                 $num_queries = get_num_queries();
    141                 $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
    142                 ++$num_queries;
    143                 $this->assertSame( $num_queries, get_num_queries() );
    144 
    145                 // Second and third requests should be in cache.
    146                 $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
    147                 $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
    148                 $this->assertSame( $num_queries, get_num_queries() );
    149 
    150                 // Querying a term not primed should result in a hit.
    151                 ++$num_queries;
    152                 $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
    153                 $this->assertSame( $num_queries, get_num_queries() );
    154             }
     135        $this->assertTrue( have_posts() );
     136
     137        while ( have_posts() ) {
     138            the_post();
     139
     140            // First request will hit the database.
     141            $num_queries = get_num_queries();
     142            $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
     143            ++$num_queries;
     144            $this->assertSame( $num_queries, get_num_queries() );
     145
     146            // Second and third requests should be in cache.
     147            $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
     148            $this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
     149            $this->assertSame( $num_queries, get_num_queries() );
     150
     151            // Querying a term not primed should result in a hit.
     152            ++$num_queries;
     153            $this->assertSame( 'bar', get_term_meta( $orphan_term, 'foo', true ) );
     154            $this->assertSame( $num_queries, get_num_queries() );
    155155        }
    156156    }
     
    202202        );
    203203
    204         if ( $q->have_posts() ) {
    205             while ( $q->have_posts() ) {
    206                 $q->the_post();
    207 
    208                 // Requests will hit the database.
    209                 $num_queries = get_num_queries();
    210                 $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
    211                 ++$num_queries;
    212                 $this->assertSame( $num_queries, get_num_queries() );
    213 
    214                 $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
    215                 ++$num_queries;
    216                 $this->assertSame( $num_queries, get_num_queries() );
    217             }
     204        $this->assertTrue( $q->have_posts() );
     205
     206        while ( $q->have_posts() ) {
     207            $q->the_post();
     208
     209            // Requests will hit the database.
     210            $num_queries = get_num_queries();
     211            $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
     212            ++$num_queries;
     213            $this->assertSame( $num_queries, get_num_queries() );
     214
     215            $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
     216            ++$num_queries;
     217            $this->assertSame( $num_queries, get_num_queries() );
    218218        }
    219219    }
Note: See TracChangeset for help on using the changeset viewer.