Make WordPress Core


Ignore:
Timestamp:
09/02/2020 12:35:36 AM (4 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/query/results.php

    r47122 r48937  
    319319        );
    320320
    321         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     321        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    322322    }
    323323
     
    327327        // There are 4 posts with Tag A.
    328328        $this->assertCount( 4, $posts );
    329         $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
    330         $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
    331         $this->assertEquals( 'tag-a', $posts[2]->post_name );
    332         $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
     329        $this->assertSame( 'tags-a-and-c', $posts[0]->post_name );
     330        $this->assertSame( 'tags-a-and-b', $posts[1]->post_name );
     331        $this->assertSame( 'tag-a', $posts[2]->post_name );
     332        $this->assertSame( 'tags-a-b-c', $posts[3]->post_name );
    333333    }
    334334
     
    338338        // There are 4 posts with Tag A.
    339339        $this->assertCount( 4, $posts );
    340         $this->assertEquals( 'tags-b-and-c', $posts[0]->post_name );
    341         $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
    342         $this->assertEquals( 'tag-b', $posts[2]->post_name );
    343         $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
     340        $this->assertSame( 'tags-b-and-c', $posts[0]->post_name );
     341        $this->assertSame( 'tags-a-and-b', $posts[1]->post_name );
     342        $this->assertSame( 'tag-b', $posts[2]->post_name );
     343        $this->assertSame( 'tags-a-b-c', $posts[3]->post_name );
    344344    }
    345345
     
    352352        // There is 1 post with Tag נ.
    353353        $this->assertCount( 1, $posts );
    354         $this->assertEquals( 'tag-%d7%a0', $posts[0]->post_name );
     354        $this->assertSame( 'tag-%d7%a0', $posts[0]->post_name );
    355355    }
    356356
     
    361361        // There are 4 posts with Tag A.
    362362        $this->assertCount( 4, $posts );
    363         $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
    364         $this->assertEquals( 'tags-a-and-b', $posts[1]->post_name );
    365         $this->assertEquals( 'tag-a', $posts[2]->post_name );
    366         $this->assertEquals( 'tags-a-b-c', $posts[3]->post_name );
     363        $this->assertSame( 'tags-a-and-c', $posts[0]->post_name );
     364        $this->assertSame( 'tags-a-and-b', $posts[1]->post_name );
     365        $this->assertSame( 'tag-a', $posts[2]->post_name );
     366        $this->assertSame( 'tags-a-b-c', $posts[3]->post_name );
    367367    }
    368368
     
    372372        // There are 4 posts with either Tag B or Tag C.
    373373        $this->assertCount( 6, $posts );
    374         $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
    375         $this->assertEquals( 'tags-b-and-c', $posts[1]->post_name );
    376         $this->assertEquals( 'tags-a-and-b', $posts[2]->post_name );
    377         $this->assertEquals( 'tag-c', $posts[3]->post_name );
    378         $this->assertEquals( 'tag-b', $posts[4]->post_name );
    379         $this->assertEquals( 'tags-a-b-c', $posts[5]->post_name );
     374        $this->assertSame( 'tags-a-and-c', $posts[0]->post_name );
     375        $this->assertSame( 'tags-b-and-c', $posts[1]->post_name );
     376        $this->assertSame( 'tags-a-and-b', $posts[2]->post_name );
     377        $this->assertSame( 'tag-c', $posts[3]->post_name );
     378        $this->assertSame( 'tag-b', $posts[4]->post_name );
     379        $this->assertSame( 'tags-a-b-c', $posts[5]->post_name );
    380380    }
    381381
     
    388388        // There are 6 posts with either Tag A or Tag B.
    389389        $this->assertCount( 6, $posts );
    390         $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
    391         $this->assertEquals( 'tags-b-and-c', $posts[1]->post_name );
    392         $this->assertEquals( 'tags-a-and-b', $posts[2]->post_name );
    393         $this->assertEquals( 'tag-b', $posts[3]->post_name );
    394         $this->assertEquals( 'tag-a', $posts[4]->post_name );
    395         $this->assertEquals( 'tags-a-b-c', $posts[5]->post_name );
     390        $this->assertSame( 'tags-a-and-c', $posts[0]->post_name );
     391        $this->assertSame( 'tags-b-and-c', $posts[1]->post_name );
     392        $this->assertSame( 'tags-a-and-b', $posts[2]->post_name );
     393        $this->assertSame( 'tag-b', $posts[3]->post_name );
     394        $this->assertSame( 'tag-a', $posts[4]->post_name );
     395        $this->assertSame( 'tags-a-b-c', $posts[5]->post_name );
    396396    }
    397397
     
    415415        );
    416416
    417         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     417        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    418418    }
    419419
     
    425425        // There are 4 posts with Tag A, only 2 when we exclude Tag B.
    426426        $this->assertCount( 2, $posts );
    427         $this->assertEquals( 'tags-a-and-c', $posts[0]->post_name );
    428         $this->assertEquals( 'tag-a', $posts[1]->post_name );
     427        $this->assertSame( 'tags-a-and-c', $posts[0]->post_name );
     428        $this->assertSame( 'tag-a', $posts[1]->post_name );
    429429    }
    430430
     
    436436        // There are 4 posts with Cat A, we'll check for them by name.
    437437        $this->assertCount( 4, $posts );
    438         $this->assertEquals( 'cat-a', $posts[0]->post_name );
    439         $this->assertEquals( 'cats-a-and-c', $posts[1]->post_name );
    440         $this->assertEquals( 'cats-a-and-b', $posts[2]->post_name );
    441         $this->assertEquals( 'cats-a-b-c', $posts[3]->post_name );
     438        $this->assertSame( 'cat-a', $posts[0]->post_name );
     439        $this->assertSame( 'cats-a-and-c', $posts[1]->post_name );
     440        $this->assertSame( 'cats-a-and-b', $posts[2]->post_name );
     441        $this->assertSame( 'cats-a-b-c', $posts[3]->post_name );
    442442    }
    443443
     
    448448        // There are 4 posts with Cat B.
    449449        $this->assertCount( 4, $posts );
    450         $this->assertEquals( 'cat-b', $posts[0]->post_name );
    451         $this->assertEquals( 'cats-b-and-c', $posts[1]->post_name );
    452         $this->assertEquals( 'cats-a-and-b', $posts[2]->post_name );
    453         $this->assertEquals( 'cats-a-b-c', $posts[3]->post_name );
     450        $this->assertSame( 'cat-b', $posts[0]->post_name );
     451        $this->assertSame( 'cats-b-and-c', $posts[1]->post_name );
     452        $this->assertSame( 'cats-a-and-b', $posts[2]->post_name );
     453        $this->assertSame( 'cats-a-b-c', $posts[3]->post_name );
    454454    }
    455455
     
    466466
    467467        $this->assertCount( 5, $posts );
    468         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     468        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    469469    }
    470470
     
    486486
    487487        $this->assertCount( 10, $posts );
    488         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     488        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    489489    }
    490490
     
    507507        $this->assertCount( 10, $posts );
    508508        $this->assertTrue( $this->q->is_paged() );
    509         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     509        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    510510    }
    511511
     
    522522        $this->assertCount( 4, $posts );
    523523        $this->assertTrue( $this->q->is_paged() );
    524         $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) );
     524        $this->assertSame( $expected, wp_list_pluck( $posts, 'post_name' ) );
    525525    }
    526526
     
    538538        );
    539539
    540         $this->assertEquals(
     540        $this->assertSame(
    541541            array(
    542542                'child-one',
     
    555555        );
    556556
    557         $this->assertEquals(
     557        $this->assertSame(
    558558            array(
    559559                'child-three',
     
    572572        );
    573573
    574         $this->assertEquals(
     574        $this->assertSame(
    575575            array(
    576576                'child-one',
     
    589589        );
    590590
    591         $this->assertEquals( array(), wp_list_pluck( $posts, 'post_title' ) );
     591        $this->assertSame( array(), wp_list_pluck( $posts, 'post_title' ) );
    592592    }
    593593
     
    604604        );
    605605
    606         $this->assertEquals(
     606        $this->assertSame(
    607607            array(
    608608                'child-three',
     
    631631
    632632        // 'order=desc' does not influence the order of returned results (returns same order as 'order=asc').
    633         $this->assertEquals( $expected_returned_array, wp_list_pluck( $posts, 'post_title' ) );
     633        $this->assertSame( $expected_returned_array, wp_list_pluck( $posts, 'post_title' ) );
    634634    }
    635635
Note: See TracChangeset for help on using the changeset viewer.