Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (9 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/wpGetObjectTerms.php

    r34999 r35225  
    1414
    1515    public function test_get_object_terms_by_slug() {
    16         $post_id = $this->factory->post->create();
     16        $post_id = self::$factory->post->create();
    1717
    1818        $terms_1 = array('Foo', 'Bar', 'Baz');
     
    3232     */
    3333    public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects() {
    34         $post_id1 = $this->factory->post->create();
    35         $post_id2 = $this->factory->post->create();
    36         $cat_id = $this->factory->category->create();
    37         $cat_id2 = $this->factory->category->create();
     34        $post_id1 = self::$factory->post->create();
     35        $post_id2 = self::$factory->post->create();
     36        $cat_id = self::$factory->category->create();
     37        $cat_id2 = self::$factory->category->create();
    3838        wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );
    3939        wp_set_post_categories( $post_id2, $cat_id );
     
    5555     */
    5656    public function test_should_return_objects_with_int_properties() {
    57         $post_id = $this->factory->post->create();
     57        $post_id = self::$factory->post->create();
    5858        $term = wp_insert_term( 'one', $this->taxonomy );
    5959        wp_set_object_terms( $post_id, $term, $this->taxonomy );
     
    7474     */
    7575    public function test_references_should_be_reset_after_wp_get_object_terms_filter() {
    76         $post_id = $this->factory->post->create();
     76        $post_id = self::$factory->post->create();
    7777        $terms_1 = array('foo', 'bar', 'baz');
    7878
     
    8787
    8888    public function test_orderby_name() {
    89         $p = $this->factory->post->create();
    90 
    91         $t1 = $this->factory->term->create( array(
     89        $p = self::$factory->post->create();
     90
     91        $t1 = self::$factory->term->create( array(
    9292            'taxonomy' => $this->taxonomy,
    9393            'name' => 'AAA',
    9494        ) );
    95         $t2 = $this->factory->term->create( array(
     95        $t2 = self::$factory->term->create( array(
    9696            'taxonomy' => $this->taxonomy,
    9797            'name' => 'ZZZ',
    9898        ) );
    99         $t3 = $this->factory->term->create( array(
     99        $t3 = self::$factory->term->create( array(
    100100            'taxonomy' => $this->taxonomy,
    101101            'name' => 'JJJ',
     
    113113
    114114    public function test_orderby_count() {
    115         $posts = $this->factory->post->create_many( 3 );
    116 
    117         $t1 = $this->factory->term->create( array(
     115        $posts = self::$factory->post->create_many( 3 );
     116
     117        $t1 = self::$factory->term->create( array(
    118118            'taxonomy' => $this->taxonomy,
    119119            'name' => 'AAA',
    120120        ) );
    121         $t2 = $this->factory->term->create( array(
     121        $t2 = self::$factory->term->create( array(
    122122            'taxonomy' => $this->taxonomy,
    123123            'name' => 'ZZZ',
    124124        ) );
    125         $t3 = $this->factory->term->create( array(
     125        $t3 = self::$factory->term->create( array(
    126126            'taxonomy' => $this->taxonomy,
    127127            'name' => 'JJJ',
     
    141141
    142142    public function test_orderby_slug() {
    143         $p = $this->factory->post->create();
    144 
    145         $t1 = $this->factory->term->create( array(
     143        $p = self::$factory->post->create();
     144
     145        $t1 = self::$factory->term->create( array(
    146146            'taxonomy' => $this->taxonomy,
    147147            'slug' => 'aaa',
    148148        ) );
    149         $t2 = $this->factory->term->create( array(
     149        $t2 = self::$factory->term->create( array(
    150150            'taxonomy' => $this->taxonomy,
    151151            'slug' => 'zzz',
    152152        ) );
    153         $t3 = $this->factory->term->create( array(
     153        $t3 = self::$factory->term->create( array(
    154154            'taxonomy' => $this->taxonomy,
    155155            'slug' => 'jjj',
     
    167167
    168168    public function test_orderby_term_group() {
    169         $p = $this->factory->post->create();
    170 
    171         $t1 = $this->factory->term->create( array(
    172             'taxonomy' => $this->taxonomy,
    173         ) );
    174         $t2 = $this->factory->term->create( array(
    175             'taxonomy' => $this->taxonomy,
    176         ) );
    177         $t3 = $this->factory->term->create( array(
     169        $p = self::$factory->post->create();
     170
     171        $t1 = self::$factory->term->create( array(
     172            'taxonomy' => $this->taxonomy,
     173        ) );
     174        $t2 = self::$factory->term->create( array(
     175            'taxonomy' => $this->taxonomy,
     176        ) );
     177        $t3 = self::$factory->term->create( array(
    178178            'taxonomy' => $this->taxonomy,
    179179        ) );
     
    196196
    197197    public function test_orderby_term_order() {
    198         $p = $this->factory->post->create();
    199 
    200         $t1 = $this->factory->term->create( array(
    201             'taxonomy' => $this->taxonomy,
    202         ) );
    203         $t2 = $this->factory->term->create( array(
    204             'taxonomy' => $this->taxonomy,
    205         ) );
    206         $t3 = $this->factory->term->create( array(
     198        $p = self::$factory->post->create();
     199
     200        $t1 = self::$factory->term->create( array(
     201            'taxonomy' => $this->taxonomy,
     202        ) );
     203        $t2 = self::$factory->term->create( array(
     204            'taxonomy' => $this->taxonomy,
     205        ) );
     206        $t3 = self::$factory->term->create( array(
    207207            'taxonomy' => $this->taxonomy,
    208208        ) );
     
    232232     */
    233233    public function test_orderby_parent() {
    234         $p = $this->factory->post->create();
    235 
    236         $t1 = $this->factory->term->create( array(
    237             'taxonomy' => $this->taxonomy,
    238         ) );
    239         $t2 = $this->factory->term->create( array(
    240             'taxonomy' => $this->taxonomy,
    241         ) );
    242         $t3 = $this->factory->term->create( array(
     234        $p = self::$factory->post->create();
     235
     236        $t1 = self::$factory->term->create( array(
     237            'taxonomy' => $this->taxonomy,
     238        ) );
     239        $t2 = self::$factory->term->create( array(
     240            'taxonomy' => $this->taxonomy,
     241        ) );
     242        $t3 = self::$factory->term->create( array(
    243243            'taxonomy' => $this->taxonomy,
    244244        ) );
     
    270270        register_taxonomy( 'wptests_tax_3', 'post' );
    271271
    272         $p = $this->factory->post->create();
    273 
    274         $t1 = $this->factory->term->create( array(
    275             'taxonomy' => $this->taxonomy,
    276         ) );
    277         $t2 = $this->factory->term->create( array(
     272        $p = self::$factory->post->create();
     273
     274        $t1 = self::$factory->term->create( array(
     275            'taxonomy' => $this->taxonomy,
     276        ) );
     277        $t2 = self::$factory->term->create( array(
    278278            'taxonomy' => 'wptests_tax_3',
    279279        ) );
    280         $t3 = $this->factory->term->create( array(
     280        $t3 = self::$factory->term->create( array(
    281281            'taxonomy' => 'wptests_tax_2',
    282282        ) );
     
    298298     */
    299299    public function test_orderby_tt_id() {
    300         $p = $this->factory->post->create();
    301 
    302         $t1 = $this->factory->term->create( array(
    303             'taxonomy' => $this->taxonomy,
    304         ) );
    305         $t2 = $this->factory->term->create( array(
    306             'taxonomy' => $this->taxonomy,
    307         ) );
    308         $t3 = $this->factory->term->create( array(
     300        $p = self::$factory->post->create();
     301
     302        $t1 = self::$factory->term->create( array(
     303            'taxonomy' => $this->taxonomy,
     304        ) );
     305        $t2 = self::$factory->term->create( array(
     306            'taxonomy' => $this->taxonomy,
     307        ) );
     308        $t3 = self::$factory->term->create( array(
    309309            'taxonomy' => $this->taxonomy,
    310310        ) );
     
    331331
    332332    public function test_order_desc() {
    333         $p = $this->factory->post->create();
    334 
    335         $t1 = $this->factory->term->create( array(
     333        $p = self::$factory->post->create();
     334
     335        $t1 = self::$factory->term->create( array(
    336336            'taxonomy' => $this->taxonomy,
    337337            'name' => 'AAA',
    338338        ) );
    339         $t2 = $this->factory->term->create( array(
     339        $t2 = self::$factory->term->create( array(
    340340            'taxonomy' => $this->taxonomy,
    341341            'name' => 'ZZZ',
    342342        ) );
    343         $t3 = $this->factory->term->create( array(
     343        $t3 = self::$factory->term->create( array(
    344344            'taxonomy' => $this->taxonomy,
    345345            'name' => 'JJJ',
     
    361361     */
    362362    public function test_parent() {
    363         $t1 = $this->factory->term->create( array(
    364             'taxonomy' => $this->taxonomy,
    365         ) );
    366         $t2 = $this->factory->term->create( array(
    367             'taxonomy' => $this->taxonomy,
    368         ) );
    369         $t3 = $this->factory->term->create( array(
     363        $t1 = self::$factory->term->create( array(
     364            'taxonomy' => $this->taxonomy,
     365        ) );
     366        $t2 = self::$factory->term->create( array(
     367            'taxonomy' => $this->taxonomy,
     368        ) );
     369        $t3 = self::$factory->term->create( array(
    370370            'taxonomy' => $this->taxonomy,
    371371            'parent' => $t1,
    372372        ) );
    373         $t4 = $this->factory->term->create( array(
     373        $t4 = self::$factory->term->create( array(
    374374            'taxonomy' => $this->taxonomy,
    375375            'parent' => $t2,
    376376        ) );
    377377
    378         $p = $this->factory->post->create();
     378        $p = self::$factory->post->create();
    379379
    380380        wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
     
    392392     */
    393393    public function test_parent_0() {
    394         $t1 = $this->factory->term->create( array(
    395             'taxonomy' => $this->taxonomy,
    396         ) );
    397         $t2 = $this->factory->term->create( array(
    398             'taxonomy' => $this->taxonomy,
    399         ) );
    400         $t3 = $this->factory->term->create( array(
     394        $t1 = self::$factory->term->create( array(
     395            'taxonomy' => $this->taxonomy,
     396        ) );
     397        $t2 = self::$factory->term->create( array(
     398            'taxonomy' => $this->taxonomy,
     399        ) );
     400        $t3 = self::$factory->term->create( array(
    401401            'taxonomy' => $this->taxonomy,
    402402            'parent' => $t1,
    403403        ) );
    404         $t4 = $this->factory->term->create( array(
     404        $t4 = self::$factory->term->create( array(
    405405            'taxonomy' => $this->taxonomy,
    406406            'parent' => $t2,
    407407        ) );
    408408
    409         $p = $this->factory->post->create();
     409        $p = self::$factory->post->create();
    410410
    411411        wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
     
    426426
    427427        register_taxonomy( 'wptests_tax', 'post' );
    428         $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     428        $terms = self::$factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
    429429        add_term_meta( $terms[0], 'foo', 'bar' );
    430430        add_term_meta( $terms[1], 'foo', 'bar' );
    431431        add_term_meta( $terms[2], 'foo', 'bar' );
    432432
    433         $p = $this->factory->post->create();
     433        $p = self::$factory->post->create();
    434434        wp_set_object_terms( $p, $terms, 'wptests_tax' );
    435435
     
    452452
    453453        register_taxonomy( 'wptests_tax', 'post' );
    454         $terms = $this->factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     454        $terms = self::$factory->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
    455455        add_term_meta( $terms[0], 'foo', 'bar' );
    456456        add_term_meta( $terms[1], 'foo', 'bar' );
    457457        add_term_meta( $terms[2], 'foo', 'bar' );
    458458
    459         $p = $this->factory->post->create();
     459        $p = self::$factory->post->create();
    460460        wp_set_object_terms( $p, $terms, 'wptests_tax' );
    461461
     
    478478    public function test_meta_query() {
    479479        register_taxonomy( 'wptests_tax', 'post' );
    480         $terms = $this->factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
     480        $terms = self::$factory->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
    481481        add_term_meta( $terms[0], 'foo', 'bar' );
    482482        add_term_meta( $terms[1], 'foo', 'bar' );
     
    484484        add_term_meta( $terms[3], 'foob', 'ar' );
    485485
    486         $p = $this->factory->post->create();
     486        $p = self::$factory->post->create();
    487487        wp_set_object_terms( $p, $terms, 'wptests_tax' );
    488488
     
    504504    public function test_should_return_wp_term_objects_for_fields_all() {
    505505        register_taxonomy( 'wptests_tax', 'post' );
    506         $p = $this->factory->post->create();
    507         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     506        $p = self::$factory->post->create();
     507        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    508508        wp_set_object_terms( $p, $t, 'wptests_tax' );
    509509
     
    523523    public function test_should_return_wp_term_objects_for_fields_all_with_object_id() {
    524524        register_taxonomy( 'wptests_tax', 'post' );
    525         $p = $this->factory->post->create();
    526         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     525        $p = self::$factory->post->create();
     526        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    527527        wp_set_object_terms( $p, $t, 'wptests_tax' );
    528528
     
    544544
    545545        register_taxonomy( 'wptests_tax', 'post' );
    546         $p = $this->factory->post->create();
    547         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     546        $p = self::$factory->post->create();
     547        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    548548        wp_set_object_terms( $p, $t, 'wptests_tax' );
    549549
     
    562562    public function test_object_id_should_not_be_cached_with_term_object() {
    563563        register_taxonomy( 'wptests_tax', 'post' );
    564         $p = $this->factory->post->create();
    565         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     564        $p = self::$factory->post->create();
     565        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    566566        wp_set_object_terms( $p, $t, 'wptests_tax' );
    567567
     
    586586        register_taxonomy( 'wptests_tax1', 'post' );
    587587        register_taxonomy( 'wptests_tax2', 'post' );
    588         $p = $this->factory->post->create();
    589         $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
    590         $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     588        $p = self::$factory->post->create();
     589        $t1 = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     590        $t2 = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
    591591        wp_set_object_terms( $p, $t1, 'wptests_tax1' );
    592592        wp_set_object_terms( $p, $t2, 'wptests_tax2' );
     
    612612    public function test_object_id_should_be_set_on_objects_that_share_terms() {
    613613        register_taxonomy( 'wptests_tax', 'post' );
    614         $posts = $this->factory->post->create_many( 2 );
    615         $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     614        $posts = self::$factory->post->create_many( 2 );
     615        $t = self::$factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    616616        wp_set_object_terms( $posts[0], $t, 'wptests_tax' );
    617617        wp_set_object_terms( $posts[1], $t, 'wptests_tax' );
Note: See TracChangeset for help on using the changeset viewer.