The question actually has nothing specific to wordpress, but maybe it is worth answering.
There is simply no code of your own in that sample that can be/is worth testing, therefor there isn’t much to unit test.
Lets look at the details. add_filter
is integration with core code, remove_filter
is integration with core code and header
is integration with PHP core. If you remove all of those lines you are left with zero code.
Or to say it differently, the three functions mentions above are changing the global state and not your internal state, and while you might be able to construct a relevant test, in the end you are going to be testing them much more than testing your own code.
What you need here is an integration test, set a wordpress instance with your code, send an api request and inspect the result.
Related Posts:
- Where can I find documentation for the WP_UnitTestCase factory classes?
- How to set up a user inside unit tests
- WordPress test environment on windows
- Can I simulate a user being logged in from a WP_UnitTestCase?
- Writing a unit test for add_menu_page
- Can’t get the phpunit core test for WordPress to work [closed]
- struggling to set up WP tests
- How can I properly set up dependencies in automated testing?
- How do I mock get_adjacent_post for testing
- Using factory.php outside of trunk for unit testing
- unit testing admin password
- What is the best way to create a factory for unit test objects?
- PHPUnit Plugin Integration Tests: Getting wordpress-tests-lib PHP into IDE (VS Code)
- Extending WP_UnitTestCase without any Tests
- What make possible unit test plugins?
- Error Testing create_empty_blog() via PHPUnit + Unit Tests
- How to do an unit test for the admin dashboard
- How can I link tests to GitHub issues?
- How do I unit test a plugin with forms?
- While running pipeline php unit test failed
- Testing hooks callback
- Unit testing in the WordPress backend (is_admin() is true)
- How do I mock HTTP requests for PHPUnit?
- Access post from post id in URL
- Proper unit testing in WordPress
- WordPress Themes and PHP unit
- Unit tests – Dealing with dependencies
- When unit testing a plugin, does the plugin need to be in the wp-content/plugins directory of the WordPress tests install?
- Writing test cases for a WordPress Plugin that has translations
- WordPress Unit Testing – Cannot Create Tables
- Plugin Unit Test for Table and Option Creation
- How to write testable classes in WordPress
- PHPUnit testing WordPress Plugin
- How to create a test that calls is_front_page in phpunit?
- Using wp_mail during an integration test
- Implement phpunit testing on a plugin
- What areas to Unit test while building a plugin?
- Why Can’t PHPUnit UnitTest My WordPress Website
- Unit testing to simulate the loop
- how to test a theme upgrade
- WP_UnitTestCase missing?
- What is the best way to do unit tests for your plugin in 2018 [closed]
- Is there any way of of making an admin-ajax request without the use of die()?
- Using Travis CI to run unit tests and running phpunit locally [closed]
- Revert WordPress default options after a PHPUnit test has run
- Plugin development with unit tests
- Unit testing wordpress plugins with plugin options
- How to properly test a method that is called by an action hook
- Install and load additional plugins when running unit tests
- Unit Testing a Plugin functionality with cron
- PHPUnit test plugin activation
- Installing WordPress Theme Unit Data
- How to add an image for unit testing?
- Fatal error: Maximum execution time of 60 seconds exceeded in H:\wamp\www\custom\wp-includes\class-wp-http-curl.php
- Why is WP_Mock not used instead of WP_UnitTestCase for writing unit tests by most plugins?
- DataBase connection problem with PHPUnit and WordPress
- WordPress function gives different results outside vs inside PHPUnit test
- how to set context in WordPress for unit testing
- Setting Up PHPUnit tests for WP development on Windows
- Testing custom API endpoint with class dependency
- How to verify meta box is registered in Unit Testing?
- Theme unit test data Foreign fonts come in garbled on my machine
- How exactly *does* unit testing work for themes?
- How to initialize something in unit test before the init hook being called?
- Unit Test in WordPress
- How to cancel WordPress’ action/filter when using OOP with anonymous callback
- Error when setting up phpunit tests with wp-cli scaffold
- How do I phpunit test a post output process?
- Coverage in integration tests
- Underscores Theme Unit Testing – Catching Untitled Posts
- Am I testing this right?
- Unit testing a plugin with a theme dependency
- How to retain table data in wordpress phpunit tests
- Is there a way to mock update_option for unit testing?
- WordPress Automatic Testing Probelm
- Using My Own Classes On Wp Unit Tests
- Why is one phpunit test throwing an error on one class when all other classes are similar without error?
- How to import Theme Unit Test on localhost
- Integration tests test script enqueue/register fails
- PHPUnit via WP-CLI: Warning: Cannot modify header information … bootstrap.php:68
- Set up PHPUnit with WordPress: The WordPress tests are 3 – 4 years old; does it matter?
- no_rest_route in Unit Testing for REST endpoint for CPT registered via show_in_rest
- Mocking WP_CLI static methods in unit tests
- How can I enable IDE integration (autocomplete) for WordPress test suite
- PHPUnit Testing and woocommerce Constant
- Unit Testing action hook
- WordPress and WordPress-test-lib not getting added to tmp folder
- Integration test – remove the mu plugins in the test
- Unable to find PHPUnit code coverage stats
- Unregister Settings Error
- Unit Test ignores stubbed method second time around in a dataProvider
- PHPUnit Ajax Serialization of ‘Closure’ is not allowed
- PHPUnit Testing Installation Issue
- Testing Plugin – Install plugin dependencies in testing enviroment
- Jest Unit tests for High order component containing Block Editor