WordPress.org

Make WordPress Core

Changeset 35186


Ignore:
Timestamp:
10/15/2015 04:43:37 AM (4 years ago)
Author:
wonderboymusic
Message:

Unit Tests: implement setUpBeforeClass() and tearDownAfterClass() on WP_UnitTestCase. Use late static binding (plus a gross fallback for PHP 5.2) to check if wpSetUpBeforeClass() or wpTearDownAfterClass() exist on the called class, and then call it and pass a static WP_UnitTest_Factory instance via Dependency Injection, if it exists.

This makes it way easier to add fixtures, and tear them down, without needing to instantiate WP_UnitTest_Factory in every class - removes the need to call commit_transaction() in each individual class.

See #30017, #33968.

Location:
trunk/tests/phpunit
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r35136 r35186  
    1919     */
    2020    protected $factory;
     21
     22    protected static $static_factory;
     23
     24    public static function get_called_class() {
     25        if ( function_exists( 'get_called_class' ) ) {
     26            return get_called_class();
     27        }
     28
     29        // PHP 5.2 only
     30        $backtrace = debug_backtrace();
     31        // [0] WP_UnitTestCase::get_called_class()
     32        // [1] WP_UnitTestCase::setUpBeforeClass()
     33        if ( 'call_user_func' ===  $backtrace[2]['function'] ) {
     34            return $backtrace[2]['args'][0][0];
     35        }
     36        return $backtrace[2]['class'];
     37    }
     38
     39    public static function setUpBeforeClass() {
     40        parent::setUpBeforeClass();
     41
     42        $c = self::get_called_class();
     43        if ( ! method_exists( $c, 'wpSetUpBeforeClass' ) ) {
     44            return;
     45        }
     46
     47        if ( ! self::$static_factory ) {
     48            self::$static_factory = new WP_UnitTest_Factory();
     49        }
     50
     51        call_user_func( array( $c, 'wpSetUpBeforeClass' ), self::$static_factory );
     52
     53        self::commit_transaction();
     54    }
     55
     56    public static function tearDownAfterClass() {
     57        parent::tearDownAfterClass();
     58
     59        $c = self::get_called_class();
     60        if ( ! method_exists( $c, 'wpTearDownAfterClass' ) ) {
     61            return;
     62        }
     63
     64        call_user_func( array( $c, 'wpTearDownAfterClass' ) );
     65
     66        self::commit_transaction();
     67    }
    2168
    2269    function setUp() {
  • trunk/tests/phpunit/tests/admin/includesListTable.php

    r35165 r35186  
    1717    }
    1818
    19     public static function setUpBeforeClass() {
    20         $factory = new WP_UnitTest_Factory();
    21 
     19    public static function wpSetUpBeforeClass( $factory ) {
    2220        // note that our top/children/grandchildren arrays are 1-indexed
    2321
     
    6563            }
    6664        }
    67 
    68         self::commit_transaction();
    69     }
    70 
    71     public static function tearDownAfterClass() {
     65    }
     66
     67    public static function wpTearDownAfterClass() {
    7268        foreach ( self::$post_ids as $post_id ) {
    7369            wp_delete_post( $post_id, true );
    7470        }
    75 
    76         self::commit_transaction();
    7771    }
    7872
  • trunk/tests/phpunit/tests/adminbar.php

    r34122 r35186  
    88class Tests_AdminBar extends WP_UnitTestCase {
    99
    10     static function setUpBeforeClass() {
    11         WP_UnitTestCase::setUpBeforeClass();
    12         require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
    13     }
    14 
    15     function setUp() {
    16         parent::setUp();
    17         $this->current_user = get_current_user_id();
    18     }
    19 
    20     function tearDown() {
    21         wp_set_current_user( $this->current_user );
    22         parent::tearDown();
     10    public static function setUpBeforeClass() {
     11        require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
    2312    }
    2413
  • trunk/tests/phpunit/tests/auth.php

    r35175 r35186  
    1616    protected $nonce_failure_hook = 'wp_verify_nonce_failed';
    1717
    18     static function setUpBeforeClass() {
    19         parent::setUpBeforeClass();
    20 
    21         $factory = new WP_UnitTest_Factory();
    22 
     18    public static function wpSetUpBeforeClass( $factory ) {
    2319        self::$_user = $factory->user->create_and_get( array(
    2420            'user_login' => 'password-tests'
     
    2925        require_once( ABSPATH . WPINC . '/class-phpass.php' );
    3026        self::$wp_hasher = new PasswordHash( 8, true );
    31 
    32         self::commit_transaction();
    33     }
    34 
    35     public static function tearDownAfterClass() {
    36         parent::tearDownAfterClass();
    37 
     27    }
     28
     29    public static function wpTearDownAfterClass() {
    3830        if ( is_multisite() ) {
    3931            wpmu_delete_user( self::$user_id );
     
    4133            wp_delete_user( self::$user_id );
    4234        }
    43 
    44         self::commit_transaction();
    4535    }
    4636
  • trunk/tests/phpunit/tests/comment.php

    r35176 r35186  
    88    protected static $post_id;
    99
    10     public static function setUpBeforeClass() {
    11         parent::setUpBeforeClass();
    12 
    13         $factory = new WP_UnitTest_Factory();
    14 
     10    public static function wpSetUpBeforeClass( $factory ) {
    1511        self::$user_id = $factory->user->create();
    1612        self::$post_id = $factory->post->create( array(
    1713            'post_author' => self::$user_id
    1814        ) );
    19 
    20         self::commit_transaction();
    21     }
    22 
    23     public static function tearDownAfterClass() {
    24         parent::tearDownAfterClass();
    25 
     15    }
     16
     17    public static function wpTearDownAfterClass() {
    2618        wp_delete_post( self::$post_id );
    2719
     
    3123            wp_delete_user( self::$user_id );
    3224        }
    33 
    34         self::commit_transaction();
    3525    }
    3626
  • trunk/tests/phpunit/tests/db/charset.php

    r34655 r35186  
    11<?php
    2 
    3 require_once dirname( dirname( __FILE__ ) ) . '/db.php';
    42
    53/**
     
    1816
    1917    public static function setUpBeforeClass() {
     18        require_once( dirname( dirname( __FILE__ ) ) . '/db.php' );
     19       
    2020        self::$_wpdb = new wpdb_exposed_methods_for_testing();
    2121    }
  • trunk/tests/phpunit/tests/feed/rss2.php

    r35162 r35186  
    1313    static $posts;
    1414
    15     public static function setUpBeforeClass() {
    16         $factory = new WP_UnitTest_Factory();
    17 
     15    public static function wpSetUpBeforeClass( $factory ) {
    1816        self::$user = $factory->user->create();
    1917        self::$posts = $factory->post->create_many( 5, array(
    2018            'post_author' => self::$user,
    2119        ) );
    22 
    23         self::commit_transaction();
    2420    }
    2521
    26     public static function tearDownAfterClass() {
     22    public static function wpTearDownAfterClass() {
    2723        if ( is_multisite() ) {
    2824            wpmu_delete_user( self::$user );
     
    3430            wp_delete_post( $post, true );
    3531        }
    36 
    37         self::commit_transaction();
    3832    }
    3933
  • trunk/tests/phpunit/tests/functions/getArchives.php

    r35163 r35186  
    1919    }
    2020
    21     public static function setUpBeforeClass() {
    22         $factory = new WP_UnitTest_Factory();
     21    public static function wpSetUpBeforeClass( $factory ) {
    2322        self::$post_ids = $factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
    24 
    25         self::commit_transaction();
    2623    }
    2724
    28     public static function tearDownAfterClass() {
     25    public static function wpTearDownAfterClass() {
    2926        foreach ( self::$post_ids as $post_id ) {
    3027            wp_delete_post( $post_id, true );
    3128        }
    32 
    33         self::commit_transaction();
    3429    }
    3530
     
    5348        $link5 = get_permalink( $ids[4] );
    5449
     50        $title1 = get_post( $ids[0] )->post_title;
     51        $title2 = get_post( $ids[1] )->post_title;
     52        $title3 = get_post( $ids[2] )->post_title;
     53        $title4 = get_post( $ids[3] )->post_title;
     54        $title5 = get_post( $ids[4] )->post_title;
     55
    5556        $expected['limit'] = <<<EOF
    56 <li><a href='$link1'>Post title 8</a></li>
    57     <li><a href='$link2'>Post title 7</a></li>
    58     <li><a href='$link3'>Post title 6</a></li>
    59     <li><a href='$link4'>Post title 5</a></li>
    60     <li><a href='$link5'>Post title 4</a></li>
     57<li><a href='$link1'>$title1</a></li>
     58    <li><a href='$link2'>$title2</a></li>
     59    <li><a href='$link3'>$title3</a></li>
     60    <li><a href='$link4'>$title4</a></li>
     61    <li><a href='$link5'>$title5</a></li>
    6162EOF;
    6263        $this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
  • trunk/tests/phpunit/tests/media.php

    r35181 r35186  
    88    protected static $large_id;
    99
    10     public static function setUpBeforeClass() {
    11         parent::setUpBeforeClass();
    12 
    13         $factory = new WP_UnitTest_Factory();
    14 
     10    public static function wpSetUpBeforeClass( $factory ) {
    1511        $filename = DIR_TESTDATA . '/images/test-image-large.png';
    1612        self::$large_id = $factory->attachment->create_upload_object( $filename );
    17 
    18         self::commit_transaction();
    19     }
    20 
    21     public static function tearDownAfterClass() {
    22         parent::tearDownAfterClass();
    23 
     13    }
     14
     15    public static function wpTearDownAfterClass() {
    2416        wp_delete_attachment( self::$large_id );
    25 
    26         self::commit_transaction();
    2717    }
    2818
  • trunk/tests/phpunit/tests/post.php

    r35183 r35186  
    1010    protected static $grammarian_id;
    1111
    12     public static function setUpBeforeClass() {
    13         parent::setUpBeforeClass();
    14 
    15         $factory = new WP_UnitTest_Factory();
    16 
     12    public static function wpSetUpBeforeClass( $factory ) {
    1713        self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
    1814
     
    2521
    2622        self::$grammarian_id = $factory->user->create( array( 'role' => 'grammarian' ) );
    27 
    28         self::commit_transaction();
    29     }
    30 
    31     public static function tearDownAfterClass() {
    32         parent::tearDownAfterClass();
    33 
     23    }
     24
     25    public static function wpTearDownAfterClass() {
    3426        $ids = array( self::$editor_id, self::$grammarian_id );
    3527        foreach ( $ids as $id ) {
     
    4032            }
    4133        }
    42 
    43         self::commit_transaction();
    4434    }
    4535
  • trunk/tests/phpunit/tests/query/date.php

    r30276 r35186  
    1313    static $post_ids = array();
    1414
    15     public static function setUpBeforeClass() {
     15    public static function wpSetUpBeforeClass( $factory ) {
    1616        // Be careful modifying this. Tests are coded to expect this exact sample data.
    1717        $post_dates = array(
     
    4141        );
    4242
    43         $factory = new WP_UnitTest_Factory;
    44 
    4543        foreach ( $post_dates as $post_date ) {
    4644            self::$post_ids[] = $factory->post->create( array( 'post_date' => $post_date ) );
    4745        }
    48 
    49         self::commit_transaction();
    50     }
    51 
    52     public static function tearDownAfterClass() {
     46    }
     47
     48    public static function wpTearDownAfterClass() {
    5349        foreach ( self::$post_ids as $post_id ) {
    5450            wp_delete_post( $post_id, true );
    5551        }
    56 
    57         self::commit_transaction();
    5852    }
    5953
  • trunk/tests/phpunit/tests/query/postStatus.php

    r31321 r35186  
    1212    public static $author_privatefoo_post;
    1313
    14     public static function setUpBeforeClass() {
    15         $f = new WP_UnitTest_Factory;
    16 
    17         self::$editor_user = $f->user->create( array( 'role' => 'editor' ) );
    18         self::$author_user = $f->user->create( array( 'role' => 'author' ) );
    19 
    20         self::$editor_private_post = $f->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'private' ) );
    21         self::$author_private_post = $f->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'private' ) );
     14    public static function wpSetUpBeforeClass( $factory ) {
     15        self::$editor_user = $factory->user->create( array( 'role' => 'editor' ) );
     16        self::$author_user = $factory->user->create( array( 'role' => 'author' ) );
     17
     18        self::$editor_private_post = $factory->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'private' ) );
     19        self::$author_private_post = $factory->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'private' ) );
    2220
    2321        // Custom status with private=true.
    2422        register_post_status( 'privatefoo', array( 'private' => true ) );
    25         self::$editor_privatefoo_post = $f->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'privatefoo' ) );
    26         self::$author_privatefoo_post = $f->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'privatefoo' ) );
     23        self::$editor_privatefoo_post = $factory->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'privatefoo' ) );
     24        self::$author_privatefoo_post = $factory->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'privatefoo' ) );
    2725        _unregister_post_status( 'privatefoo' );
    28 
    29         self::commit_transaction();
    30     }
    31 
    32     public static function tearDownAfterClass() {
     26    }
     27
     28    public static function wpTearDownAfterClass() {
    3329        if ( is_multisite() ) {
    3430            wpmu_delete_user( self::$editor_user );
     
    4339        wp_delete_post( self::$editor_privatefoo_post, true );
    4440        wp_delete_post( self::$author_privatefoo_post, true );
    45 
    46         self::commit_transaction();
    4741    }
    4842
  • trunk/tests/phpunit/tests/query/results.php

    r34802 r35186  
    2323    static $child_four;
    2424
    25     public static function setUpBeforeClass() {
    26         $factory = new WP_UnitTest_Factory;
    27 
     25    public static function wpSetUpBeforeClass( $factory ) {
    2826        self::$cat_ids[] = $cat_a = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
    2927        self::$cat_ids[] = $cat_b = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
     
    6866        self::$post_ids[] = self::$child_three = $factory->post->create( array( 'post_title' => 'child-three', 'post_parent' => self::$parent_two, 'post_date' => '2007-01-01 00:00:03' ) );
    6967        self::$post_ids[] = self::$child_four = $factory->post->create( array( 'post_title' => 'child-four', 'post_parent' => self::$parent_two, 'post_date' => '2007-01-01 00:00:04' ) );
    70 
    71         self::commit_transaction();
    72     }
    73 
    74     public static function tearDownAfterClass() {
     68    }
     69
     70    public static function wpTearDownAfterClass() {
    7571        foreach ( self::$cat_ids as $cat_id ) {
    7672            wp_delete_term( $cat_id, 'category' );
     
    8480            wp_delete_post( $post_id, true );
    8581        }
    86 
    87         self::commit_transaction();
    8882    }
    8983
  • trunk/tests/phpunit/tests/query/stickies.php

    r31439 r35186  
    99    static $posts = array();
    1010
    11     public static function setUpBeforeClass() {
    12         $f = new WP_UnitTest_Factory();
    13 
     11    public static function wpSetUpBeforeClass( $factory ) {
    1412        // Set post times to get a reliable order.
    1513        $now = time();
    1614        for ( $i = 0; $i <= 22; $i++ ) {
    1715            $post_date = date( 'Y-m-d H:i:s', $now - ( 10 * $i ) );
    18             self::$posts[ $i ] = $f->post->create( array(
     16            self::$posts[ $i ] = $factory->post->create( array(
    1917                'post_date' => $post_date,
    2018            ) );
     
    2422        stick_post( self::$posts[14] );
    2523        stick_post( self::$posts[8] );
    26 
    27         self::commit_transaction();
    2824    }
    2925
    30     public static function tearDownAfterClass() {
     26    public static function wpTearDownAfterClass() {
    3127        foreach ( self::$posts as $p ) {
    3228            wp_delete_post( $p, true );
    3329        }
    34 
    35         self::commit_transaction();
    3630    }
    3731
  • trunk/tests/phpunit/tests/term.php

    r35185 r35186  
    88    protected static $post_ids = array();
    99
    10     public static function setUpBeforeClass() {
    11         parent::setUpBeforeClass();
    12 
    13         $factory = new WP_UnitTest_Factory();
    14 
     10    public static function wpSetUpBeforeClass( $factory ) {
    1511        self::$post_ids = $factory->post->create_many( 5 );
    16 
    17         self::commit_transaction();
    18     }
    19 
    20     public static function tearDownAfterClass() {
    21         parent::tearDownAfterClass();
    22 
     12    }
     13
     14    public static function wpTearDownAfterClass() {
    2315        array_map( 'wp_delete_post', self::$post_ids );
    24 
    25         self::commit_transaction();
    2616    }
    2717
  • trunk/tests/phpunit/tests/user/countUserPosts.php

    r32523 r35186  
    99    static $post_ids = array();
    1010
    11     public static function setUpBeforeClass() {
    12         $factory = new WP_UnitTest_Factory();
    13 
     11    public static function wpSetUpBeforeClass( $factory ) {
    1412        self::$user_id = $factory->user->create( array(
    1513            'role' => 'author',
     
    3432            'post_type'   => 'wptests_pt',
    3533        ) ) );
    36 
    37         self::commit_transaction();
    3834    }
    3935
    40     public static function tearDownAfterClass() {
     36    public static function wpTearDownAfterClass() {
    4137        if ( is_multisite() ) {
    4238            wpmu_delete_user( self::$user_id );
     
    4844            wp_delete_post( $post_id, true );
    4945        }
    50 
    51         self::commit_transaction();
    5246    }
    5347
  • trunk/tests/phpunit/tests/user/listAuthors.php

    r31676 r35186  
    2424        'html'          => true );
    2525        */
    26     public static function setUpBeforeClass() {
    27         $factory = new WP_UnitTest_Factory;
    28 
     26    public static function wpSetUpBeforeClass( $factory ) {
    2927        self::$users[] = $factory->user->create( array( 'user_login' => 'zack', 'display_name' => 'zack', 'role' => 'author', 'first_name' => 'zack', 'last_name' => 'moon' ) );
    3028        self::$users[] = $factory->user->create( array( 'user_login' => 'bob', 'display_name' => 'bob', 'role' => 'author', 'first_name' => 'bob', 'last_name' => 'reno' ) );
     
    4139            self::$user_urls[] = get_author_posts_url( $userid );
    4240        }
    43 
    44         self::commit_transaction();
    4541    }
    4642
    47     public static function tearDownAfterClass() {
     43    public static function wpTearDownAfterClass() {
    4844        foreach ( array_merge( self::$users, array( self::$fred_id ) ) as $user_id ) {
    4945            if ( is_multisite() ) {
     
    5753            wp_delete_post( $post_id, true );
    5854        }
    59 
    60         self::commit_transaction();
    6155    }
    6256
Note: See TracChangeset for help on using the changeset viewer.