Unit test trait to allow options to be globally set in isolation between tests#3057
Closed
Unit test trait to allow options to be globally set in isolation between tests#3057
Conversation
Contributor
|
@sol-loup has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Contributor
mradmeta
pushed a commit
that referenced
this pull request
Apr 14, 2025
…tion between tests (#3057) Summary: ## Description Introduced a new abstract test class, `AbstractWPUnitTestWithOptionIsolation`, designed to provide isolation for unit tests that interact with WordPress options (`get_option`, `update_option`). **Motivation & Context:** Previously, unit tests modifying WordPress options could potentially interfere with each other or leave persistent changes in the test database, leading to unreliable test results and side effects. This change aims to improve test reliability and independence by ensuring option modifications within one test do not affect others. **Implementation:** The `AbstractWPUnitTestWithOptionIsolation` class extends `AbstractWPUnitTestWithSafeFiltering` and utilizes WordPress filters (`pre_option`, `pre_update_option`) to intercept calls to `get_option` and `update_option`. Instead of interacting with the database, option values are stored and retrieved from a local array (`$mocked_options`) within the test class instance. The `setUp()` method initializes this mechanism for each test, and `tearDown()` ensures the local store is cleared and filters are removed (via the parent class). **Changes:** * Created `tests/Unit/AbstractWPUnitTestWithOptionIsolation.php`. * Refactored the following test classes to extend `AbstractWPUnitTestWithOptionIsolation` instead of their previous base class: * `tests/Unit/WCFacebookCommerceIntegrationTest.php` * `tests/Unit/Handlers/MetaExtensionTest.php` * `tests/Unit/Api/REST/RestAPITest.php` * `tests/Unit/Feed/AbstractFeedTest.php` (Note: Affects tests extending this) * `tests/Unit/Feed/FeedUploadUtilsTest.php` * `tests/Unit/FacebookCommercePixelEventTest.php` * `tests/Unit/ExternalVersionUpdate/UpdateTest.php` * `tests/Unit/Admin/Settings_Screens/ConnectionTest.php` * `tests/Unit/Admin/Settings_Screens/ShopsTest.php` * Corrected visibility (`protected` to `public`) for `setUp` methods in child classes where necessary to match the parent. * Added a new test `test_options_are_reset_after_previous_test` to `RestAPITest.php` to explicitly verify the isolation mechanism. **Dependencies:** Requires the existing `AbstractWPUnitTestWithSafeFiltering` class for filter management. ### Type of change - [x] New feature (non-breaking change which adds functionality) - [x] Bug fix (non-breaking change which fixes an issue - potential test interference) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas (Docblocks added to new class). - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I followed general Pull Request best practices. - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes (Added verification test, confirmed existing tests pass). - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality (Tested the test suite with the changes). - [x] I have updated or requested update to plugin documentations (if necessary) (Included README section below). ## Changelog entry Added test isolation for WordPress options (`get_option`/`update_option`) to improve unit test reliability. Pull Request resolved: #3057 Test Plan: 1. Run individual modified test files using `vendor/bin/phpunit path/to/test/file.php`. Verify they pass. 2. Run the full unit test suite for the relevant directory: `vendor/bin/phpunit tests/Unit`. 3. Observe that tests in the modified files pass. 4. Specifically check that `tests/Unit/Api/REST/RestAPITest.php::test_options_are_reset_after_previous_test` passes, confirming option state is reset between tests within that class. 5. Confirm that no new failures were introduced in other test files due to these changes. DONE ## Screenshots N/A **!---- (auto-generated) DO NOT EDIT OR PUT ANYTHING AFTER THIS LINE ----!** MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: www / Diff Version V1 https://internalfb.com/intern/testinfra/testrun/1125900318791272 MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: bloks / Diff Version V1 https://internalfb.com/intern/testinfra/testrun/16044073750707943 Reviewed By: carterbuce Differential Revision: D72984764 Pulled By: sol-loup fbshipit-source-id: eb66b94f4ea872e7e4b6335ace0535c6952610bd
rubycalling
pushed a commit
that referenced
this pull request
Apr 16, 2025
…tion between tests (#3057) Summary: ## Description Introduced a new abstract test class, `AbstractWPUnitTestWithOptionIsolation`, designed to provide isolation for unit tests that interact with WordPress options (`get_option`, `update_option`). **Motivation & Context:** Previously, unit tests modifying WordPress options could potentially interfere with each other or leave persistent changes in the test database, leading to unreliable test results and side effects. This change aims to improve test reliability and independence by ensuring option modifications within one test do not affect others. **Implementation:** The `AbstractWPUnitTestWithOptionIsolation` class extends `AbstractWPUnitTestWithSafeFiltering` and utilizes WordPress filters (`pre_option`, `pre_update_option`) to intercept calls to `get_option` and `update_option`. Instead of interacting with the database, option values are stored and retrieved from a local array (`$mocked_options`) within the test class instance. The `setUp()` method initializes this mechanism for each test, and `tearDown()` ensures the local store is cleared and filters are removed (via the parent class). **Changes:** * Created `tests/Unit/AbstractWPUnitTestWithOptionIsolation.php`. * Refactored the following test classes to extend `AbstractWPUnitTestWithOptionIsolation` instead of their previous base class: * `tests/Unit/WCFacebookCommerceIntegrationTest.php` * `tests/Unit/Handlers/MetaExtensionTest.php` * `tests/Unit/Api/REST/RestAPITest.php` * `tests/Unit/Feed/AbstractFeedTest.php` (Note: Affects tests extending this) * `tests/Unit/Feed/FeedUploadUtilsTest.php` * `tests/Unit/FacebookCommercePixelEventTest.php` * `tests/Unit/ExternalVersionUpdate/UpdateTest.php` * `tests/Unit/Admin/Settings_Screens/ConnectionTest.php` * `tests/Unit/Admin/Settings_Screens/ShopsTest.php` * Corrected visibility (`protected` to `public`) for `setUp` methods in child classes where necessary to match the parent. * Added a new test `test_options_are_reset_after_previous_test` to `RestAPITest.php` to explicitly verify the isolation mechanism. **Dependencies:** Requires the existing `AbstractWPUnitTestWithSafeFiltering` class for filter management. ### Type of change - [x] New feature (non-breaking change which adds functionality) - [x] Bug fix (non-breaking change which fixes an issue - potential test interference) ## Checklist - [x] I have commented my code, particularly in hard-to-understand areas (Docblocks added to new class). - [x] I have confirmed that my changes do not introduce any new PHPCS warnings or errors. - [x] I followed general Pull Request best practices. - [x] I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes (Added verification test, confirmed existing tests pass). - [x] I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality (Tested the test suite with the changes). - [x] I have updated or requested update to plugin documentations (if necessary) (Included README section below). ## Changelog entry Added test isolation for WordPress options (`get_option`/`update_option`) to improve unit test reliability. Pull Request resolved: #3057 Test Plan: 1. Run individual modified test files using `vendor/bin/phpunit path/to/test/file.php`. Verify they pass. 2. Run the full unit test suite for the relevant directory: `vendor/bin/phpunit tests/Unit`. 3. Observe that tests in the modified files pass. 4. Specifically check that `tests/Unit/Api/REST/RestAPITest.php::test_options_are_reset_after_previous_test` passes, confirming option state is reset between tests within that class. 5. Confirm that no new failures were introduced in other test files due to these changes. DONE ## Screenshots N/A **!---- (auto-generated) DO NOT EDIT OR PUT ANYTHING AFTER THIS LINE ----!** MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: www / Diff Version V1 https://internalfb.com/intern/testinfra/testrun/1125900318791272 MFTRunTestsScript Run / Test Suite: sa_checkout / Test Collection: bloks / Diff Version V1 https://internalfb.com/intern/testinfra/testrun/16044073750707943 Reviewed By: carterbuce Differential Revision: D72984764 Pulled By: sol-loup fbshipit-source-id: eb66b94f4ea872e7e4b6335ace0535c6952610bd
This was referenced Apr 18, 2025
This was referenced May 2, 2025
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
Introduced a new abstract test class,
AbstractWPUnitTestWithOptionIsolation, designed to provide isolation for unit tests that interact with WordPress options (get_option,update_option).Motivation & Context:
Previously, unit tests modifying WordPress options could potentially interfere with each other or leave persistent changes in the test database, leading to unreliable test results and side effects. This change aims to improve test reliability and independence by ensuring option modifications within one test do not affect others.
Implementation:
The
AbstractWPUnitTestWithOptionIsolationclass extendsAbstractWPUnitTestWithSafeFilteringand utilizes WordPress filters (pre_option,pre_update_option) to intercept calls toget_optionandupdate_option. Instead of interacting with the database, option values are stored and retrieved from a local array ($mocked_options) within the test class instance. ThesetUp()method initializes this mechanism for each test, andtearDown()ensures the local store is cleared and filters are removed (via the parent class).Changes:
tests/Unit/AbstractWPUnitTestWithOptionIsolation.php.AbstractWPUnitTestWithOptionIsolationinstead of their previous base class:tests/Unit/WCFacebookCommerceIntegrationTest.phptests/Unit/Handlers/MetaExtensionTest.phptests/Unit/Api/REST/RestAPITest.phptests/Unit/Feed/AbstractFeedTest.php(Note: Affects tests extending this)tests/Unit/Feed/FeedUploadUtilsTest.phptests/Unit/FacebookCommercePixelEventTest.phptests/Unit/ExternalVersionUpdate/UpdateTest.phptests/Unit/Admin/Settings_Screens/ConnectionTest.phptests/Unit/Admin/Settings_Screens/ShopsTest.phpprotectedtopublic) forsetUpmethods in child classes where necessary to match the parent.test_options_are_reset_after_previous_testtoRestAPITest.phpto explicitly verify the isolation mechanism.Dependencies:
Requires the existing
AbstractWPUnitTestWithSafeFilteringclass for filter management.Type of change
Checklist
Changelog entry
Added test isolation for WordPress options (
get_option/update_option) to improve unit test reliability.Test Plan
vendor/bin/phpunit path/to/test/file.php. Verify they pass.vendor/bin/phpunit tests/Unit.tests/Unit/Api/REST/RestAPITest.php::test_options_are_reset_after_previous_testpasses, confirming option state is reset between tests within that class.Screenshots
N/A