Improved handling of expectedDeprecated and expectedIncorrectUsage annotations in unit tests.

* Do the `expectedDeprecated()` check in `assertPostConditions()` instead of `tearDown()`. Previously, `fail`ing inside of `tearDown()` was causing the rest of the teardown process to be aborted, resulting in inter-test leakage.
* Collect all `expectedDeprecated` and `expectedIncorrectUsage` annotations in an entire method and display them all when `fail`ing, instead of showing only the first one.

Props jdgrimes.
Fixes #31362.

git-svn-id: https://develop.svn.wordpress.org/trunk@31469 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-02-17 16:11:09 +00:00
parent 8bf5fce78f
commit 086e03e8e2
1 changed files with 21 additions and 5 deletions

View File

@ -55,9 +55,19 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
} }
/**
* Detect post-test failure conditions.
*
* We use this method to detect expectedDeprecated and expectedIncorrectUsage annotations.
*
* @since 4.2.0
*/
protected function assertPostConditions() {
$this->expectedDeprecated();
}
function tearDown() { function tearDown() {
global $wpdb, $wp_query, $post; global $wpdb, $wp_query, $post;
$this->expectedDeprecated();
$wpdb->query( 'ROLLBACK' ); $wpdb->query( 'ROLLBACK' );
if ( is_multisite() ) { if ( is_multisite() ) {
while ( ms_is_switched() ) { while ( ms_is_switched() ) {
@ -224,24 +234,30 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
} }
function expectedDeprecated() { function expectedDeprecated() {
$errors = array();
$not_caught_deprecated = array_diff( $this->expected_deprecated, $this->caught_deprecated ); $not_caught_deprecated = array_diff( $this->expected_deprecated, $this->caught_deprecated );
foreach ( $not_caught_deprecated as $not_caught ) { foreach ( $not_caught_deprecated as $not_caught ) {
$this->fail( "Failed to assert that $not_caught triggered a deprecated notice" ); $errors[] = "Failed to assert that $not_caught triggered a deprecated notice";
} }
$unexpected_deprecated = array_diff( $this->caught_deprecated, $this->expected_deprecated ); $unexpected_deprecated = array_diff( $this->caught_deprecated, $this->expected_deprecated );
foreach ( $unexpected_deprecated as $unexpected ) { foreach ( $unexpected_deprecated as $unexpected ) {
$this->fail( "Unexpected deprecated notice for $unexpected" ); $errors[] = "Unexpected deprecated notice for $unexpected";
} }
$not_caught_doing_it_wrong = array_diff( $this->expected_doing_it_wrong, $this->caught_doing_it_wrong ); $not_caught_doing_it_wrong = array_diff( $this->expected_doing_it_wrong, $this->caught_doing_it_wrong );
foreach ( $not_caught_doing_it_wrong as $not_caught ) { foreach ( $not_caught_doing_it_wrong as $not_caught ) {
$this->fail( "Failed to assert that $not_caught triggered an incorrect usage notice" ); $errors[] = "Failed to assert that $not_caught triggered an incorrect usage notice";
} }
$unexpected_doing_it_wrong = array_diff( $this->caught_doing_it_wrong, $this->expected_doing_it_wrong ); $unexpected_doing_it_wrong = array_diff( $this->caught_doing_it_wrong, $this->expected_doing_it_wrong );
foreach ( $unexpected_doing_it_wrong as $unexpected ) { foreach ( $unexpected_doing_it_wrong as $unexpected ) {
$this->fail( "Unexpected incorrect usage notice for $unexpected" ); $errors[] = "Unexpected incorrect usage notice for $unexpected";
}
if ( ! empty( $errors ) ) {
$this->fail( implode( "\n", $errors ) );
} }
} }