Creating PHPUnit Test Trait to safely update and restore option values with update_option#3043
Closed
Creating PHPUnit Test Trait to safely update and restore option values with update_option#3043
Conversation
Contributor
|
@sol-loup has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Contributor
|
@sol-loup has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces a new reusable mechanism,
SafelyUpdateOptionsTestTrait, designed to improve the reliability and isolation of PHPUnit tests that need to modify WordPress options.Motivation & Context:
Unit tests often require temporarily changing WordPress options (
wp_optionstable entries) to simulate specific environments or configurations. Modifying these global options directly within tests can lead to several problems:Solution:
The
SafelyUpdateOptionsTestTraitaddresses these issues by providing helper methods (set_option_safely_only_for_this_testandremove_option_safely_only_for_this_test) that automatically:@beforehook (setup_options_safely_trait) to reset the tracking for each test.@afterhook (tear_down_options_safely_trait) to automatically restore all modified options back to their recorded original state after each test method finishes executing.Changes:
tests/Unit/SafelyUpdateOptionsTestTrait.php- Contains the core logic for safe option management.tests/Unit/SafelyUpdateOptionsTestTraitTest.php- Provides comprehensive unit tests specifically for theSafelyUpdateOptionsTestTraititself, ensuring it records and restores options correctly under various scenarios (setting new/existing, deleting new/existing, multiple changes).tests/Unit/WCFacebookCommerceIntegrationTest.php(and potentially other test files where safe option handling is beneficial, though this specific PR might focus on its initial introduction and testing). [Note: Update this section if you've added theusestatement to other specific files in this PR]This approach ensures that tests using this trait leave the WordPress options environment exactly as they found it, improving test stability and reducing boilerplate.
Type of change
Checklist
Changelog entry
Dev - Add SafelyUpdateOptionsTestTrait for reliable WordPress option management in unit tests.
Test Plan
use SafelyUpdateOptionsTestTrait(e.g.,WCFacebookCommerceIntegrationTest).Screenshots
N/A - Changes are internal to testing infrastructure.
Before
N/A
After
N/A