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