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.


git-svn-id: https://develop.svn.wordpress.org/trunk@35186 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-10-15 04:43:37 +00:00
parent e1a09eff54
commit 16d98ebf73
17 changed files with 97 additions and 158 deletions

View File

@@ -7,22 +7,14 @@ class Tests_Comment extends WP_UnitTestCase {
protected static $user_id;
protected static $post_id;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create();
self::$post_id = $factory->post->create( array(
'post_author' => self::$user_id
) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post_id );
if ( is_multisite() ) {
@@ -30,8 +22,6 @@ class Tests_Comment extends WP_UnitTestCase {
} else {
wp_delete_user( self::$user_id );
}
self::commit_transaction();
}
function test_wp_update_comment() {