[ISSUE-48] Add Comprehensive Testing Utilities with MockServer and Request Recording#65
Merged
Thavarshan merged 9 commits intomainfrom Nov 17, 2025
Merged
Conversation
…, and request recording Implements a powerful testing framework inspired by Laravel's HTTP client testing, adapted for Fetch PHP's architecture. This provides developers with robust tools for testing HTTP-dependent code without making real network requests. Features: - MockServer with URL pattern matching (wildcards, method-specific, callbacks) - MockResponse fluent builder with convenience methods for all HTTP status codes - MockResponseSequence for testing retry logic and flaky endpoints - Recorder for capturing and replaying request/response pairs - Comprehensive assertion helpers (assertSent, assertNotSent, assertSentCount) - Request recording with JSON export/import for test fixtures - Stray request prevention to ensure complete test coverage - Response delays and exception throwing for edge case testing Implementation Details: - Added MockServer singleton for centralized mock management - Created MockResponse with 10+ convenience factory methods - Implemented HandlesMocking trait integrated into ClientHandler - Added Request::createFromBase() for PSR-7 request wrapping - Modified PerformsHttpRequests to intercept and mock requests Testing: - 84 tests with 182 assertions (all passing) - Unit tests for MockServer, MockResponse, Sequence, and Recorder - Integration tests demonstrating real-world usage patterns - Full PHPStan level max compliance - PSR-12 code style compliance Documentation: - Updated /guide/testing with comprehensive MockServer examples - Added /api/testing with complete API reference - Integrated testing docs into VitePress sidebar navigation - Removed outdated Guzzle MockHandler examples Closes #48 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Updated all async helper function imports to use the correct namespaced paths from the Matrix library (Matrix\Support\*). The Matrix library has namespaced these helper functions, so imports need to use the full namespace. Fixed in: - src/Fetch/Concerns/ManagesPromises.php (all, any, async, await, race, reject, resolve, timeout) - src/Fetch/Concerns/PerformsHttpRequests.php (async) - tests/Unit/ManagesPromisesTest.php (async, await, reject, resolve) - tests/Integration/AsyncRequestsTest.php (all, async, await) All 282 tests now passing with 841 assertions. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
✅ Deploy Preview for fetch-php canceled.
|
Removed Dependabot integration to simplify dependency management: - Deleted .github/dependabot.yml (Composer, NPM, and GitHub Actions updates) - Deleted .github/workflows/dependabot-auto-merge.yml (auto-merge workflow) Note: The dependency-review.yml workflow remains active for security reviews on PRs. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Added comprehensive changelog entry for v3.3.0 including: - New testing utilities (MockServer, MockResponse, MockResponseSequence, Recorder) - 117 new tests with 288 assertions - Comprehensive testing documentation - Fixed async helper function imports - Removed Dependabot configuration Also cleaned up duplicate entries at the end of the changelog. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Changed `allow-dependencies-licenses` to `allow-licenses` parameter. The incorrect parameter name was causing package-url parsing errors. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.OpenSSF Scorecard
Scanned Files
|
The pcntl extension is Unix-only and not available on Windows. Added --ignore-platform-req=ext-pcntl flag for Windows builds to allow composer install to succeed. Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
The --prefer-lowest flag only works with 'composer update', not 'composer install'. Split the install step into two conditional steps: - Regular install for stable builds - Update with --prefer-lowest for lowest dependency builds Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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 powerful testing framework for Fetch PHP, inspired by Laravel's HTTP client testing utilities. It provides developers with robust tools for testing HTTP-dependent code without making real network requests.
Key Features:
Fixes #48
Type of Change
Changes Made
Core Testing Utilities
1. MockResponse (
src/Fetch/Testing/MockResponse.php)create(),json(),sequence()ok(),created(),noContent()badRequest(),unauthorized(),forbidden(),notFound(),unprocessableEntity()serverError(),serviceUnavailable()2. MockResponseSequence (
src/Fetch/Testing/MockResponseSequence.php)push(),pushJson(),pushStatus(),pushResponse()3. MockServer (
src/Fetch/Testing/MockServer.php)https://api.example.com/users/*)POST https://api.example.com/users)assertSent(),assertNotSent(),assertSentCount(),assertNothingSent()4. Recorder (
src/Fetch/Testing/Recorder.php)start(),stop(),replay(),exportToJson(),importFromJson()5. HandlesMocking Trait (
src/Fetch/Concerns/HandlesMocking.php)Integration Changes
Modified Files:
src/Fetch/Http/ClientHandler.php- Added HandlesMocking traitsrc/Fetch/Concerns/PerformsHttpRequests.php- Added mock request interceptionsrc/Fetch/Http/Request.php- AddedcreateFromBase()for PSR-7 conversionsrc/Fetch/Concerns/ManagesPromises.php- Fixed async helper function importstests/Integration/AsyncRequestsTest.php- Fixed async helper function importstests/Unit/ManagesPromisesTest.php- Fixed async helper function importsDocumentation
New Documentation:
docs/guide/testing.md- Comprehensive testing guide with examples (617 lines)docs/api/testing.md- Complete API reference (539 lines)docs/.vitepress/config.mts- Added testing section to sidebar navigationDocumentation Highlights:
Tests
New Test Files:
tests/Unit/Testing/MockResponseTest.php- 40 tests, 82 assertionstests/Unit/Testing/MockResponseSequenceTest.php- 28 tests, 72 assertionstests/Unit/Testing/MockServerTest.php- 18 tests, 44 assertionstests/Unit/Testing/RecorderTest.php- 15 tests, 31 assertionstests/Integration/MockingIntegrationTest.php- 16 tests, 59 assertionsTotal: 117 new tests with 288 assertions
Bug Fixes
Fixed pre-existing async helper function import issues by updating all imports to use the correct
Matrix\Support\*namespace:async,await,all,any,race,reject,resolve,timeoutTesting
How has this been tested?
Test Coverage:
Test Environment:
Code Quality:
Documentation
docs/guide/testing.md)docs/api/testing.md)Checklist
Usage Examples
Basic Mocking
Testing Retry Logic
Recording and Replaying Requests
Additional Notes
Architecture Decisions:
Singleton Pattern: Both MockServer and Recorder use singleton pattern for global test state management, similar to Laravel's approach.
Trait-based Integration: HandlesMocking trait provides clean separation and easy integration into the existing ClientHandler architecture.
PSR-7 Compatibility: Full compatibility with PSR-7 HTTP messages maintained throughout.
Non-invasive Interception: Mock interception happens at the request execution level, preserving all existing functionality.
Pattern Matching: Flexible pattern matching with wildcards and method prefixes provides powerful test configuration options.
Performance Considerations:
Future Enhancements (out of scope for this PR):
Breaking Changes: None - this is a purely additive feature that doesn't affect existing functionality.