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/taxonomy.php

    r34247 r35225  
    3535
    3636        function test_get_the_taxonomies() {
    37                 $post_id = $this->factory->post->create();
     37                $post_id = self::$factory->post->create();
    3838
    3939                $taxes = get_the_taxonomies( $post_id );
    … …  
    4141                $this->assertEquals( array( 'category' ), array_keys( $taxes ) );
    4242
    43                 $id = $this->factory->tag->create();
     43                $id = self::$factory->tag->create();
    4444                wp_set_post_tags( $post_id, array( $id ) );
    4545
    … …  
    5454         */
    5555        public function test_get_the_taxonomies_term_template() {
    56                 $post_id = $this->factory->post->create();
     56                $post_id = self::$factory->post->create();
    5757
    5858                $taxes = get_the_taxonomies( $post_id, array( 'term_template' => '%2$s' ) );
    … …  
    6565
    6666        function test_the_taxonomies() {
    67                 $post_id = $this->factory->post->create();
     67                $post_id = self::$factory->post->create();
    6868
    6969                ob_start();
    … …  
    8080         */
    8181        function test_the_taxonomies_term_template() {
    82                 $post_id = $this->factory->post->create();
     82                $post_id = self::$factory->post->create();
    8383
    8484                $output = get_echo( 'the_taxonomies', array( array( 'post' => $post_id, 'term_template' => '%2$s' ) ) );
    … …  
    263263
    264264        public function test_get_objects_in_term_should_return_objects_ids() {
    265                 $tag_id = $this->factory->tag->create();
    266                 $cat_id = $this->factory->category->create();
     265                $tag_id = self::$factory->tag->create();
     266                $cat_id = self::$factory->category->create();
    267267                $posts_with_tag = array();
    268268                $posts_with_category = array();
    269269
    270270                for ( $i = 0; $i < 3; $i++ ) {
    271                         $post_id = $this->factory->post->create();
     271                        $post_id = self::$factory->post->create();
    272272                        wp_set_post_tags( $post_id, array( $tag_id ) );
    273273                        $posts_with_tag[] = $post_id;
    … …  
    275275
    276276                for ( $i = 0; $i < 3; $i++ ) {
    277                         $post_id = $this->factory->post->create();
     277                        $post_id = self::$factory->post->create();
    278278                        wp_set_post_categories( $post_id, array( $cat_id ) );
    279279                        $posts_with_category[] = $post_id;
    … …  
    281281
    282282                for ( $i = 0; $i < 3; $i++ ) {
    283                         $this->factory->post->create();
     283                        self::$factory->post->create();
    284284                }
    285285
    … …  
    296296         */
    297297        function test_in_category() {
    298                 $post = $this->factory->post->create_and_get();
     298                $post = self::$factory->post->create_and_get();
    299299
    300300                // in_category() returns false when first parameter is empty()
    … …  
    347347        public function test_get_ancestors_taxonomy_non_hierarchical() {
    348348                register_taxonomy( 'wptests_tax', 'post' );
    349                 $t = $this->factory->term->create( array(
     349                $t = self::$factory->term->create( array(
    350350                        'taxonomy' => 'wptests_tax',
    351351                ) );
    … …  
    359359                        'hierarchical' => true,
    360360                ) );
    361                 $t1 = $this->factory->term->create( array(
    362                         'taxonomy' => 'wptests_tax',
    363                 ) );
    364                 $t2 = $this->factory->term->create( array(
     361                $t1 = self::$factory->term->create( array(
     362                        'taxonomy' => 'wptests_tax',
     363                ) );
     364                $t2 = self::$factory->term->create( array(
    365365                        'taxonomy' => 'wptests_tax',
    366366                        'parent' => $t1,
    367367                ) );
    368                 $t3 = $this->factory->term->create( array(
     368                $t3 = self::$factory->term->create( array(
    369369                        'taxonomy' => 'wptests_tax',
    370370                        'parent' => $t2,
    371371                ) );
    372                 $t4 = $this->factory->term->create( array(
     372                $t4 = self::$factory->term->create( array(
    373373                        'taxonomy' => 'wptests_tax',
    374374                        'parent' => $t1,
    … …  
    381381        public function test_get_ancestors_post_type_non_hierarchical() {
    382382                register_post_type( 'wptests_pt' );
    383                 $p = $this->factory->post->create( array(
     383                $p = self::$factory->post->create( array(
    384384                        'taxonomy' => 'wptests_pt',
    385385                ) );
    … …  
    392392                        'hierarchical' => true,
    393393                ) );
    394                 $p1 = $this->factory->post->create( array(
     394                $p1 = self::$factory->post->create( array(
    395395                        'post_type' => 'wptests_pt',
    396396                ) );
    397                 $p2 = $this->factory->post->create( array(
     397                $p2 = self::$factory->post->create( array(
    398398                        'post_type' => 'wptests_pt',
    399399                        'post_parent' => $p1,
    400400                ) );
    401                 $p3 = $this->factory->post->create( array(
     401                $p3 = self::$factory->post->create( array(
    402402                        'post_type' => 'wptests_pt',
    403403                        'post_parent' => $p2,
    404404                ) );
    405                 $p4 = $this->factory->post->create( array(
     405                $p4 = self::$factory->post->create( array(
    406406                        'post_type' => 'wptests_pt',
    407407                        'post_parent' => $p1,
    … …  
    419419                        'hierarchical' => true,
    420420                ) );
    421                 $p1 = $this->factory->post->create( array(
     421                $p1 = self::$factory->post->create( array(
    422422                        'post_type' => 'wptests_conflict',
    423423                ) );
    424                 $p2 = $this->factory->post->create( array(
     424                $p2 = self::$factory->post->create( array(
    425425                        'post_type' => 'wptests_conflict',
    426426                        'post_parent' => $p1,
    … …  
    430430                        'hierarchical' => true,
    431431                ) );
    432                 $t1 = $this->factory->term->create( array(
     432                $t1 = self::$factory->term->create( array(
    433433                        'taxonomy' => 'wptests_conflict',
    434434                ) );
    435                 $t2 = $this->factory->term->create( array(
     435                $t2 = self::$factory->term->create( array(
    436436                        'taxonomy' => 'wptests_conflict',
    437437                        'parent' => $t1,
    … …  
    452452                ) );
    453453
    454                 $t = $this->factory->term->create_and_get( array(
    455                         'taxonomy' => 'wptests_tax',
    456                 ) );
    457 
    458                 $p = $this->factory->post->create();
     454                $t = self::$factory->term->create_and_get( array(
     455                        'taxonomy' => 'wptests_tax',
     456                ) );
     457
     458                $p = self::$factory->post->create();
    459459                wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
    460460
    … …  
    472472                ) );
    473473
    474                 $t = $this->factory->term->create_and_get( array(
    475                         'taxonomy' => 'wptests_tax',
    476                 ) );
    477 
    478                 $p = $this->factory->post->create();
     474                $t = self::$factory->term->create_and_get( array(
     475                        'taxonomy' => 'wptests_tax',
     476                ) );
     477
     478                $p = self::$factory->post->create();
    479479                wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
    480480
Note: See TracChangeset for help on using the changeset viewer.