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/nav-menu.php

    r32294 r35225  
    1717
    1818    function test_wp_get_associated_nav_menu_items() {
    19         $tag_id = $this->factory->tag->create();
    20         $cat_id = $this->factory->category->create();
    21         $post_id = $this->factory->post->create();
    22         $post_2_id = $this->factory->post->create();
    23         $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     19        $tag_id = self::$factory->tag->create();
     20        $cat_id = self::$factory->category->create();
     21        $post_id = self::$factory->post->create();
     22        $post_2_id = self::$factory->post->create();
     23        $page_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    2424
    2525        $tag_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     
    108108
    109109        // Update the orphan with an associated nav menu
    110         wp_update_nav_menu_item( $this->menu_id, $custom_item_id, array( 
     110        wp_update_nav_menu_item( $this->menu_id, $custom_item_id, array(
    111111            'menu-item-title'     => 'WordPress.org',
    112112            ) );
     
    120120    public function test_wp_get_nav_menu_items_with_taxonomy_term() {
    121121        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
    122         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    123         $child_terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax', 'parent' => $t ) );
     122        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     123        $child_terms = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax', 'parent' => $t ) );
    124124
    125125        $term_menu_item = wp_update_nav_menu_item( $this->menu_id, 0, array(
     
    141141     */
    142142    function test_orderby_name_by_default() {
    143         // We are going to create a random number of menus (min 2, max 10) 
     143        // We are going to create a random number of menus (min 2, max 10)
    144144        $menus_no = rand( 2, 10 );
    145145
     
    149149
    150150        // This is the expected array of menu names
    151         $expected_nav_menus_names = wp_list_pluck( 
     151        $expected_nav_menus_names = wp_list_pluck(
    152152            get_terms( 'nav_menu',  array( 'hide_empty' => false, 'orderby' => 'name' ) ),
    153153            'name'
     
    156156        // And this is what we got when calling wp_get_nav_menus()
    157157        $nav_menus_names = wp_list_pluck( wp_get_nav_menus(), 'name' );
    158        
     158
    159159        $this->assertEquals( $nav_menus_names, $expected_nav_menus_names );
    160160    }
Note: See TracChangeset for help on using the changeset viewer.