Add HttpRequestTrait to mock outbound HTTP in integration tests - #32
Merged
Conversation
Provides a reusable way for integration suites to stub `wp_remote_*` via `pre_http_request`, with responses declared in the test fixture under an `http` key. The trait fails closed: a URL with no fixture entry - or a request beyond the end of its list of responses - is blocked with a WP_Error and reported as a test failure on teardown, so a test can never silently reach the network. Supports several URLs at once, repeated requests to the same URL, and an ordered list of responses when a URL must answer differently per call. The trait does not declare `$config` itself: PHP rejects composing a trait property with an inherited one whose default value differs, which would be a fatal error in test cases extending VirtualFilesystemTestCase. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
8 tasks
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.
Closes #11
What
Adds
WPMedia\PHPUnit\Integration\HttpRequestTrait, a reusable way for our integration suites to stubwp_remote_*calls so a test can exercise code that crawls the site without a reachable URL.Responses are declared in the test fixture under an
httpkey, mapping the requested URL to the raw WordPress response array (or aWP_Error):Against the criteria in #11:
VirtualFilesystemTestCaseand shares its$configfixture, covered by a dedicated test. Note the responses themselves come from the fixture array rather than from virtual files; say the word if serving bodies from vfs paths is wanted too.pre_http_requestfilter and an exact URL match. No re-implementation of the HTTP API.$config.Fails closed
A URL with no fixture entry, or a request past the end of its list of responses, is blocked with a
WP_Errorand reported on teardown:This is deliberate: returning
falsewould tell WordPress to perform the real request, so an unmocked URL would silently hit the network and every new test using the trait would inherit that trap with no signal.Multiple requests
$configis not declared by the traitPHP rejects composing a trait property with an inherited one whose default value differs. Since
VirtualFilesystemTestTraitdeclaresprotected $config = [];, a trait-levelprotected $config;is a fatal error at class-composition time for any test case extendingVirtualFilesystemTestCase. The consuming class supplies$configinstead; this is documented in the class docblock and guarded bytestHttpRequestTraitWithVirtualFilesystem.php.Tests
Tests/Integration/testHttpRequestTrait.php(19 tests) andTests/Integration/testHttpRequestTraitWithVirtualFilesystem.php(2 tests), both in theHttpRequestTraitgroup. They drive realwp_remote_get()calls rather than calling the callback directly, so the filter wiring is covered. Included: filter registration/removal, response shape, multiple URLs, repeated requests, ordered lists,WP_Errorpassthrough, blocking and teardown reporting, state reset between tests, and a data provider for missing/empty/non-arrayhttpconfig.🤖 Generated with Claude Code