Make WordPress Core


Ignore:
Timestamp:
10/15/2015 04:43:37 AM (9 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/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
Note: See TracChangeset for help on using the changeset viewer.