WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (6 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/xmlrpc/wp/editPost.php

    r34855 r35225  
    127127        // create attachment
    128128        $filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
    129         $attachment_id = $this->factory->attachment->create_upload_object( $filename, $post_id );
     129        $attachment_id = self::$factory->attachment->create_upload_object( $filename, $post_id );
    130130
    131131        // add post thumbnail to post that does not have one
     
    149149
    150150        // create another attachment
    151         $attachment2_id = $this->factory->attachment->create_upload_object( $filename, $post_id );
     151        $attachment2_id = self::$factory->attachment->create_upload_object( $filename, $post_id );
    152152
    153153        // change the post's post_thumbnail
     
    209209        $editor_id = $this->make_user_by_role( 'editor' );
    210210
    211         $post_id = $this->factory->post->create( array( 'post_author' => $editor_id ) );
     211        $post_id = self::$factory->post->create( array( 'post_author' => $editor_id ) );
    212212        stick_post( $post_id );
    213213
     
    221221        // when transitioning to private status or adding a post password, post should be un-stuck
    222222        $editor_id = $this->make_user_by_role( 'editor' );
    223         $post_id = $this->factory->post->create( array( 'post_author' => $editor_id ) );
     223        $post_id = self::$factory->post->create( array( 'post_author' => $editor_id ) );
    224224        stick_post( $post_id );
    225225
     
    235235        $yesterday = strtotime( '-1 day' );
    236236
    237         $post_id = $this->factory->post->create( array(
     237        $post_id = self::$factory->post->create( array(
    238238            'post_title'   => 'Post Revision Test',
    239239            'post_content' => 'Not edited',
     
    264264        $editor_id = $this->make_user_by_role( 'editor' );
    265265
    266         $post_id = $this->factory->post->create( array(
     266        $post_id = self::$factory->post->create( array(
    267267            'post_title'   => 'Post Revision Test',
    268268            'post_content' => 'Not edited',
     
    283283        $editor_id = $this->make_user_by_role( 'editor' );
    284284
    285         $post_id = $this->factory->post->create( array(
     285        $post_id = self::$factory->post->create( array(
    286286            'post_title'   => 'Post Revision Test',
    287287            'post_content' => 'Not edited',
     
    303303        $editor_id = $this->make_user_by_role( 'editor' );
    304304
    305         $post_id = $this->factory->post->create( array( 'post_author'  => $editor_id ) );
    306         $term_id = $this->factory->category->create();
    307         $this->factory->term->add_post_terms( $post_id, $term_id, 'category', true );
     305        $post_id = self::$factory->post->create( array( 'post_author'  => $editor_id ) );
     306        $term_id = self::$factory->category->create();
     307        self::$factory->term->add_post_terms( $post_id, $term_id, 'category', true );
    308308        $term_ids = wp_list_pluck( get_the_category( $post_id ), 'term_id' );
    309309        $this->assertContains( $term_id, $term_ids );
     
    323323        $editor_id = $this->make_user_by_role( 'editor' );
    324324
    325         $post_id = $this->factory->post->create( array( 'post_author'  => $editor_id ) );
    326         $term_id = $this->factory->category->create();
    327         $this->factory->term->add_post_terms( $post_id, $term_id, 'category', true );
     325        $post_id = self::$factory->post->create( array( 'post_author'  => $editor_id ) );
     326        $term_id = self::$factory->category->create();
     327        self::$factory->term->add_post_terms( $post_id, $term_id, 'category', true );
    328328        $term_ids = wp_list_pluck( get_the_category( $post_id ), 'term_id' );
    329329        $this->assertContains( $term_id, $term_ids );
     
    366366
    367367        // Add a dummy post
    368         $post_id = $this->factory->post->create( array(
     368        $post_id = self::$factory->post->create( array(
    369369            'post_title'   => 'Post Enclosure Test',
    370370            'post_content' => 'Fake content',
Note: See TracChangeset for help on using the changeset viewer.