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/option/updateOption.php

    r53865 r55745  
    2929     */
    3030    public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_missing() {
    31         global $wpdb;
    3231        $this->flush_cache();
    3332        update_option( 'test_update_option_default', 'value' );
     
    3736        wp_load_alloptions();
    3837
    39         $before = $wpdb->num_queries;
    40         $value  = get_option( 'test_update_option_default' );
    41         $after  = $wpdb->num_queries;
     38        $before = get_num_queries();
     39        $value  = get_option( 'test_update_option_default' );
     40        $after  = get_num_queries();
    4241
    4342        $this->assertSame( $before, $after );
     
    5352     */
    5453    public function test_should_set_autoload_yes_for_nonexistent_option_when_autoload_param_is_yes() {
    55         global $wpdb;
    5654        $this->flush_cache();
    5755        update_option( 'test_update_option_default', 'value', 'yes' );
     
    6159        wp_load_alloptions();
    6260
    63         $before = $wpdb->num_queries;
    64         $value  = get_option( 'test_update_option_default' );
    65         $after  = $wpdb->num_queries;
     61        $before = get_num_queries();
     62        $value  = get_option( 'test_update_option_default' );
     63        $after  = get_num_queries();
    6664
    6765        $this->assertSame( $before, $after );
     
    7775     */
    7876    public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_no() {
    79         global $wpdb;
    8077        $this->flush_cache();
    8178        update_option( 'test_update_option_default', 'value', 'no' );
     
    8582        wp_load_alloptions();
    8683
    87         $before = $wpdb->num_queries;
    88         $value  = get_option( 'test_update_option_default' );
    89         $after  = $wpdb->num_queries;
     84        $before = get_num_queries();
     85        $value  = get_option( 'test_update_option_default' );
     86        $after  = get_num_queries();
    9087
    9188        // Database has been hit.
     
    10299     */
    103100    public function test_should_set_autoload_no_for_nonexistent_option_when_autoload_param_is_false() {
    104         global $wpdb;
    105101        $this->flush_cache();
    106102        update_option( 'test_update_option_default', 'value', false );
     
    110106        wp_load_alloptions();
    111107
    112         $before = $wpdb->num_queries;
    113         $value  = get_option( 'test_update_option_default' );
    114         $after  = $wpdb->num_queries;
     108        $before = get_num_queries();
     109        $value  = get_option( 'test_update_option_default' );
     110        $after  = get_num_queries();
    115111
    116112        // Database has been hit.
     
    127123     */
    128124    public function test_autoload_should_be_updated_for_existing_option_when_value_is_changed() {
    129         global $wpdb;
    130125        add_option( 'foo', 'bar', '', 'no' );
    131126        $updated = update_option( 'foo', 'bar2', true );
     
    137132        wp_load_alloptions();
    138133
    139         $before = $wpdb->num_queries;
     134        $before = get_num_queries();
    140135        $value  = get_option( 'foo' );
    141136
    142         $this->assertSame( $before, $wpdb->num_queries );
     137        $this->assertSame( $before, get_num_queries() );
    143138        $this->assertSame( $value, 'bar2' );
    144139    }
     
    152147     */
    153148    public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_unchanged() {
    154         global $wpdb;
    155149        add_option( 'foo', 'bar', '', 'yes' );
    156150        $updated = update_option( 'foo', 'bar', false );
     
    162156        wp_load_alloptions();
    163157
    164         $before = $wpdb->num_queries;
     158        $before = get_num_queries();
    165159        $value  = get_option( 'foo' );
    166160
    167161        // 'foo' should still be autoload=yes, so we should see no additional querios.
    168         $this->assertSame( $before, $wpdb->num_queries );
     162        $this->assertSame( $before, get_num_queries() );
    169163        $this->assertSame( $value, 'bar' );
    170164    }
     
    178172     */
    179173    public function test_autoload_should_not_be_updated_for_existing_option_when_value_is_changed_but_no_value_of_autoload_is_provided() {
    180         global $wpdb;
    181174        add_option( 'foo', 'bar', '', 'yes' );
    182175
     
    190183        wp_load_alloptions();
    191184
    192         $before = $wpdb->num_queries;
     185        $before = get_num_queries();
    193186        $value  = get_option( 'foo' );
    194187
    195188        // 'foo' should still be autoload=yes, so we should see no additional queries.
    196         $this->assertSame( $before, $wpdb->num_queries );
     189        $this->assertSame( $before, get_num_queries() );
    197190        $this->assertSame( $value, 'bar2' );
    198191    }
Note: See TracChangeset for help on using the changeset viewer.