Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (9 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

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

    r34828 r35225  
    88
    99    public function test_last_comment() {
    10         $p = $this->factory->post->create();
     10        $p = self::$factory->post->create();
    1111
    1212        // page 4
    13         $comment_last = $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
    14         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-23 00:00:00' ) );
     13        $comment_last = self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-24 00:00:00' ) );
     14        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-23 00:00:00' ) );
    1515
    1616        // page 3
    17         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
    18         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
    19         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-20 00:00:00' ) );
     17        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-22 00:00:00' ) );
     18        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-21 00:00:00' ) );
     19        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-20 00:00:00' ) );
    2020
    2121        // page 2
    22         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
    23         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
    24         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-17 00:00:00' ) );
     22        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-19 00:00:00' ) );
     23        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-18 00:00:00' ) );
     24        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-17 00:00:00' ) );
    2525
    2626        // page 1
    27         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
    28         $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
    29         $comment_first = $this->factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
     27        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) );
     28        self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-15 00:00:00' ) );
     29        $comment_first = self::$factory->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) );
    3030
    3131        $this->assertEquals( 4, get_page_of_comment( $comment_last[0],  array( 'per_page' =>  3 ) ) );
     
    3737
    3838    public function test_type_pings() {
    39         $p = $this->factory->post->create();
     39        $p = self::$factory->post->create();
    4040        $now = time();
    4141
    4242        $trackbacks = array();
    4343        for ( $i = 0; $i <= 3; $i++ ) {
    44             $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     44            $trackbacks[ $i ] = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
    4545            $now -= 10 * $i;
    4646        }
     
    4848        $pingbacks = array();
    4949        for ( $i = 0; $i <= 6; $i++ ) {
    50             $pingbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     50            $pingbacks[ $i ] = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
    5151            $now -= 10 * $i;
    5252        }
     
    6363        global $wpdb;
    6464
    65         $p = $this->factory->post->create();
    66         $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     65        $p = self::$factory->post->create();
     66        $c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
    6767
    6868        // Prime cache.
     
    8282        global $wpdb;
    8383
    84         $p = $this->factory->post->create();
    85         $comment = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
     84        $p = self::$factory->post->create();
     85        $comment = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
    8686
    8787        $now = time();
    8888        $trackbacks = array();
    8989        for ( $i = 0; $i <= 5; $i++ ) {
    90             $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
     90            $trackbacks[ $i ] = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
    9191        }
    9292
     
    106106     */
    107107    public function test_cache_should_be_invalidated_when_comment_is_approved() {
    108         $p = $this->factory->post->create();
    109         $c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
     108        $p = self::$factory->post->create();
     109        $c = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
    110110
    111111        // Prime cache.
     
    122122     */
    123123    public function test_cache_should_be_invalidated_when_comment_is_deleted() {
    124         $p = $this->factory->post->create();
    125         $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     124        $p = self::$factory->post->create();
     125        $c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
    126126
    127127        // Prime cache.
     
    138138     */
    139139    public function test_cache_should_be_invalidated_when_comment_is_spammed() {
    140         $p = $this->factory->post->create();
    141         $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     140        $p = self::$factory->post->create();
     141        $c = self::$factory->comment->create( array( 'comment_post_ID' => $p ) );
    142142
    143143        // Prime cache.
     
    156156        $now = time();
    157157
    158         $p = $this->factory->post->create();
    159         $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
    160         $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
    161         $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
     158        $p = self::$factory->post->create();
     159        $c1 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     160        $c2 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
     161        $c3 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
    162162
    163163        $this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
     
    172172     */
    173173    public function test_query_should_be_limited_to_comments_on_the_proper_post() {
    174         $posts = $this->factory->post->create_many( 2 );
     174        $posts = self::$factory->post->create_many( 2 );
    175175
    176176        $now = time();
    177177        $comments_0 = $comments_1 = array();
    178178        for ( $i = 0; $i < 5; $i++ ) {
    179             $comments_0[] = $this->factory->comment->create( array( 'comment_post_ID' => $posts[0], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
    180             $comments_1[] = $this->factory->comment->create( array( 'comment_post_ID' => $posts[1], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
     179            $comments_0[] = self::$factory->comment->create( array( 'comment_post_ID' => $posts[0], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
     180            $comments_1[] = self::$factory->comment->create( array( 'comment_post_ID' => $posts[1], 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
    181181        }
    182182
     
    192192     */
    193193    public function test_only_top_level_comments_should_be_included_in_older_count() {
    194         $post = $this->factory->post->create();
     194        $post = self::$factory->post->create();
    195195
    196196        $now = time();
    197197        $comment_parents = $comment_children = array();
    198198        for ( $i = 0; $i < 5; $i++ ) {
    199             $parent = $this->factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
     199            $parent = self::$factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 60 ) ) ) );
    200200            $comment_parents[ $i ] = $parent;
    201201
    202             $child = $this->factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 59 ) ), 'comment_parent' => $parent ) );
     202            $child = self::$factory->comment->create( array( 'comment_post_ID' => $post, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( $i * 59 ) ), 'comment_parent' => $parent ) );
    203203            $comment_children[ $i ] = $child;
    204204        }
     
    229229        $now = time();
    230230
    231         $p = $this->factory->post->create();
    232         $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
    233         $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
    234         $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
     231        $p = self::$factory->post->create();
     232        $c1 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
     233        $c2 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
     234        $c3 = self::$factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
    235235
    236236        update_option( 'comments_per_page', 2 );
Note: See TracChangeset for help on using the changeset viewer.