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

    r34519 r35225  
    44 */
    55class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
     6    protected $terms = array();
     7
    68    /**
    79     * Testing when passed $tags array is empty
     
    2628     */
    2729    function test_empty_tags_list_returned( $expected, $args ) {
    28         $this->factory->term->create_many( 4, array( 'taxonomy' => 'post_tag' ) );
     30        $term_ids = self::$factory->term->create_many( 4, array( 'taxonomy' => 'post_tag' ) );
     31        $this->terms = array();
     32        foreach ( $term_ids as $term_id ) {
     33            $this->terms[] = get_term( $term_id, 'post_tag' );
     34        }
    2935        $tags = $this->retrieve_terms( array( 'number' => 4 ) );
    3036        $this->assertSame( $expected, wp_generate_tag_cloud( $tags, $args ) );
     
    6571    }
    6672
    67 
    68     /**
    69      * Testing the various output for a single link
    70      * in various formats
    71      *
    72      * @dataProvider single_link_data_provider
    73      *
    74      * @param int   $create          How many tags to create.
    75      * @param array $get_terms_args  What args we want to pass to retreve terms.
    76      * @param mixed $expected        Expected output from `wp_generate_tag_cloud()`.
    77      * @param array $args            Options for `wp_generate_tag_cloud()`.
    78      *
    79      */
    80     function test_wp_generate_tag_cloud( $create, $get_terms_args, $expected, $args ) {
    81         $this->factory->term->create_many( $create, array( 'taxonomy' => 'post_tag' ) );
    82         $tags = $this->retrieve_terms( $get_terms_args );
    83 
    84         $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, $args ) );
    85     }
    86 
    87 
    88     function single_link_data_provider() {
    89         return array(
    90             array(
    91                 1,
    92                 array(
    93                     'number' => 1,
    94                     'hide_empty' => false,
    95                 ),
    96                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>",
    97                 array(
    98                     'hide_empty' => false,
    99                 ),
    100             ),
    101 
    102             // Should return an array of links.
    103             array(
    104                 1,
    105                 array(
    106                     'number' => 1,
    107                     'hide_empty' => false,
    108                 ),
    109                 array(
    110                     "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>",
    111                 ),
    112                 array(
    113                     'hide_empty' => false,
    114                     'format'     => 'array',
    115                 ),
    116             ),
    117 
    118             // Should return a string containing a <ul> list of links.
    119             array(
    120                 1,
    121                 array(
    122                     'number' => 1,
    123                     'hide_empty' => false,
    124                 ),
    125                 "<ul class='wp-tag-cloud'>\n\t<li><a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a></li>\n</ul>\n",
    126                 array(
    127                     'hide_empty' => false,
    128                     'format'     => 'list',
    129                 ),
    130             ),
    131 
    132             array(
    133                 4,
    134                 array(
    135                     'number' => 4,
    136                     'hide_empty' => false,
    137                 ),
    138                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>\n".
    139                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-2' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>Term 2</a>\n".
    140                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-3' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>Term 3</a>\n".
    141                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-4' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>Term 4</a>",
    142                 array(
    143                     'hide_empty' => false,
    144                 ),
    145             ),
    146 
    147             array(
    148                 4,
    149                 array(
    150                     'number' => 4,
    151                     'hide_empty' => false,
    152                 ),
    153                 "<ul class='wp-tag-cloud'>\n\t<li>".
    154                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a></li>\n\t<li>".
    155                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-2' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>Term 2</a></li>\n\t<li>".
    156                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-3' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>Term 3</a></li>\n\t<li>".
    157                 "<a href='http://" . WP_TESTS_DOMAIN . "/?tag=term-4' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>Term 4</a>".
    158                 "</li>\n</ul>\n",
    159                 array(
    160                     'hide_empty' => false,
    161                     'format'     => 'list',
    162                 ),
    163             ),
    164         );
     73    function test_hide_empty_false() {
     74        $term_id = self::$factory->tag->create();
     75        $term = get_term( $term_id, 'post_tag' );
     76
     77        $tags = $this->retrieve_terms( array(
     78            'number' => 1,
     79            'hide_empty' => false,
     80        ) );
     81        $expected = "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$term->slug}' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>{$term->name}</a>";
     82        $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array(
     83            'hide_empty' => false,
     84        ) ) );
     85    }
     86
     87    function test_hide_empty_false_format_array() {
     88        $term_id = self::$factory->tag->create();
     89        $term = get_term( $term_id, 'post_tag' );
     90
     91        $tags = $this->retrieve_terms( array(
     92            'number' => 1,
     93            'hide_empty' => false,
     94            'format'     => 'array',
     95        ) );
     96
     97        $expected = "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$term->slug}' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>{$term->name}</a>";
     98        $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array(
     99            'hide_empty' => false,
     100        ) ) );
     101    }
     102
     103    function test_hide_empty_false_format_list() {
     104        $term_id = self::$factory->tag->create();
     105        $term = get_term( $term_id, 'post_tag' );
     106
     107        $tags = $this->retrieve_terms( array(
     108            'number' => 1,
     109            'hide_empty' => false,
     110        ) );
     111
     112        $expected = "<ul class='wp-tag-cloud'>\n\t<li><a href='http://" . WP_TESTS_DOMAIN . "/?tag={$term->slug}' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>{$term->name}</a></li>\n</ul>\n";
     113        $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array(
     114            'hide_empty' => false,
     115            'format'     => 'list',
     116        ) ) );
     117    }
     118
     119    function test_hide_empty_false_multi() {
     120        $term_ids = self::$factory->tag->create_many( 4 );
     121        $terms = array();
     122        foreach ( $term_ids as $term_id ) {
     123            $terms[] = get_term( $term_id, 'post_tag' );
     124        }
     125
     126        $tags = $this->retrieve_terms( array(
     127            'number' => 4,
     128            'order' => 'id',
     129            'hide_empty' => false,
     130        ) );
     131
     132        $expected = "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[0]->slug}' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>{$terms[0]->name}</a>\n".
     133            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[1]->slug}' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>{$terms[1]->name}</a>\n".
     134            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[2]->slug}' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>{$terms[2]->name}</a>\n".
     135            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[3]->slug}' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>{$terms[3]->name}</a>";
     136        $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array(
     137            'hide_empty' => false,
     138        ) ) );
     139    }
     140
     141    function test_hide_empty_false_multi_format_list() {
     142        $term_ids = self::$factory->tag->create_many( 4 );
     143        $terms = array();
     144        foreach ( $term_ids as $term_id ) {
     145            $terms[] = get_term( $term_id, 'post_tag' );
     146        }
     147
     148        $tags = $this->retrieve_terms( array(
     149            'number' => 4,
     150            'orderby' => 'id',
     151            'hide_empty' => false,
     152        ) );
     153
     154        $expected = "<ul class='wp-tag-cloud'>\n\t<li>".
     155            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[0]->slug}' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>{$terms[0]->name}</a></li>\n\t<li>".
     156            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[1]->slug}' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>{$terms[1]->name}</a></li>\n\t<li>".
     157            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[2]->slug}' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>{$terms[2]->name}</a></li>\n\t<li>".
     158            "<a href='http://" . WP_TESTS_DOMAIN . "/?tag={$terms[3]->slug}' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>{$terms[3]->name}</a>".
     159            "</li>\n</ul>\n";
     160
     161        $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, array(
     162            'hide_empty' => false,
     163            'format'     => 'list',
     164        ) ) );
    165165    }
    166166
    167167    public function test_topic_count_text() {
    168168        register_taxonomy( 'wptests_tax', 'post' );
    169         $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
    170         $posts = $this->factory->post->create_many( 2 );
    171 
    172         wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
    173         wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     169        $term_ids = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     170        $this->terms = array();
     171        foreach ( $term_ids as $term_id ) {
     172            $this->terms[] = get_term( $term_id, 'post_tag' );
     173        }
     174        $posts = self::$factory->post->create_many( 2 );
     175
     176        wp_set_post_terms( $posts[0], $term_ids, 'wptests_tax' );
     177        wp_set_post_terms( $posts[1], array( $term_ids[1] ), 'wptests_tax' );
    174178
    175179        $term_objects = $this->retrieve_terms( array(
    176             'include' => $terms,
     180            'include' => $term_ids,
    177181        ), 'wptests_tax' );
    178182
     
    193197    public function test_topic_count_text_callback() {
    194198        register_taxonomy( 'wptests_tax', 'post' );
    195         $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
    196         $posts = $this->factory->post->create_many( 2 );
    197 
    198         wp_set_post_terms( $posts[0], $terms, 'wptests_tax' );
    199         wp_set_post_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     199        $term_ids = self::$factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     200        $this->terms = array();
     201        foreach ( $term_ids as $term_id ) {
     202            $this->terms[] = get_term( $term_id, 'post_tag' );
     203        }
     204        $posts = self::$factory->post->create_many( 2 );
     205
     206        wp_set_post_terms( $posts[0], $term_ids, 'wptests_tax' );
     207        wp_set_post_terms( $posts[1], array( $term_ids[1] ), 'wptests_tax' );
    200208
    201209        $term_objects = $this->retrieve_terms( array(
    202             'include' => $terms,
     210            'include' => $term_ids,
    203211        ), 'wptests_tax' );
    204212
Note: See TracChangeset for help on using the changeset viewer.