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

@ -19,6 +19,53 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
*/
protected $factory;
protected static $static_factory;
public static function get_called_class() {
if ( function_exists( 'get_called_class' ) ) {
return get_called_class();
}
// PHP 5.2 only
$backtrace = debug_backtrace();
// [0] WP_UnitTestCase::get_called_class()
// [1] WP_UnitTestCase::setUpBeforeClass()
if ( 'call_user_func' === $backtrace[2]['function'] ) {
return $backtrace[2]['args'][0][0];
}
return $backtrace[2]['class'];
}
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$c = self::get_called_class();
if ( ! method_exists( $c, 'wpSetUpBeforeClass' ) ) {
return;
}
if ( ! self::$static_factory ) {
self::$static_factory = new WP_UnitTest_Factory();
}
call_user_func( array( $c, 'wpSetUpBeforeClass' ), self::$static_factory );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
$c = self::get_called_class();
if ( ! method_exists( $c, 'wpTearDownAfterClass' ) ) {
return;
}
call_user_func( array( $c, 'wpTearDownAfterClass' ) );
self::commit_transaction();
}
function setUp() {
set_time_limit(0);

View File

@ -16,9 +16,7 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
$this->table = _get_list_table( 'WP_Posts_List_Table' );
}
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
// note that our top/children/grandchildren arrays are 1-indexed
// create top level pages
@ -64,16 +62,12 @@ class Tests_Admin_includesListTable extends WP_UnitTestCase {
}
}
}
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( self::$post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
/**

View File

@ -7,19 +7,8 @@
*/
class Tests_AdminBar extends WP_UnitTestCase {
static function setUpBeforeClass() {
WP_UnitTestCase::setUpBeforeClass();
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php';
}
function setUp() {
parent::setUp();
$this->current_user = get_current_user_id();
}
function tearDown() {
wp_set_current_user( $this->current_user );
parent::tearDown();
public static function setUpBeforeClass() {
require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
}
/**

View File

@ -15,11 +15,7 @@ class Tests_Auth extends WP_UnitTestCase {
*/
protected $nonce_failure_hook = 'wp_verify_nonce_failed';
static function setUpBeforeClass() {
parent::setUpBeforeClass();
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$_user = $factory->user->create_and_get( array(
'user_login' => 'password-tests'
) );
@ -28,20 +24,14 @@ class Tests_Auth extends WP_UnitTestCase {
require_once( ABSPATH . WPINC . '/class-phpass.php' );
self::$wp_hasher = new PasswordHash( 8, true );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function wpTearDownAfterClass() {
if ( is_multisite() ) {
wpmu_delete_user( self::$user_id );
} else {
wp_delete_user( self::$user_id );
}
self::commit_transaction();
}
function setUp() {

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() {

View File

@ -1,7 +1,5 @@
<?php
require_once dirname( dirname( __FILE__ ) ) . '/db.php';
/**
* Test WPDB methods
*
@ -17,6 +15,8 @@ class Tests_DB_Charset extends WP_UnitTestCase {
protected static $_wpdb;
public static function setUpBeforeClass() {
require_once( dirname( dirname( __FILE__ ) ) . '/db.php' );
self::$_wpdb = new wpdb_exposed_methods_for_testing();
}

View File

@ -12,18 +12,14 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
static $user;
static $posts;
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$user = $factory->user->create();
self::$posts = $factory->post->create_many( 5, array(
'post_author' => self::$user,
) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
if ( is_multisite() ) {
wpmu_delete_user( self::$user );
} else {
@ -33,8 +29,6 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
foreach ( self::$posts as $post ) {
wp_delete_post( $post, true );
}
self::commit_transaction();
}
public function setUp() {

View File

@ -18,19 +18,14 @@ class Tests_Get_Archives extends WP_UnitTestCase {
$this->year_url = get_year_link( date( 'Y' ) );
}
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$post_ids = $factory->post->create_many( 8, array( 'post_type' => 'post', 'post_author' => '1' ) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( self::$post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
function test_wp_get_archives_default() {
@ -52,12 +47,18 @@ class Tests_Get_Archives extends WP_UnitTestCase {
$link4 = get_permalink( $ids[3] );
$link5 = get_permalink( $ids[4] );
$title1 = get_post( $ids[0] )->post_title;
$title2 = get_post( $ids[1] )->post_title;
$title3 = get_post( $ids[2] )->post_title;
$title4 = get_post( $ids[3] )->post_title;
$title5 = get_post( $ids[4] )->post_title;
$expected['limit'] = <<<EOF
<li><a href='$link1'>Post title 8</a></li>
<li><a href='$link2'>Post title 7</a></li>
<li><a href='$link3'>Post title 6</a></li>
<li><a href='$link4'>Post title 5</a></li>
<li><a href='$link5'>Post title 4</a></li>
<li><a href='$link1'>$title1</a></li>
<li><a href='$link2'>$title2</a></li>
<li><a href='$link3'>$title3</a></li>
<li><a href='$link4'>$title4</a></li>
<li><a href='$link5'>$title5</a></li>
EOF;
$this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) );
}

View File

@ -7,23 +7,13 @@
class Tests_Media extends WP_UnitTestCase {
protected static $large_id;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
$filename = DIR_TESTDATA . '/images/test-image-large.png';
self::$large_id = $factory->attachment->create_upload_object( $filename );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function wpTearDownAfterClass() {
wp_delete_attachment( self::$large_id );
self::commit_transaction();
}
function setUp() {

View File

@ -9,11 +9,7 @@ class Tests_Post extends WP_UnitTestCase {
protected static $editor_id;
protected static $grammarian_id;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
add_role( 'grammarian', 'Grammarian', array(
@ -24,13 +20,9 @@ class Tests_Post extends WP_UnitTestCase {
) );
self::$grammarian_id = $factory->user->create( array( 'role' => 'grammarian' ) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function wpTearDownAfterClass() {
$ids = array( self::$editor_id, self::$grammarian_id );
foreach ( $ids as $id ) {
if ( is_multisite() ) {
@ -39,8 +31,6 @@ class Tests_Post extends WP_UnitTestCase {
wp_delete_user( $id );
}
}
self::commit_transaction();
}
function setUp() {

View File

@ -12,7 +12,7 @@ class Tests_Query_Date extends WP_UnitTestCase {
static $post_ids = array();
public static function setUpBeforeClass() {
public static function wpSetUpBeforeClass( $factory ) {
// Be careful modifying this. Tests are coded to expect this exact sample data.
$post_dates = array(
'1972-05-24 14:53:45',
@ -40,21 +40,15 @@ class Tests_Query_Date extends WP_UnitTestCase {
'2025-05-20 10:13:01',
);
$factory = new WP_UnitTest_Factory;
foreach ( $post_dates as $post_date ) {
self::$post_ids[] = $factory->post->create( array( 'post_date' => $post_date ) );
}
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( self::$post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
public function setUp() {

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() {

View File

@ -22,9 +22,7 @@ class Tests_Query_Results extends WP_UnitTestCase {
static $child_three;
static $child_four;
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory;
public static function wpSetUpBeforeClass( $factory ) {
self::$cat_ids[] = $cat_a = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
self::$cat_ids[] = $cat_b = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
self::$cat_ids[] = $cat_c = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) );
@ -67,11 +65,9 @@ class Tests_Query_Results extends WP_UnitTestCase {
self::$post_ids[] = self::$child_two = $factory->post->create( array( 'post_title' => 'child-two', 'post_parent' => self::$parent_one, 'post_date' => '2007-01-01 00:00:02' ) );
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' ) );
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' ) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( self::$cat_ids as $cat_id ) {
wp_delete_term( $cat_id, 'category' );
}
@ -83,8 +79,6 @@ class Tests_Query_Results extends WP_UnitTestCase {
foreach ( self::$post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
function setUp() {

View File

@ -8,14 +8,12 @@
class Tests_Query_Stickies extends WP_UnitTestCase {
static $posts = array();
public static function setUpBeforeClass() {
$f = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
// Set post times to get a reliable order.
$now = time();
for ( $i = 0; $i <= 22; $i++ ) {
$post_date = date( 'Y-m-d H:i:s', $now - ( 10 * $i ) );
self::$posts[ $i ] = $f->post->create( array(
self::$posts[ $i ] = $factory->post->create( array(
'post_date' => $post_date,
) );
}
@ -23,16 +21,12 @@ class Tests_Query_Stickies extends WP_UnitTestCase {
stick_post( self::$posts[2] );
stick_post( self::$posts[14] );
stick_post( self::$posts[8] );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( self::$posts as $p ) {
wp_delete_post( $p, true );
}
self::commit_transaction();
}
public function test_stickies_should_be_ignored_when_is_home_is_false() {

View File

@ -7,22 +7,12 @@ class Tests_Term extends WP_UnitTestCase {
protected $taxonomy = 'category';
protected static $post_ids = array();
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$post_ids = $factory->post->create_many( 5 );
self::commit_transaction();
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
public static function wpTearDownAfterClass() {
array_map( 'wp_delete_post', self::$post_ids );
self::commit_transaction();
}
/**

View File

@ -8,9 +8,7 @@ class Tests_User_CountUserPosts extends WP_UnitTestCase {
static $user_id;
static $post_ids = array();
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory();
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->user->create( array(
'role' => 'author',
'user_login' => 'count_user_posts_user',
@ -33,11 +31,9 @@ class Tests_User_CountUserPosts extends WP_UnitTestCase {
'post_author' => 12345,
'post_type' => 'wptests_pt',
) ) );
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
if ( is_multisite() ) {
wpmu_delete_user( self::$user_id );
} else {
@ -47,8 +43,6 @@ class Tests_User_CountUserPosts extends WP_UnitTestCase {
foreach ( self::$post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
public function setUp() {

View File

@ -23,9 +23,7 @@ class Tests_User_ListAuthors extends WP_UnitTestCase {
'style' => 'list',
'html' => true );
*/
public static function setUpBeforeClass() {
$factory = new WP_UnitTest_Factory;
public static function wpSetUpBeforeClass( $factory ) {
self::$users[] = $factory->user->create( array( 'user_login' => 'zack', 'display_name' => 'zack', 'role' => 'author', 'first_name' => 'zack', 'last_name' => 'moon' ) );
self::$users[] = $factory->user->create( array( 'user_login' => 'bob', 'display_name' => 'bob', 'role' => 'author', 'first_name' => 'bob', 'last_name' => 'reno' ) );
self::$users[] = $factory->user->create( array( 'user_login' => 'paul', 'display_name' => 'paul', 'role' => 'author', 'first_name' => 'paul', 'last_name' => 'norris' ) );
@ -40,11 +38,9 @@ class Tests_User_ListAuthors extends WP_UnitTestCase {
self::$user_urls[] = get_author_posts_url( $userid );
}
self::commit_transaction();
}
public static function tearDownAfterClass() {
public static function wpTearDownAfterClass() {
foreach ( array_merge( self::$users, array( self::$fred_id ) ) as $user_id ) {
if ( is_multisite() ) {
wpmu_delete_user( $user_id );
@ -56,8 +52,6 @@ class Tests_User_ListAuthors extends WP_UnitTestCase {
foreach ( self::$posts as $post_id ) {
wp_delete_post( $post_id, true );
}
self::commit_transaction();
}
function test_wp_list_authors_default() {