Skip to content

Add HttpRequestTrait to mock outbound HTTP in integration tests - #32

Merged
remyperona merged 2 commits into
developfrom
enhancement/http-request-trait
Jul 30, 2026
Merged

Add HttpRequestTrait to mock outbound HTTP in integration tests#32
remyperona merged 2 commits into
developfrom
enhancement/http-request-trait

Conversation

@remyperona

@remyperona remyperona commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Closes #11

What

Adds WPMedia\PHPUnit\Integration\HttpRequestTrait, a reusable way for our integration suites to stub wp_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 http key, mapping the requested URL to the raw WordPress response array (or a WP_Error):

'http' => [
	'https://example.org/api' => [
		'body'     => '{"ok":true}',
		'response' => [ 'code' => 200 ],
	],
],

Against the criteria in #11:

  1. Uses our virtual filesystem — composes with VirtualFilesystemTestCase and shares its $config fixture, 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.
  2. Lean — one pre_http_request filter and an exact URL match. No re-implementation of the HTTP API.
  3. Reusable — no product-specific assumptions; the consuming class just provides $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_Error and reported on teardown:

The test performed HTTP request(s) the fixture does not mock:
  - https://example.org/api (no fixture entry)
Add them to the fixture's `http` config.

This is deliberate: returning false would 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

  • Several URLs mocked at once, matched exactly.
  • The same URL requested repeatedly reuses its single response.
  • When a URL must answer differently per call, declare a list; responses are returned in order, and each URL's position is tracked separately.

$config is not declared by the trait

PHP rejects composing a trait property with an inherited one whose default value differs. Since VirtualFilesystemTestTrait declares protected $config = [];, a trait-level protected $config; is a fatal error at class-composition time for any test case extending VirtualFilesystemTestCase. The consuming class supplies $config instead; this is documented in the class docblock and guarded by testHttpRequestTraitWithVirtualFilesystem.php.

Tests

Tests/Integration/testHttpRequestTrait.php (19 tests) and Tests/Integration/testHttpRequestTraitWithVirtualFilesystem.php (2 tests), both in the HttpRequestTrait group. They drive real wp_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_Error passthrough, blocking and teardown reporting, state reset between tests, and a data provider for missing/empty/non-array http config.

🤖 Generated with Claude Code

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>
@remyperona remyperona self-assigned this Jul 30, 2026
@remyperona remyperona linked an issue Jul 30, 2026 that may be closed by this pull request
@remyperona
remyperona merged commit 9142259 into develop Jul 30, 2026
6 checks passed
@remyperona
remyperona deleted the enhancement/http-request-trait branch July 30, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a WP HTTP handler for request requests.

1 participant