Make WordPress Core


Ignore:
Timestamp:
05/11/2023 10:05:51 AM (3 years ago)
Author:
spacedmonkey
Message:

Tests: Use the function get_num_queries across all unit tests.

Replace use of $wpdb->num_queries with a function call to get_num_queries. This improves readability and consistency between tests.

Props SergeyBiryukov, peterwilsoncc, spacedmonkey.
See #57841.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/metaCache.php

    r54704 r55745  
    1313         */
    1414        public function test_update_comment_meta_cache_should_default_to_true() {
    15                 global $wpdb;
    16 
    1715                $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    1816                $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    3129                );
    3230
    33                 $num_queries = $wpdb->num_queries;
     31                $num_queries = get_num_queries();
    3432                foreach ( $comment_ids as $cid ) {
    3533                        get_comment_meta( $cid, 'foo', 'bar' );
    3634                }
    3735
    38                 $this->assertSame( $num_queries, $wpdb->num_queries );
     36                $this->assertSame( $num_queries, get_num_queries() );
    3937        }
    4038
     
    4543         */
    4644        public function test_update_comment_meta_cache_true() {
    47                 global $wpdb;
    48 
    4945                $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    5046                $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    6460                );
    6561
    66                 $num_queries = $wpdb->num_queries;
     62                $num_queries = get_num_queries();
    6763                foreach ( $comment_ids as $cid ) {
    6864                        get_comment_meta( $cid, 'foo', 'bar' );
    6965                }
    7066
    71                 $this->assertSame( $num_queries, $wpdb->num_queries );
     67                $this->assertSame( $num_queries, get_num_queries() );
    7268        }
    7369
     
    7874         */
    7975        public function test_update_comment_meta_cache_false() {
    80                 global $wpdb;
    81 
    8276                $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    8377                $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    9488                );
    9589
    96                 $num_queries = $wpdb->num_queries;
     90                $num_queries = get_num_queries();
    9791                foreach ( $comment_ids as $cid ) {
    9892                        get_comment_meta( $cid, 'foo', 'bar' );
    9993                }
    10094
    101                 $this->assertSame( $num_queries + 3, $wpdb->num_queries );
     95                $this->assertSame( $num_queries + 3, get_num_queries() );
    10296        }
    10397
     
    108102         */
    109103        public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comments_template() {
    110                 global $wpdb;
    111 
    112104                $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
    113105                $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
     
    127119
    128120                                // First request will hit the database.
    129                                 $num_queries = $wpdb->num_queries;
     121                                $num_queries = get_num_queries();
    130122                                get_comment_meta( $comment_ids[0], 'sauce' );
    131                                 $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     123                                $this->assertSame( $num_queries + 1, get_num_queries() );
    132124
    133125                                // Second and third requests should be in cache.
    134126                                get_comment_meta( $comment_ids[1], 'sauce' );
    135127                                get_comment_meta( $comment_ids[2], 'sauce' );
    136                                 $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     128                                $this->assertSame( $num_queries + 1, get_num_queries() );
    137129                        }
    138130                }
     
    145137         */
    146138        public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
    147                 global $wpdb;
    148 
    149139                $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
    150140
     
    174164
    175165                // First comment will cause the cache to be primed.
    176                 $num_queries = $wpdb->num_queries;
     166                $num_queries = get_num_queries();
    177167                $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
    178168                $num_queries++;
    179                 $this->assertSame( $num_queries, $wpdb->num_queries );
     169                $this->assertSame( $num_queries, get_num_queries() );
    180170
    181171                // Second comment from the results should not cause more queries.
    182172                $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
    183                 $this->assertSame( $num_queries, $wpdb->num_queries );
     173                $this->assertSame( $num_queries, get_num_queries() );
    184174
    185175                // A comment from outside the results will not be primed.
    186176                $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
    187177                $num_queries++;
    188                 $this->assertSame( $num_queries, $wpdb->num_queries );
     178                $this->assertSame( $num_queries, get_num_queries() );
    189179        }
    190180
     
    195185         */
    196186        public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
    197                 global $wpdb;
    198 
    199187                $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
    200188
     
    225213
    226214                // First comment will cause the cache to be primed.
    227                 $num_queries = $wpdb->num_queries;
     215                $num_queries = get_num_queries();
    228216                $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
    229217                $num_queries++;
    230                 $this->assertSame( $num_queries, $wpdb->num_queries );
     218                $this->assertSame( $num_queries, get_num_queries() );
    231219
    232220                // Second comment from the results should not cause more queries.
    233221                $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
    234                 $this->assertSame( $num_queries, $wpdb->num_queries );
     222                $this->assertSame( $num_queries, get_num_queries() );
    235223
    236224                // A comment from outside the results will not be primed.
    237225                $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
    238226                $num_queries++;
    239                 $this->assertSame( $num_queries, $wpdb->num_queries );
     227                $this->assertSame( $num_queries, get_num_queries() );
    240228        }
    241229
Note: See TracChangeset for help on using the changeset viewer.