[wp-trac] [WordPress Trac] #60116: PHPUnit tests for _deprecated_function()

WordPress Trac noreply at wordpress.org
Thu Feb 15 05:45:34 UTC 2024


#60116: PHPUnit tests for _deprecated_function()
--------------------------------------+------------------------------
 Reporter:  pbearne                   |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  Awaiting Review
Component:  Build/Test Tools          |     Version:
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+------------------------------

Comment (by xtwijdlc16):

 To create PHPUnit tests for `_deprecated_function()` in WordPress, you'll
 need to set up a testing environment and create test cases that verify the
 behavior of this function. Here's a general outline of how you can
 approach this:

 ### 1. Set Up PHPUnit Testing Environment:
    - Install PHPUnit and set up a testing environment for WordPress.
    - You can use tools like WP-CLI or the WordPress core testing framework
 to scaffold your test suite.

 ### 2. Define Test Cases:
    - Create test cases to cover different scenarios related to
 `_deprecated_function()`.

 ### 3. Example Test Cases:
    - Test if `_deprecated_function()` triggers a `_doing_it_wrong` notice
 when called.
    - Test if `_deprecated_function()` correctly includes the deprecated
 notice with the function name and alternative in the notice.
    - Test if `_deprecated_function()` does not trigger a notice when the
 function is not deprecated.
    - Test if `_deprecated_function()` throws an exception when called with
 invalid parameters.

 ### 4. Test Implementation:
    - Write test methods that call `_deprecated_function()` with various
 arguments and verify the expected behavior.
    - Use assertions to check if `_doing_it_wrong` notices are triggered,
 and if the notices contain the expected information.

 ### 5. Set Up WordPress Environment:
    - Initialize the WordPress environment in your test suite to ensure
 that `_deprecated_function()` behaves as expected within a WordPress
 context.

 ### 6. Run Tests:
    - Run your PHPUnit tests and ensure that all test cases pass.
    - Use continuous integration tools like Travis CI or GitHub Actions to
 automatically run tests on every push to your repository.

 ### Example Test Case:

 ```php
 class DeprecatedFunctionTest extends WP_UnitTestCase {
     public function testDeprecatedFunction() {
         // Call the deprecated function
         _deprecated_function( 'old_function', '1.0.0', 'new_function' );

         // Check if a _doing_it_wrong notice was triggered
         $this->assertTrue( did_action( '_doing_it_wrong' ) > 0 );

         // Check if the deprecated notice contains the expected
 information
         $notice = $GLOBALS['_deprecated_function_args'];
         $this->assertEquals( 'old_function',
 $notice['deprecated_function'] );
         $this->assertEquals( 'new_function', $notice['replacement'] );
     }
 }
 ```

 ### Conclusion:
 By creating PHPUnit tests for `_deprecated_function()`, you can ensure
 that deprecated functions are properly flagged and developers are notified
 when using them. This helps maintain code quality and assists developers
 in transitioning to newer, supported alternatives.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60116#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list