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

@@ -11,25 +11,21 @@ class Tests_Query_PostStatus extends WP_UnitTestCase {
public static $editor_privatefoo_post;
public static $author_privatefoo_post;
public static function setUpBeforeClass() {
$f = new WP_UnitTest_Factory;
public static function wpSetUpBeforeClass( $factory ) {
self::$editor_user = $factory->user->create( array( 'role' => 'editor' ) );
self::$author_user = $factory->user->create( array( 'role' => 'author' ) );
self::$editor_user = $f->user->create( array( 'role' => 'editor' ) );
self::$author_user = $f->user->create( array( 'role' => 'author' ) );
self::$editor_private_post = $f->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'private' ) );
self::$author_private_post = $f->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'private' ) );
self::$editor_private_post = $factory->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'private' ) );
self::$author_private_post = $factory->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'private' ) );
// Custom status with private=true.
register_post_status( 'privatefoo', array( 'private' => true ) );
self::$editor_privatefoo_post = $f->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'privatefoo' ) );
self::$author_privatefoo_post = $f->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'privatefoo' ) );
self::$editor_privatefoo_post = $factory->post->create( array( 'post_author' => self::$editor_user, 'post_status' => 'privatefoo' ) );
self::$author_privatefoo_post = $factory->post->create( array( 'post_author' => self::$author_user, 'post_status' => 'privatefoo' ) );
_unregister_post_status( 'privatefoo' );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
if ( is_multisite() ) {
wpmu_delete_user( self::$editor_user );
wpmu_delete_user( self::$author_user );
@@ -42,8 +38,6 @@ class Tests_Query_PostStatus extends WP_UnitTestCase {
wp_delete_post( self::$author_private_post, true );
wp_delete_post( self::$editor_privatefoo_post, true );
wp_delete_post( self::$author_privatefoo_post, true );
self::commit_transaction();
}
public function test_any_should_not_include_statuses_where_exclude_from_search_is_true() {