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

    r35112 r35225  
    1313
    1414        public function test_add() {
    15                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     15                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    1616
    1717                $this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
    … …  
    1919
    2020        public function test_add_unique() {
    21                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     21                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    2222
    2323                $this->assertNotEmpty( add_term_meta( $t, 'foo', 'bar' ) );
    … …  
    2626
    2727        public function test_delete() {
    28                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     28                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    2929                add_term_meta( $t, 'foo', 'bar' );
    3030
    … …  
    3333
    3434        public function test_delete_with_invalid_meta_key_should_return_false() {
    35                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     35                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    3636
    3737                $this->assertFalse( delete_term_meta( $t, 'foo' ) );
    … …  
    3939
    4040        public function test_delete_should_respect_meta_value() {
    41                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     41                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    4242                add_term_meta( $t, 'foo', 'bar' );
    4343                add_term_meta( $t, 'foo', 'baz' );
    … …  
    5050
    5151        public function test_get_with_no_key_should_fetch_all_keys() {
    52                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     52                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    5353                add_term_meta( $t, 'foo', 'bar' );
    5454                add_term_meta( $t, 'foo1', 'baz' );
    … …  
    6464
    6565        public function test_get_with_key_should_fetch_all_for_key() {
    66                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     66                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    6767                add_term_meta( $t, 'foo', 'bar' );
    6868                add_term_meta( $t, 'foo', 'baz' );
    … …  
    7676
    7777        public function test_get_should_respect_single_true() {
    78                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     78                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    7979                add_term_meta( $t, 'foo', 'bar' );
    8080                add_term_meta( $t, 'foo', 'baz' );
    … …  
    8585
    8686        public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
    87                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     87                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    8888
    8989                $actual = update_term_meta( $t, 'foo', 'bar' );
    … …  
    9696
    9797        public function test_update_should_return_true_when_updating_existing_value_for_key() {
    98                 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     98                $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    9999
    100100                add_term_meta( $t, 'foo', 'bar' );
    … …  
    110110                global $wpdb;
    111111
    112                 $p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     112                $p = self::$factory->post->create( array( 'post_status' => 'publish' ) );
    113113
    114114                register_taxonomy( 'wptests_tax', 'post' );
    115                 $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     115                $terms = self::$factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
    116116                wp_set_object_terms( $p, $terms, 'wptests_tax' );
    117117                foreach ( $terms as $t ) {
    … …  
    120120
    121121                // Create another term, which should *not* be lazy loaded because it's unattached.
    122                 $orphan_term = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     122                $orphan_term = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    123123                add_term_meta( $orphan_term, 'foo', 'bar' );
    124124
    … …  
    155155                global $wpdb;
    156156
    157                 $posts = $this->factory->post->create_many( 3, array( 'post_status' => 'publish' ) );
     157                $posts = self::$factory->post->create_many( 3, array( 'post_status' => 'publish' ) );
    158158                register_taxonomy( 'wptests_tax', 'post' );
    159                 $terms = $this->factory->term->create_many( 6, array( 'taxonomy' => 'wptests_tax' ) );
     159                $terms = self::$factory->term->create_many( 6, array( 'taxonomy' => 'wptests_tax' ) );
    160160
    161161                wp_set_object_terms( $posts[0], array( $terms[0], $terms[1] ), 'wptests_tax' );
    … …  
    202202
    203203        public function test_adding_term_meta_should_bust_get_terms_cache() {
    204                 $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     204                $terms = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
    205205
    206206                add_term_meta( $terms[0], 'foo', 'bar' );
    … …  
    237237
    238238        public function test_updating_term_meta_should_bust_get_terms_cache() {
    239                 $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     239                $terms = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
    240240
    241241                add_term_meta( $terms[0], 'foo', 'bar' );
    … …  
    273273
    274274        public function test_deleting_term_meta_should_bust_get_terms_cache() {
    275                 $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     275                $terms = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
    276276
    277277                add_term_meta( $terms[0], 'foo', 'bar' );
Note: See TracChangeset for help on using the changeset viewer.