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