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