mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
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:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user