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/category/wpListCategories.php

    r34696 r35225  
    66class Tests_Category_WpListCategories extends WP_UnitTestCase {
    77    public function test_class() {
    8         $c = $this->factory->category->create();
     8        $c = self::$factory->category->create();
    99
    1010        $found = wp_list_categories( array(
     
    1717
    1818    public function test_class_containing_current_cat() {
    19         $c1 = $this->factory->category->create();
    20         $c2 = $this->factory->category->create();
     19        $c1 = self::$factory->category->create();
     20        $c2 = self::$factory->category->create();
    2121
    2222        $found = wp_list_categories( array(
     
    3131
    3232    public function test_class_containing_current_cat_parent() {
    33         $c1 = $this->factory->category->create();
    34         $c2 = $this->factory->category->create( array(
     33        $c1 = self::$factory->category->create();
     34        $c2 = self::$factory->category->create( array(
    3535            'parent' => $c1,
    3636        ) );
     
    5050     */
    5151    public function test_current_category_should_accept_an_array_of_ids() {
    52         $cats = $this->factory->category->create_many( 3 );
     52        $cats = self::$factory->category->create_many( 3 );
    5353
    5454        $found = wp_list_categories( array(
     
    6767     */
    6868    public function test_should_not_create_element_when_cat_name_is_filtered_to_empty_string() {
    69         $c1 = $this->factory->category->create( array(
     69        $c1 = self::$factory->category->create( array(
    7070            'name' => 'Test Cat 1',
    7171        ) );
    72         $c2 = $this->factory->category->create( array(
     72        $c2 = self::$factory->category->create( array(
    7373            'name' => 'Test Cat 2',
    7474        ) );
     
    8989
    9090    public function test_show_option_all_link_should_go_to_home_page_when_show_on_front_is_false() {
    91         $cats = $this->factory->category->create_many( 2 );
     91        $cats = self::$factory->category->create_many( 2 );
    9292
    9393        $found = wp_list_categories( array(
     
    102102
    103103    public function test_show_option_all_link_should_respect_page_for_posts() {
    104         $cats = $this->factory->category->create_many( 2 );
    105         $p = $this->factory->post->create( array( 'post_type' => 'page' ) );
     104        $cats = self::$factory->category->create_many( 2 );
     105        $p = self::$factory->post->create( array( 'post_type' => 'page' ) );
    106106
    107107        update_option( 'show_on_front', 'page' );
     
    126126        register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
    127127
    128         $terms = $this->factory->term->create_many( 2, array(
     128        $terms = self::$factory->term->create_many( 2, array(
    129129            'taxonomy' => 'wptests_tax',
    130130        ) );
     
    150150        register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
    151151
    152         $terms = $this->factory->term->create_many( 2, array(
     152        $terms = self::$factory->term->create_many( 2, array(
    153153            'taxonomy' => 'wptests_tax',
    154154        ) );
     
    171171        register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'post', 'wptests_pt2' ) );
    172172
    173         $terms = $this->factory->term->create_many( 2, array(
     173        $terms = self::$factory->term->create_many( 2, array(
    174174            'taxonomy' => 'wptests_tax',
    175175        ) );
     
    192192        register_taxonomy( 'wptests_tax', array( 'foo', 'wptests_pt', 'wptests_pt2' ) );
    193193
    194         $terms = $this->factory->term->create_many( 2, array(
     194        $terms = self::$factory->term->create_many( 2, array(
    195195            'taxonomy' => 'wptests_tax',
    196196        ) );
     
    255255     */
    256256    public function test_hide_title_if_empty_should_be_ignored_when_category_list_is_not_empty() {
    257         $cat = $this->factory->category->create();
     257        $cat = self::$factory->category->create();
    258258
    259259        $found = wp_list_categories( array(
     
    270270     */
    271271    public function test_exclude_tree_should_be_respected() {
    272         $c = $this->factory->category->create();
    273         $parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
    274         $child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
     272        $c = self::$factory->category->create();
     273        $parent = self::$factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
     274        $child = self::$factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
    275275
    276276        $args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
     
    287287     */
    288288    public function test_exclude_tree_should_be_merged_with_exclude() {
    289         $c = $this->factory->category->create();
    290         $parent = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
    291         $child = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
    292         $parent2 = $this->factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
    293         $child2 = $this->factory->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
     289        $c = self::$factory->category->create();
     290        $parent = self::$factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
     291        $child = self::$factory->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
     292        $parent2 = self::$factory->category->create( array( 'name' => 'Parent', 'slug' => 'parent2' ) );
     293        $child2 = self::$factory->category->create( array( 'name' => 'Child', 'slug' => 'child2', 'parent' => $parent2 ) );
    294294
    295295        $args = array( 'echo' => 0, 'hide_empty' => 0, 'exclude_tree' => $parent );
Note: See TracChangeset for help on using the changeset viewer.