Introduce `setExpectedDeprecated()` and `setExpectedIncorrectUsage()` methods to `WP_UnitTestCase.

These methods provide a procedural alternative to the `@expectedDeprecated`
and `@expectedIncorrectUsage` test annotations, and parallel PHPUnit's native
`setExpectedException()`.

Props prasoon2211, jdgrimes.
Fixes #28486.

git-svn-id: https://develop.svn.wordpress.org/trunk@31306 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-01-30 16:47:44 +00:00
parent 14c3492ff5
commit 2fb8562a97
2 changed files with 48 additions and 0 deletions

View File

@ -245,6 +245,30 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
}
/**
* Declare an expected `_deprecated_function()` or `_deprecated_argument()` call from within a test.
*
* @since 4.2.0
*
* @param string $deprecated Name of the function, method, class, or argument that is deprecated. Must match
* first parameter of the `_deprecated_function()` or `_deprecated_argument()` call.
*/
public function setExpectedDeprecated( $deprecated ) {
array_push( $this->expected_deprecated, $deprecated );
}
/**
* Declare an expected `_doing_it_wrong()` call from within a test.
*
* @since 4.2.0
*
* @param string $deprecated Name of the function, method, or class that appears in the first argument of the
* source `_doing_it_wrong()` call.
*/
public function setExpectedIncorrectUsage( $doing_it_wrong ) {
array_push( $this->expected_doing_it_wrong, $doing_it_wrong );
}
function deprecated_function_run( $function ) {
if ( ! in_array( $function, $this->caught_deprecated ) )
$this->caught_deprecated[] = $function;

View File

@ -168,4 +168,28 @@ class Tests_TestHelpers extends WP_UnitTestCase {
$this->assertFalse( isset( $stati['foo'] ) );
}
/**
* @ticket 28486
*/
public function test_setExpectedDeprecated() {
$this->setExpectedDeprecated( 'Tests_TestHelpers::mock_deprecated' );
$this->mock_deprecated();
}
/**
* @ticket 28486
*/
public function test_setExpectedIncorrectUsage() {
$this->setExpectedIncorrectUsage( 'Tests_TestHelpers::mock_incorrect_usage' );
$this->mock_incorrect_usage();
}
protected function mock_deprecated() {
_deprecated_function( __METHOD__, '2.5' );
}
protected function mock_incorrect_usage() {
_doing_it_wrong( __METHOD__, __( 'Incorrect usage test' ), '2.5' );
}
}