Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

File:
1 edited

Legend:

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

    r47122 r48937  
    1111        public function test_empty_meta_query_param() {
    1212                $query = new WP_Meta_Query();
    13                 $this->assertSame( null, $query->relation );
     13                $this->assertNull( $query->relation );
    1414        }
    1515
    1616        public function test_default_relation() {
    1717                $query = new WP_Meta_Query( array( array( 'key' => 'abc' ) ) );
    18                 $this->assertEquals( 'AND', $query->relation );
     18                $this->assertSame( 'AND', $query->relation );
    1919        }
    2020
     
    2828                );
    2929
    30                 $this->assertEquals( 'AND', $query->relation );
     30                $this->assertSame( 'AND', $query->relation );
    3131
    3232                $query = new WP_Meta_Query(
     
    3737                );
    3838
    39                 $this->assertEquals( 'OR', $query->relation );
     39                $this->assertSame( 'OR', $query->relation );
    4040        }
    4141
     
    7575                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
    7676
    77                 $this->assertEquals( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
     77                $this->assertSame( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
    7878
    7979                // Also check mixing key and key => value.
     
    9393                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID' );
    9494
    95                 $this->assertEquals( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
     95                $this->assertSame( 1, substr_count( $sql['join'], 'INNER JOIN' ) );
    9696        }
    9797
     
    121121                        'value'   => 'baz',
    122122                );
    123                 $this->assertEquals( $expected0, $query->queries[0] );
     123                $this->assertSame( $expected0, $query->queries[0] );
    124124
    125125                $expected1 = array(
    126                         'relation' => 'OR',
    127126                        array(
    128127                                'key'     => 'foo1',
     
    130129                                'value'   => 'bar1',
    131130                        ),
    132                 );
    133                 $this->assertEquals( $expected1, $query->queries[1] );
     131                        'relation' => 'OR',
     132                );
     133                $this->assertSame( $expected1, $query->queries[1] );
    134134        }
    135135
     
    177177                // Just meta_value.
    178178                $expected = array(
     179                        array(
     180                                'key' => 'abc',
     181                        ),
    179182                        'relation' => 'OR',
    180                         array(
    181                                 'key' => 'abc',
    182                         ),
    183183                );
    184184                $query->parse_query_vars(
     
    187187                        )
    188188                );
    189                 $this->assertEquals( $expected, $query->queries );
     189                $this->assertSame( $expected, $query->queries );
    190190
    191191                // meta_key & meta_value.
    192192                $expected = array(
    193                         'relation' => 'OR',
    194193                        array(
    195194                                'key'   => 'abc',
    196195                                'value' => 'def',
    197196                        ),
     197                        'relation' => 'OR',
    198198                );
    199199                $query->parse_query_vars(
     
    203203                        )
    204204                );
    205                 $this->assertEquals( $expected, $query->queries );
     205                $this->assertSame( $expected, $query->queries );
    206206
    207207                // meta_compare.
    208208                $expected = array(
    209                         'relation' => 'OR',
    210209                        array(
    211210                                'key'     => 'abc',
    212211                                'compare' => '=>',
    213212                        ),
     213                        'relation' => 'OR',
    214214                );
    215215                $query->parse_query_vars(
     
    219219                        )
    220220                );
    221                 $this->assertEquals( $expected, $query->queries );
     221                $this->assertSame( $expected, $query->queries );
    222222        }
    223223
     
    227227        public function test_get_cast_for_type() {
    228228                $query = new WP_Meta_Query();
    229                 $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
    230                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
    231                 $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) );
    232                 $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
    233                 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
    234                 $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
    235                 $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) );
    236                 $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
    237                 $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
    238                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
    239                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
    240                 $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
    241                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10,  5)' ) );
    242                 $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
    243                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
    244                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
    245                 $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
    246                 $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
    247                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
    248                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
    249                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
    250                 $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
    251                 $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
    252                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10,  5)' ) );
    253 
    254                 $this->assertEquals( 'CHAR', $query->get_cast_for_type() );
    255                 $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
     229                $this->assertSame( 'BINARY', $query->get_cast_for_type( 'BINARY' ) );
     230                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'CHAR' ) );
     231                $this->assertSame( 'DATE', $query->get_cast_for_type( 'DATE' ) );
     232                $this->assertSame( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) );
     233                $this->assertSame( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) );
     234                $this->assertSame( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) );
     235                $this->assertSame( 'TIME', $query->get_cast_for_type( 'TIME' ) );
     236                $this->assertSame( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) );
     237                $this->assertSame( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) );
     238                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) );
     239                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) );
     240                $this->assertSame( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) );
     241                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10,  5)' ) );
     242                $this->assertSame( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) );
     243                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) );
     244                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) );
     245                $this->assertSame( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) );
     246                $this->assertSame( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) );
     247                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) );
     248                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) );
     249                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) );
     250                $this->assertSame( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) );
     251                $this->assertSame( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) );
     252                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10,  5)' ) );
     253
     254                $this->assertSame( 'CHAR', $query->get_cast_for_type() );
     255                $this->assertSame( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
    256256        }
    257257
    258258        public function test_sanitize_query_single_query() {
    259259                $expected = array(
    260                         'relation' => 'OR',
    261260                        array(
    262261                                'key'   => 'foo',
    263262                                'value' => 'bar',
    264263                        ),
     264                        'relation' => 'OR',
    265265                );
    266266
     
    275275                );
    276276
    277                 $this->assertEquals( $expected, $found );
     277                $this->assertSame( $expected, $found );
    278278        }
    279279
    280280        public function test_sanitize_query_multiple_first_order_queries_relation_default() {
    281281                $expected = array(
    282                         'relation' => 'AND',
    283282                        array(
    284283                                'key'   => 'foo',
     
    289288                                'value' => 'bar2',
    290289                        ),
     290                        'relation' => 'AND',
    291291                );
    292292
     
    305305                );
    306306
    307                 $this->assertEquals( $expected, $found );
     307                $this->assertSame( $expected, $found );
    308308        }
    309309
    310310        public function test_sanitize_query_multiple_first_order_queries_relation_or() {
    311311                $expected = array(
    312                         'relation' => 'OR',
    313312                        array(
    314313                                'key'   => 'foo',
     
    319318                                'value' => 'bar2',
    320319                        ),
     320                        'relation' => 'OR',
    321321                );
    322322
     
    324324                $found = $q->sanitize_query(
    325325                        array(
    326                                 'relation' => 'OR',
    327326                                array(
    328327                                        'key'   => 'foo',
     
    333332                                        'value' => 'bar2',
    334333                                ),
    335                         )
    336                 );
    337 
    338                 $this->assertEquals( $expected, $found );
     334                                'relation' => 'OR',
     335                        )
     336                );
     337
     338                $this->assertSame( $expected, $found );
    339339        }
    340340
    341341        public function test_sanitize_query_multiple_first_order_queries_relation_or_lowercase() {
    342342                $expected = array(
    343                         'relation' => 'OR',
    344343                        array(
    345344                                'key'   => 'foo',
     
    350349                                'value' => 'bar2',
    351350                        ),
     351                        'relation' => 'OR',
    352352                );
    353353
     
    355355                $found = $q->sanitize_query(
    356356                        array(
    357                                 'relation' => 'or',
    358357                                array(
    359358                                        'key'   => 'foo',
     
    364363                                        'value' => 'bar2',
    365364                                ),
    366                         )
    367                 );
    368 
    369                 $this->assertEquals( $expected, $found );
     365                                'relation' => 'or',
     366                        )
     367                );
     368
     369                $this->assertSame( $expected, $found );
    370370        }
    371371
    372372        public function test_sanitize_query_multiple_first_order_queries_invalid_relation() {
    373373                $expected = array(
    374                         'relation' => 'AND',
    375374                        array(
    376375                                'key'   => 'foo',
     
    381380                                'value' => 'bar2',
    382381                        ),
     382                        'relation' => 'AND',
    383383                );
    384384
     
    386386                $found = $q->sanitize_query(
    387387                        array(
    388                                 'relation' => 'FOO',
    389388                                array(
    390389                                        'key'   => 'foo',
     
    395394                                        'value' => 'bar2',
    396395                                ),
    397                         )
    398                 );
    399 
    400                 $this->assertEquals( $expected, $found );
     396                                'relation' => 'FOO',
     397                        )
     398                );
     399
     400                $this->assertSame( $expected, $found );
    401401        }
    402402
    403403        public function test_sanitize_query_single_query_which_is_a_nested_query() {
    404404                $expected = array(
    405                         'relation' => 'OR',
    406                         array(
    407                                 'relation' => 'AND',
     405                        array(
    408406                                array(
    409407                                        'key'   => 'foo',
     
    414412                                        'value' => 'bar2',
    415413                                ),
    416                         ),
     414                                'relation' => 'AND',
     415                        ),
     416                        'relation' => 'OR',
    417417                );
    418418
     
    433433                );
    434434
    435                 $this->assertEquals( $expected, $found );
     435                $this->assertSame( $expected, $found );
    436436        }
    437437
    438438        public function test_sanitize_query_multiple_nested_queries() {
    439439                $expected = array(
    440                         'relation' => 'OR',
    441                         array(
    442                                 'relation' => 'AND',
     440                        array(
    443441                                array(
    444442                                        'key'   => 'foo',
     
    449447                                        'value' => 'bar2',
    450448                                ),
    451                         ),
    452                         array(
    453449                                'relation' => 'AND',
     450                        ),
     451                        array(
    454452                                array(
    455453                                        'key'   => 'foo3',
     
    460458                                        'value' => 'bar4',
    461459                                ),
    462                         ),
     460                                'relation' => 'AND',
     461                        ),
     462                        'relation' => 'OR',
    463463                );
    464464
     
    466466                $found = $q->sanitize_query(
    467467                        array(
    468                                 'relation' => 'OR',
    469468                                array(
    470469                                        array(
     
    487486                                        ),
    488487                                ),
    489                         )
    490                 );
    491 
    492                 $this->assertEquals( $expected, $found );
     488                                'relation' => 'OR',
     489                        )
     490                );
     491
     492                $this->assertSame( $expected, $found );
    493493        }
    494494
     
    529529                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    530530
    531                 $this->assertEquals( 3, substr_count( $sql['join'], 'JOIN' ) );
     531                $this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
    532532        }
    533533
     
    549549                $sql   = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    550550
    551                 $this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) );
     551                $this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) );
    552552        }
    553553
     
    561561                $query1 = new WP_Meta_Query(
    562562                        array(
    563                                 'relation' => 'OR',
    564 
    565563                                // Empty 'compare'.
    566564                                array(
     
    590588                                        'value' => 'bar',
    591589                                ),
     590
     591                                'relation' => 'OR',
    592592                        )
    593593                );
     
    605605                $query2 = new WP_Meta_Query(
    606606                        array(
    607                                 'relation' => 'AND',
    608 
    609607                                // Empty 'compare'.
    610608                                array(
     
    617615                                        'compare' => '<',
    618616                                ),
     617
     618                                'relation' => 'AND',
    619619                        )
    620620                );
     
    888888                $query = new WP_Meta_Query(
    889889                        array(
    890                                 'relation' => 'OR',
    891890                                array(
    892891                                        'key'     => 'exclude',
     
    898897                                        'value'   => '1',
    899898                                ),
     899                                'relation' => 'OR',
    900900                        )
    901901                );
     
    911911                $query = new WP_Meta_Query(
    912912                        array(
    913                                 'relation' => 'OR',
    914913                                array(
    915914                                        'key'     => 'exclude',
     
    921920                                        'value'   => '1',
    922921                                ),
     922                                'relation' => 'OR',
    923923                        )
    924924                );
     
    937937                $q = new WP_Meta_Query(
    938938                        array(
    939                                 'relation' => 'AND',
    940939                                array(
    941940                                        'key'   => 'foo',
     
    953952                                        ),
    954953                                ),
     954                                'relation' => 'AND',
    955955                        )
    956956                );
     
    965965                $q = new WP_Meta_Query(
    966966                        array(
    967                                 'relation' => 'OR',
    968967                                array(
    969968                                        'key'   => 'foo',
     
    981980                                        ),
    982981                                ),
     982                                'relation' => 'OR',
    983983                        )
    984984                );
     
    993993                $q = new WP_Meta_Query(
    994994                        array(
    995                                 'relation' => 'AND',
    996995                                array(
    997996                                        'key'   => 'foo',
     
    10091008                                        ),
    10101009                                ),
     1010                                'relation' => 'AND',
    10111011                        )
    10121012                );
Note: See TracChangeset for help on using the changeset viewer.