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/term/wpUpdateTerm.php

    r34646 r35225  
    2121        public function test_wp_update_term_unslash_name() {
    2222                register_taxonomy( 'wptests_tax', 'post' );
    23                 $t = $this->factory->term->create( array(
     23                $t = self::$factory->term->create( array(
    2424                        'taxonomy' => 'wptests_tax',
    2525                ) );
    … …  
    3737        public function test_wp_update_term_unslash_description() {
    3838                register_taxonomy( 'wptests_tax', 'post' );
    39                 $t = $this->factory->term->create( array(
     39                $t = self::$factory->term->create( array(
    4040                        'taxonomy' => 'wptests_tax',
    4141                ) );
    … …  
    5353        public function test_wp_update_term_name_empty_string() {
    5454                register_taxonomy( 'wptests_tax', 'post' );
    55                 $t = $this->factory->term->create( array(
     55                $t = self::$factory->term->create( array(
    5656                        'taxonomy' => 'wptests_tax',
    5757                ) );
    … …  
    7777                $this->assertNull( term_exists( $fake_term_id, 'wptests_tax' ) );
    7878
    79                 $t = $this->factory->term->create( array(
     79                $t = self::$factory->term->create( array(
    8080                        'taxonomy' => 'wptests_tax',
    8181                ) );
    … …  
    9595        public function test_wp_update_term_slug_empty_string_while_not_updating_name() {
    9696                register_taxonomy( 'wptests_tax', 'post' );
    97                 $t = $this->factory->term->create( array(
     97                $t = self::$factory->term->create( array(
    9898                        'taxonomy' => 'wptests_tax',
    9999                        'name' => 'Foo Bar',
    … …  
    111111        public function test_wp_update_term_slug_empty_string_while_updating_name() {
    112112                register_taxonomy( 'wptests_tax', 'post' );
    113                 $t = $this->factory->term->create( array(
     113                $t = self::$factory->term->create( array(
    114114                        'taxonomy' => 'wptests_tax',
    115115                ) );
    … …  
    127127        public function test_wp_update_term_slug_set_slug() {
    128128                register_taxonomy( 'wptests_tax', 'post' );
    129                 $t = $this->factory->term->create( array(
     129                $t = self::$factory->term->create( array(
    130130                        'taxonomy' => 'wptests_tax',
    131131                ) );
    … …  
    146146                register_taxonomy( 'wptests_tax', 'post' );
    147147
    148                 $t1 = $this->factory->term->create( array(
     148                $t1 = self::$factory->term->create( array(
    149149                        'name' => 'Foo',
    150150                        'slug' => 'foo',
    … …  
    152152                ) );
    153153
    154                 $t2 = $this->factory->term->create( array(
     154                $t2 = self::$factory->term->create( array(
    155155                        'name' => 'Bar',
    156156                        'slug' => 'bar',
    … …  
    173173                register_taxonomy( 'wptests_tax_2', 'post' );
    174174
    175                 $t1 = $this->factory->term->create( array(
     175                $t1 = self::$factory->term->create( array(
    176176                        'name' => 'Foo',
    177177                        'slug' => 'foo',
    … …  
    179179                ) );
    180180
    181                 $t2 = $this->factory->term->create( array(
     181                $t2 = self::$factory->term->create( array(
    182182                        'name' => 'Foo',
    183183                        'slug' => 'bar',
    … …  
    203203                register_taxonomy( 'wptests_tax_2', 'post' );
    204204
    205                 $t1 = $this->factory->term->create( array(
     205                $t1 = self::$factory->term->create( array(
    206206                        'name' => 'Foo',
    207207                        'slug' => 'foo',
    … …  
    209209                ) );
    210210
    211                 $t2 = $this->factory->term->create( array(
     211                $t2 = self::$factory->term->create( array(
    212212                        'name' => 'Bar',
    213213                        'slug' => 'bar',
    … …  
    233233                ) );
    234234
    235                 $t1 = $this->factory->term->create( array(
     235                $t1 = self::$factory->term->create( array(
    236236                        'name' => 'Foo',
    237237                        'slug' => 'foo',
    … …  
    239239                ) );
    240240
    241                 $t2 = $this->factory->term->create( array(
     241                $t2 = self::$factory->term->create( array(
    242242                        'name' => 'Bar',
    243243                        'slug' => 'bar',
    … …  
    246246                ) );
    247247
    248                 $t3 = $this->factory->term->create( array(
     248                $t3 = self::$factory->term->create( array(
    249249                        'name' => 'Bar Child',
    250250                        'slug' => 'bar-child',
    … …  
    283283                );
    284284
    285                 $posts = $this->factory->post->create_many( 2 );
     285                $posts = self::$factory->post->create_many( 2 );
    286286                wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );
    287287                wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );
    … …  
    303303        public function test_wp_update_term_alias_of_no_term_group() {
    304304                register_taxonomy( 'wptests_tax', 'post' );
    305                 $t1 = $this->factory->term->create( array(
     305                $t1 = self::$factory->term->create( array(
    306306                        'taxonomy' => 'wptests_tax',
    307307                ) );
    … …  
    324324        public function test_wp_update_term_alias_of_existing_term_group() {
    325325                register_taxonomy( 'wptests_tax', 'post' );
    326                 $t1 = $this->factory->term->create( array(
     326                $t1 = self::$factory->term->create( array(
    327327                        'taxonomy' => 'wptests_tax',
    328328                ) );
    329329                $term_1 = get_term( $t1, 'wptests_tax' );
    330330
    331                 $t2 = $this->factory->term->create( array(
     331                $t2 = self::$factory->term->create( array(
    332332                        'taxonomy' => 'wptests_tax',
    333333                        'alias_of' => $term_1->slug,
    … …  
    360360        public function test_wp_update_term_slug_same_as_old_slug() {
    361361                register_taxonomy( 'wptests_tax', 'post' );
    362                 $t = $this->factory->term->create( array(
     362                $t = self::$factory->term->create( array(
    363363                        'taxonomy' => 'wptests_tax',
    364364                        'slug' => 'foo',
    … …  
    378378        public function test_wp_update_term_duplicate_slug_generated_due_to_empty_slug_param() {
    379379                register_taxonomy( 'wptests_tax', 'post' );
    380                 $t1 = $this->factory->term->create( array(
     380                $t1 = self::$factory->term->create( array(
    381381                        'taxonomy' => 'wptests_tax',
    382382                        'slug' => 'foo-bar',
    383383                ) );
    384                 $t2 = $this->factory->term->create( array(
     384                $t2 = self::$factory->term->create( array(
    385385                        'taxonomy' => 'wptests_tax',
    386386                        'name' => 'not foo bar',
    … …  
    403403                        'hierarchical' => true,
    404404                ) );
    405                 $p = $this->factory->term->create( array(
    406                         'taxonomy' => 'wptests_tax',
    407                 ) );
    408                 $t1 = $this->factory->term->create( array(
     405                $p = self::$factory->term->create( array(
     406                        'taxonomy' => 'wptests_tax',
     407                ) );
     408                $t1 = self::$factory->term->create( array(
    409409                        'taxonomy' => 'wptests_tax',
    410410                        'slug' => 'foo-bar',
    411411                ) );
    412                 $t2 = $this->factory->term->create( array(
     412                $t2 = self::$factory->term->create( array(
    413413                        'taxonomy' => 'wptests_tax',
    414414                ) );
    … …  
    429429        public function test_wp_update_term_duplicate_slug_failure() {
    430430                register_taxonomy( 'wptests_tax', 'post' );
    431                 $t1 = $this->factory->term->create( array(
     431                $t1 = self::$factory->term->create( array(
    432432                        'taxonomy' => 'wptests_tax',
    433433                        'slug' => 'foo-bar',
    434434                ) );
    435                 $t2 = $this->factory->term->create( array(
     435                $t2 = self::$factory->term->create( array(
    436436                        'taxonomy' => 'wptests_tax',
    437437                        'slug' => 'my-old-slug',
    … …  
    452452        public function test_wp_update_term_should_return_term_id_and_term_taxonomy_id() {
    453453                register_taxonomy( 'wptests_tax', 'post' );
    454                 $t = $this->factory->term->create( array(
     454                $t = self::$factory->term->create( array(
    455455                        'taxonomy' => 'wptests_tax',
    456456                ) );
    … …  
    478478        public function test_wp_update_term_should_return_int_values_for_term_id_and_term_taxonomy_id() {
    479479                register_taxonomy( 'wptests_tax', 'post' );
    480                 $t = $this->factory->term->create( array(
     480                $t = self::$factory->term->create( array(
    481481                        'taxonomy' => 'wptests_tax',
    482482                ) );
    … …  
    492492                register_taxonomy( 'wptests_tax_for_post', 'post' );
    493493                register_taxonomy( 'wptests_tax_for_page', 'page' );
    494                 $post = $this->factory->post->create();
    495                 $page = $this->factory->post->create( array(
     494                $post = self::$factory->post->create();
     495                $page = self::$factory->post->create( array(
    496496                        'post_type' => 'page',
    497497                ) );
    498498
    499                 $t_for_post = $this->factory->term->create( array(
     499                $t_for_post = self::$factory->term->create( array(
    500500                        'taxonomy' => 'wptests_tax_for_post',
    501501                ) );
    502                 $t_for_page = $this->factory->term->create( array(
     502                $t_for_page = self::$factory->term->create( array(
    503503                        'taxonomy' => 'wptests_tax_for_page',
    504504                ) );
    … …  
    528528                ) );
    529529
    530                 $t1 = $this->factory->term->create( array(
    531                         'taxonomy' => 'wptests_tax',
    532                 ) );
    533                 $t2 = $this->factory->term->create( array(
     530                $t1 = self::$factory->term->create( array(
     531                        'taxonomy' => 'wptests_tax',
     532                ) );
     533                $t2 = self::$factory->term->create( array(
    534534                        'taxonomy' => 'wptests_tax',
    535535                ) );
    … …  
    568568                ) );
    569569
    570                 $t1 = $this->factory->term->create( array(
     570                $t1 = self::$factory->term->create( array(
    571571                        'taxonomy' => 'wptests_tax',
    572572                        'slug' => 'parent-term',
    573573                ) );
    574574
    575                 $t2 = $this->factory->term->create( array(
     575                $t2 = self::$factory->term->create( array(
    576576                        'taxonomy' => 'wptests_tax',
    577577                        'slug' => 'foo',
    … …  
    600600                ) );
    601601
    602                 $t1 = $this->factory->term->create( array(
     602                $t1 = self::$factory->term->create( array(
    603603                        'taxonomy' => 'wptests_tax',
    604604                        'slug' => 'parent-term',
    … …  
    606606
    607607                // Same slug but in a different tax.
    608                 $t2 = $this->factory->term->create( array(
     608                $t2 = self::$factory->term->create( array(
    609609                        'taxonomy' => 'wptests_tax_2',
    610610                        'slug' => 'foo',
    611611                ) );
    612612
    613                 $t3 = $this->factory->term->create( array(
     613                $t3 = self::$factory->term->create( array(
    614614                        'taxonomy' => 'wptests_tax',
    615615                        'slug' => 'foo',
    … …  
    631631         */
    632632        public function test_wp_update_term_with_null_get_term() {
    633                 $t = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
     633                $t = self::$factory->term->create( array( 'taxonomy' => 'category' ) );
    634634                $found = wp_update_term( $t, 'post_tag', array( 'slug' => 'foo' ) );
    635635
Note: See TracChangeset for help on using the changeset viewer.