Make WordPress Core


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

    r35162 r35225  
    1616        global $wpdb;
    1717
    18         $this->factory->post->create_many( 3, array( 'post_type' => 'page' ) );
     18        self::$factory->post->create_many( 3, array( 'post_type' => 'page' ) );
    1919        wp_cache_delete( 'last_changed', 'posts' );
    2020        $this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
     
    100100     */
    101101    function test_get_pages_meta() {
    102         $posts = $this->factory->post->create_many( 3, array( 'post_type' => 'page' ) );
     102        $posts = self::$factory->post->create_many( 3, array( 'post_type' => 'page' ) );
    103103        add_post_meta( $posts[0], 'some-meta-key', '0' );
    104104        add_post_meta( $posts[1], 'some-meta-key', '' );
     
    117117
    118118        foreach ( range( 1, 20 ) as $i )
    119             $page_ids[] = $this->factory->post->create( array( 'post_type' => 'page' ) );
     119            $page_ids[] = self::$factory->post->create( array( 'post_type' => 'page' ) );
    120120
    121121        $inc = array_slice( $page_ids, 0, 10 );
     
    139139     */
    140140    function test_get_pages_parent() {
    141         $page_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    142         $page_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
    143         $page_id3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
    144         $page_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
     141        $page_id1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     142        $page_id2 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
     143        $page_id3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
     144        $page_id4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
    145145
    146146        $pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
     
    167167     */
    168168    function test_wp_dropdown_pages() {
    169         $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
     169        self::$factory->post->create_many( 5, array( 'post_type' => 'page' ) );
    170170
    171171        preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
     
    178178     */
    179179    function test_get_chidren_fields_ids() {
    180         $post_id = $this->factory->post->create();
    181         $child_ids = $this->factory->post->create_many( 5, array( 'post_parent' => $post_id ) );
     180        $post_id = self::$factory->post->create();
     181        $child_ids = self::$factory->post->create_many( 5, array( 'post_parent' => $post_id ) );
    182182
    183183        $post_ids = get_children( array( 'fields' => 'ids', 'post_parent' => $post_id ) );
     
    190190    function test_get_pages_hierarchical_and_no_parent() {
    191191        global $wpdb;
    192         $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    193         $page_2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    194         $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    195         $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     192        $page_1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     193        $page_2 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     194        $page_3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     195        $page_4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
    196196
    197197        $pages = get_pages(); // Defaults: hierarchical = true, parent = -1
     
    219219     */
    220220    public function test_get_pages_hierarchical_empty_child_of() {
    221         $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    222         $page_2 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    223         $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    224         $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     221        $page_1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     222        $page_2 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     223        $page_3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     224        $page_4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    225225
    226226        $pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1
     
    253253     */
    254254    public function test_get_pages_non_hierarchical_empty_child_of() {
    255         $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    256         $page_2 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    257         $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    258         $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     255        $page_1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     256        $page_2 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     257        $page_3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     258        $page_4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    259259
    260260        $pages = get_pages( array( 'hierarchical' => false ) ); // child_of = '', parent = -1
     
    279279     */
    280280    public function test_get_pages_hierarchical_non_empty_child_of() {
    281         $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    282         $page_2 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    283         $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    284         $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
    285         $page_5 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     281        $page_1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     282        $page_2 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     283        $page_3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     284        $page_4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
     285        $page_5 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    286286
    287287        $pages = get_pages( array( 'child_of' => $page_1 ) ); // Defaults: hierarchical = true, parent = -1.
     
    307307     */
    308308    public function test_get_pages_non_hierarchical_non_empty_child_of() {
    309         $page_1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    310         $page_2 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    311         $page_3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    312         $page_4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
    313         $page_5 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     309        $page_1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     310        $page_2 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     311        $page_3 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     312        $page_4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
     313        $page_5 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
    314314
    315315        $pages = get_pages( array( 'hierarchical' => false, 'child_of' => $page_1 ) );
     
    339339        register_post_type( $type, array( 'hierarchical' => true, 'public' => true ) );
    340340
    341         $posts = $this->factory->post->create_many( 2, array( 'post_type' => $type ) );
     341        $posts = self::$factory->post->create_many( 2, array( 'post_type' => $type ) );
    342342        $post_id = reset( $posts );
    343343
     
    361361
    362362    function test_exclude_tree() {
    363         $post_id1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    364         $post_id2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) );
    365         $post_id3 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    366         $post_id4 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) );
     363        $post_id1 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     364        $post_id2 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) );
     365        $post_id3 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     366        $post_id4 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) );
    367367
    368368        $all = get_pages();
     
    385385        $this->assertCount( 0, $exclude5 );
    386386
    387         $post_id5 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    388         $post_id6 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) );
     387        $post_id5 = self::$factory->post->create( array( 'post_type' => 'page' ) );
     388        $post_id6 = self::$factory->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) );
    389389
    390390        $exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
Note: See TracChangeset for help on using the changeset viewer.