Make WordPress Core


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

    r34810 r35225  
    3434
    3535    function test_wp_get_shortlink() {
    36         $post_id = $this->factory->post->create();
    37         $post_id2 = $this->factory->post->create();
     36        $post_id = self::$factory->post->create();
     37        $post_id2 = self::$factory->post->create();
    3838
    3939        // Basic case
     
    7878
    7979    function test_wp_get_shortlink_with_page() {
    80         $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     80        $post_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    8181
    8282        // Basic case
     
    9393     */
    9494    function test_wp_get_shortlink_with_home_page() {
    95         $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     95        $post_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    9696        update_option( 'show_on_front', 'page' );
    9797        update_option( 'page_on_front', $post_id );
     
    109109    function test_get_adjacent_post() {
    110110        // Need some sample posts to test adjacency
    111         $post_one = $this->factory->post->create_and_get( array(
     111        $post_one = self::$factory->post->create_and_get( array(
    112112            'post_title' => 'First',
    113113            'post_date' => '2012-01-01 12:00:00'
    114114        ) );
    115115
    116         $post_two = $this->factory->post->create_and_get( array(
     116        $post_two = self::$factory->post->create_and_get( array(
    117117            'post_title' => 'Second',
    118118            'post_date' => '2012-02-01 12:00:00'
    119119        ) );
    120120
    121         $post_three = $this->factory->post->create_and_get( array(
     121        $post_three = self::$factory->post->create_and_get( array(
    122122            'post_title' => 'Third',
    123123            'post_date' => '2012-03-01 12:00:00'
    124124        ) );
    125125
    126         $post_four = $this->factory->post->create_and_get( array(
     126        $post_four = self::$factory->post->create_and_get( array(
    127127            'post_title' => 'Fourth',
    128128            'post_date' => '2012-04-01 12:00:00'
     
    184184        $wpdb->insert( $wpdb->term_taxonomy, array( 'taxonomy' => 'foo', 'term_id' => 12345, 'description' => '' ) );
    185185
    186         $include = $this->factory->term->create( array(
     186        $include = self::$factory->term->create( array(
    187187            'taxonomy' => 'category',
    188188            'name' => 'Include',
    189189        ) );
    190         $exclude = $this->factory->category->create();
    191 
    192         $one = $this->factory->post->create_and_get( array(
     190        $exclude = self::$factory->category->create();
     191
     192        $one = self::$factory->post->create_and_get( array(
    193193            'post_date' => '2012-01-01 12:00:00',
    194194            'post_category' => array( $include, $exclude ),
    195195        ) );
    196196
    197         $two = $this->factory->post->create_and_get( array(
     197        $two = self::$factory->post->create_and_get( array(
    198198            'post_date' => '2012-01-02 12:00:00',
    199199            'post_category' => array(),
    200200        ) );
    201201
    202         $three = $this->factory->post->create_and_get( array(
     202        $three = self::$factory->post->create_and_get( array(
    203203            'post_date' => '2012-01-03 12:00:00',
    204204            'post_category' => array( $include, $exclude ),
    205205        ) );
    206206
    207         $four = $this->factory->post->create_and_get( array(
     207        $four = self::$factory->post->create_and_get( array(
    208208            'post_date' => '2012-01-04 12:00:00',
    209209            'post_category' => array( $include ),
    210210        ) );
    211211
    212         $five = $this->factory->post->create_and_get( array(
     212        $five = self::$factory->post->create_and_get( array(
    213213            'post_date' => '2012-01-05 12:00:00',
    214214            'post_category' => array( $include, $exclude ),
     
    250250        register_taxonomy( 'wptests_tax', 'post' );
    251251
    252         $t = $this->factory->term->create( array(
     252        $t = self::$factory->term->create( array(
    253253            'taxonomy' => 'wptests_tax',
    254254        ) );
    255255
    256         $p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
    257         $p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
    258         $p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
     256        $p1 = self::$factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
     257        $p2 = self::$factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
     258        $p3 = self::$factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
    259259
    260260        wp_set_post_terms( $p2, array( $t ), 'wptests_tax' );
     
    282282        register_taxonomy( 'wptests_tax', 'post' );
    283283
    284         $t = $this->factory->term->create( array(
     284        $t = self::$factory->term->create( array(
    285285            'taxonomy' => 'wptests_tax',
    286286        ) );
    287287
    288         $p1 = $this->factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
    289         $p2 = $this->factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
    290         $p3 = $this->factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
     288        $p1 = self::$factory->post->create( array( 'post_date' => '2015-08-27 12:00:00' ) );
     289        $p2 = self::$factory->post->create( array( 'post_date' => '2015-08-26 12:00:00' ) );
     290        $p3 = self::$factory->post->create( array( 'post_date' => '2015-08-25 12:00:00' ) );
    291291
    292292        wp_set_post_terms( $p2, array( $t ), 'wptests_tax' );
     
    349349        flush_rewrite_rules();
    350350
    351         $p = $this->factory->post->create( array(
     351        $p = self::$factory->post->create( array(
    352352            'post_status' => 'publish',
    353353            'post_date'   => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) )
     
    369369        flush_rewrite_rules();
    370370
    371         $p = $this->factory->post->create( array(
     371        $p = self::$factory->post->create( array(
    372372            'post_status' => 'future',
    373373            'post_type'   => 'wptests_pt',
     
    389389        $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    390390
    391         $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     391        $attachment_id = self::$factory->attachment->create_object( 'image.jpg', 0, array(
    392392            'post_mime_type' => 'image/jpeg',
    393393            'post_type' => 'attachment',
     
    413413        flush_rewrite_rules();
    414414
    415         $post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
    416 
    417         $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
     415        $post_id = self::$factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
     416
     417        $attachment_id = self::$factory->attachment->create_object( 'image.jpg', $post_id, array(
    418418            'post_mime_type' => 'image/jpeg',
    419419            'post_type' => 'attachment',
Note: See TracChangeset for help on using the changeset viewer.