Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (11 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/post/query.php

    r35162 r35225  
    1212        $q = new WP_Query();
    1313
    14         $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
    15         $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
    16         $post_id = $this->factory->post->create();
     14        $term_id = self::$factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
     15        $term_id2 = self::$factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
     16        $post_id = self::$factory->post->create();
    1717
    1818        wp_set_post_categories( $post_id, $term_id );
     
    4242     */
    4343    function test_empty_category__in() {
    44         $cat_id = $this->factory->category->create();
    45         $post_id = $this->factory->post->create();
     44        $cat_id = self::$factory->category->create();
     45        $post_id = self::$factory->post->create();
    4646        wp_set_post_categories( $post_id, $cat_id );
    4747
     
    7272    function test_the_posts_filter() {
    7373        // Create posts and clear their caches.
    74         $post_ids = $this->factory->post->create_many( 4 );
     74        $post_ids = self::$factory->post->create_many( 4 );
    7575        foreach ( $post_ids as $post_id )
    7676            clean_post_cache( $post_id );
     
    116116
    117117    function test_post__in_ordering() {
    118         $post_id1 = $this->factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
    119         $post_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
    120         $post_id3 = $this->factory->post->create( array(
     118        $post_id1 = self::$factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
     119        $post_id2 = self::$factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
     120        $post_id3 = self::$factory->post->create( array(
    121121            'post_type' => 'page',
    122122            'post_parent' => $post_id2,
    123123            'menu_order' => rand( 1, 100 )
    124124        ) );
    125         $post_id4 = $this->factory->post->create( array(
     125        $post_id4 = self::$factory->post->create( array(
    126126            'post_type' => 'page',
    127127            'post_parent' => $post_id2,
    128128            'menu_order' => rand( 1, 100 )
    129129        ) );
    130         $post_id5 = $this->factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
     130        $post_id5 = self::$factory->post->create( array( 'post_type' => 'page', 'menu_order' => rand( 1, 100 ) ) );
    131131
    132132        $ordered = array( $post_id2, $post_id4, $post_id3, $post_id1, $post_id5 );
     
    141141
    142142    function test_post__in_attachment_ordering() {
    143         $post_id = $this->factory->post->create();
     143        $post_id = self::$factory->post->create();
    144144        $att_ids = array();
    145145        $file = DIR_TESTDATA . '/images/canola.jpg';
    146         $att_ids[1] = $this->factory->attachment->create_object( $file, $post_id, array(
    147             'post_mime_type' => 'image/jpeg',
    148             'menu_order' => rand( 1, 100 )
    149         ) );
    150         $att_ids[2] = $this->factory->attachment->create_object( $file, $post_id, array(
    151             'post_mime_type' => 'image/jpeg',
    152             'menu_order' => rand( 1, 100 )
    153         ) );
    154         $att_ids[3] = $this->factory->attachment->create_object( $file, $post_id, array(
    155             'post_mime_type' => 'image/jpeg',
    156             'menu_order' => rand( 1, 100 )
    157         ) );
    158         $att_ids[4] = $this->factory->attachment->create_object( $file, $post_id, array(
    159             'post_mime_type' => 'image/jpeg',
    160             'menu_order' => rand( 1, 100 )
    161         ) );
    162         $att_ids[5] = $this->factory->attachment->create_object( $file, $post_id, array(
     146        $att_ids[1] = self::$factory->attachment->create_object( $file, $post_id, array(
     147            'post_mime_type' => 'image/jpeg',
     148            'menu_order' => rand( 1, 100 )
     149        ) );
     150        $att_ids[2] = self::$factory->attachment->create_object( $file, $post_id, array(
     151            'post_mime_type' => 'image/jpeg',
     152            'menu_order' => rand( 1, 100 )
     153        ) );
     154        $att_ids[3] = self::$factory->attachment->create_object( $file, $post_id, array(
     155            'post_mime_type' => 'image/jpeg',
     156            'menu_order' => rand( 1, 100 )
     157        ) );
     158        $att_ids[4] = self::$factory->attachment->create_object( $file, $post_id, array(
     159            'post_mime_type' => 'image/jpeg',
     160            'menu_order' => rand( 1, 100 )
     161        ) );
     162        $att_ids[5] = self::$factory->attachment->create_object( $file, $post_id, array(
    163163            'post_mime_type' => 'image/jpeg',
    164164            'menu_order' => rand( 1, 100 )
     
    312312        $q = new WP_Query();
    313313
    314         $post_ids[0] = $this->factory->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) );
    315         $post_ids[1] = $this->factory->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) );
    316         $post_ids[2] = $this->factory->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) );
    317         $post_ids[3] = $this->factory->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) );
     314        $post_ids[0] = self::$factory->post->create( array( 'post_title' => 'woo', 'post_date' => '2015-07-23 00:00:00' ) );
     315        $post_ids[1] = self::$factory->post->create( array( 'post_title' => 'hoo', 'post_date' => '2015-07-23 00:00:00' ) );
     316        $post_ids[2] = self::$factory->post->create( array( 'post_title' => 'test', 'post_date' => '2015-07-23 00:00:00' ) );
     317        $post_ids[3] = self::$factory->post->create( array( 'post_title' => 'me', 'post_date' => '2015-07-23 00:00:00' ) );
    318318
    319319        $requested = array( $post_ids[0], $post_ids[3] );
Note: See TracChangeset for help on using the changeset viewer.