From 16d98ebf73b6c3d101cb6660972941ab48821653 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 15 Oct 2015 04:43:37 +0000 Subject: [PATCH] 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 --- tests/phpunit/includes/testcase.php | 47 +++++++++++++++++++ .../phpunit/tests/admin/includesListTable.php | 10 +--- tests/phpunit/tests/adminbar.php | 15 +----- tests/phpunit/tests/auth.php | 14 +----- tests/phpunit/tests/comment.php | 14 +----- tests/phpunit/tests/db/charset.php | 4 +- tests/phpunit/tests/feed/rss2.php | 10 +--- tests/phpunit/tests/functions/getArchives.php | 25 +++++----- tests/phpunit/tests/media.php | 14 +----- tests/phpunit/tests/post.php | 14 +----- tests/phpunit/tests/query/date.php | 10 +--- tests/phpunit/tests/query/postStatus.php | 22 ++++----- tests/phpunit/tests/query/results.php | 10 +--- tests/phpunit/tests/query/stickies.php | 12 ++--- tests/phpunit/tests/term.php | 14 +----- tests/phpunit/tests/user/countUserPosts.php | 10 +--- tests/phpunit/tests/user/listAuthors.php | 10 +--- 17 files changed, 97 insertions(+), 158 deletions(-) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 5576940c33..0d56cc3e7d 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -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); diff --git a/tests/phpunit/tests/admin/includesListTable.php b/tests/phpunit/tests/admin/includesListTable.php index 67d04f0d22..9dac76fdcf 100644 --- a/tests/phpunit/tests/admin/includesListTable.php +++ b/tests/phpunit/tests/admin/includesListTable.php @@ -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(); } /** diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 709deecdf9..299323790b 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -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' ); } /** diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index ab76f1e674..46cac85dc8 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -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() { diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 4befe0bd9b..fc05a8063a 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -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() { diff --git a/tests/phpunit/tests/db/charset.php b/tests/phpunit/tests/db/charset.php index c1692921da..cb605d697c 100644 --- a/tests/phpunit/tests/db/charset.php +++ b/tests/phpunit/tests/db/charset.php @@ -1,7 +1,5 @@ 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() { diff --git a/tests/phpunit/tests/functions/getArchives.php b/tests/phpunit/tests/functions/getArchives.php index c64496b879..ff2879da2c 100644 --- a/tests/phpunit/tests/functions/getArchives.php +++ b/tests/phpunit/tests/functions/getArchives.php @@ -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'] = <<Post title 8 -
  • Post title 7
  • -
  • Post title 6
  • -
  • Post title 5
  • -
  • Post title 4
  • +
  • $title1
  • +
  • $title2
  • +
  • $title3
  • +
  • $title4
  • +
  • $title5
  • EOF; $this->assertEquals( $expected['limit'], trim( wp_get_archives( array( 'echo' => false, 'type' => 'postbypost', 'limit' => 5 ) ) ) ); } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 19b4ccca85..b1e95b1efc 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -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() { diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index ebdb1dc934..1d7e4747d2 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -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() { diff --git a/tests/phpunit/tests/query/date.php b/tests/phpunit/tests/query/date.php index 5e8a73dff2..9af9655bdc 100644 --- a/tests/phpunit/tests/query/date.php +++ b/tests/phpunit/tests/query/date.php @@ -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() { diff --git a/tests/phpunit/tests/query/postStatus.php b/tests/phpunit/tests/query/postStatus.php index bcc7fbad60..d96ea88fae 100644 --- a/tests/phpunit/tests/query/postStatus.php +++ b/tests/phpunit/tests/query/postStatus.php @@ -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() { diff --git a/tests/phpunit/tests/query/results.php b/tests/phpunit/tests/query/results.php index e7a5354001..380b2aabd2 100644 --- a/tests/phpunit/tests/query/results.php +++ b/tests/phpunit/tests/query/results.php @@ -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() { diff --git a/tests/phpunit/tests/query/stickies.php b/tests/phpunit/tests/query/stickies.php index 73ff74623b..9674f88b9d 100644 --- a/tests/phpunit/tests/query/stickies.php +++ b/tests/phpunit/tests/query/stickies.php @@ -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() { diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 33f6651161..5a95037218 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -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(); } /** diff --git a/tests/phpunit/tests/user/countUserPosts.php b/tests/phpunit/tests/user/countUserPosts.php index d969a4305e..9e12743b40 100644 --- a/tests/phpunit/tests/user/countUserPosts.php +++ b/tests/phpunit/tests/user/countUserPosts.php @@ -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() { diff --git a/tests/phpunit/tests/user/listAuthors.php b/tests/phpunit/tests/user/listAuthors.php index 06fa092469..afe484d86e 100644 --- a/tests/phpunit/tests/user/listAuthors.php +++ b/tests/phpunit/tests/user/listAuthors.php @@ -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() {