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
+2 -8
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() {