Skip to content

Defer mock Requests transport instantiation until http_request_options - #432

Merged
swissspidy merged 1 commit into
mainfrom
fix/lazy-mock-requests-transport
Nov 19, 2026
Merged

swissspidy merged 1 commit into
mainfrom
fix/lazy-mock-requests-transport

Conversation

@swissspidy

@swissspidy swissspidy commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Updates src/Context/templates/mock-requests.php so it no longer probes interface_exists( '\WpOrg\Requests\Transport' ) or declares the transport class eagerly at file-require time (during WP-CLI's LoadRequiredCommand bootstrap step).

Why

When WP-CLI defers loading WordPress core's Requests library until Runner::load_wordpress(), requiring mock-requests.php eagerly during bootstrap would otherwise trigger bundled Requests v2 autoloading prematurely—breaking compatibility when WordPress <= 6.1 (Requests v1) is subsequently loaded.

Changes

  • Move the transport class instantiation inside the http_request_options hook callback using anonymous classes implementing \WpOrg\Requests\Transport (v2) or \Requests_Transport (v1).
  • Ensure WP_CLI\RequestsLibrary::register_autoloader() is called inside http_request_options before checking for the transport interface so the active Requests version is always registered first.
  • Remove the now-unnecessary Generic.Files.OneObjectStructurePerFile and Generic.Classes.DuplicateClassName PHPCS exclusions for src/Context/templates/*.

@swissspidy
swissspidy requested a review from a team as a code owner November 19, 2026 11:01
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The mock Requests template now conditionally registers the Requests autoloader and assigns an anonymous v1 or v2 transport through the request-options hook. New subprocess tests cover template loading, autoloader registration, and transport selection.

Changes: Mock Requests transport

Layer / File(s) Summary
Initialize the mock transport
src/Context/templates/mock-requests.php, phpcs.xml.dist
The hook conditionally registers the Requests autoloader and assigns an anonymous transport that implements the available v1 or v2 interface. The PHPCS exclusions for the template directory were removed, and the related comment was updated.
Verify template loading and transport selection
tests/tests/TestMockRequestsTemplate.php
Subprocess tests check that requiring the template does not trigger autoloading, and that invoking the hook registers the autoloader when available and returns a v1 or v2 transport.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: schlessera

Merge Risk: Minimal · up to 9df02

The change defers mock Requests transport creation so bundled Requests v2 is not autoloaded too early during WP-CLI bootstrap. No merge-blocking issue was found, and new tests cover the lazy-loading and version-selection behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: mock Requests transport creation is moved from file load time to the http_request_options hook. The raw summary and PR objectives both describe this deferral as the pr…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: mock Requests transport creation is moved from file load time to the http_request_options hook. The raw summary and PR objectives both describe this deferral as the primary change, so the title matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d1363dac-2b29-473b-969c-96c18562f6e4
Commits

Reviewing files that changed from the base of the PR and between 791f3c1 and 9df02af.

Files selected for processing: 3

  • phpcs.xml.dist
  • src/Context/templates/mock-requests.php
  • tests/tests/TestMockRequestsTemplate.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions github-actions Bot added bug Something isn't working scope:testing Related to testing labels Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Context/templates/mock-requests.php 0.00% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy added this to the 5.5.4 milestone Nov 19, 2026
@swissspidy
swissspidy force-pushed the fix/lazy-mock-requests-transport branch from 9df02af to 4d037dc Compare November 19, 2026 11:14
@swissspidy
swissspidy merged commit f1d4366 into main Nov 19, 2026
68 of 69 checks passed
@swissspidy
swissspidy deleted the fix/lazy-mock-requests-transport branch November 19, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants