WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/15/2015 04:43:37 AM (5 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.