From 2fb8562a979283bcb04b950a11d3162f54af5152 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 30 Jan 2015 16:47:44 +0000 Subject: [PATCH] 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 --- tests/phpunit/includes/testcase.php | 24 ++++++++++++++++++++++++ tests/phpunit/tests/includes/helpers.php | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 782482f196..1150bc0128 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -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; diff --git a/tests/phpunit/tests/includes/helpers.php b/tests/phpunit/tests/includes/helpers.php index 2bcd309dbc..025b9384f6 100755 --- a/tests/phpunit/tests/includes/helpers.php +++ b/tests/phpunit/tests/includes/helpers.php @@ -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' ); + } }