Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mock Api Bundle Laravel Package

bneumann/mock-api-bundle

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony/Laravel Compatibility: The package is a Symfony bundle, not a Laravel package, but Laravel’s Symfony integration (via symfony/http-client, symfony/process, etc.) allows partial adoption. The core concept—mocking HTTP requests—aligns with Laravel’s testing needs (e.g., Http::fake() in Laravel), but the implementation is not natively Laravel-compatible without abstraction.
  • Use Case Alignment: Ideal for integration testing of third-party APIs where external dependencies are undesirable. Laravel’s built-in Http::fake() is more mature, but this bundle offers YAML-driven mocks, which could appeal to teams preferring declarative configurations over PHP-based fakes.
  • Isolation Scope: Explicitly designed for test environments only, leveraging Symfony’s test bundle flag. Laravel’s Http::fake() also enforces this, but the bundle’s service decoration approach is more explicit and configurable.

Integration Feasibility

  • Laravel HTTP Client: The bundle targets Symfony’s HttpClient, but Laravel’s GuzzleHttp/SymfonyHttpClient can be bridged via custom decorators or service providers. Feasible but requires wrapper logic to intercept Laravel’s HTTP calls.
  • YAML Configuration: Laravel’s testing ecosystem leans toward PHP-based fakes (e.g., Http::fake() with closures) or JSON/XML stubs. YAML is less common but could be parsed via spatie/array-to-xml or similar for hybrid setups.
  • Dependency Overhead: Minimal (symfony/http-client, symfony/yaml), but Laravel projects may already have conflicting versions of these packages (e.g., Symfony 6.x vs. Laravel’s older symfony/process).

Technical Risk

  • Non-Native Laravel Support: High risk of integration friction without custom glue code. Laravel’s Http::fake() is the de facto standard; this bundle adds another layer of abstraction.
  • Configuration Complexity: YAML mocks require file system management (e.g., tests/mocks/ directory), which may clash with Laravel’s convention of inline test data (e.g., tests/Feature/ApiTest.php).
  • Limited Adoption: Low stars/dependents suggest unproven stability. Laravel teams may prefer bailer-laravel-http-faker or mockery for similar use cases.
  • Test Environment Leakage: Risk of accidental activation in production if bundle registration isn’t strictly scoped to test => true.

Key Questions

  1. Why not Laravel’s Http::fake()?
    • Does the team need YAML-driven mocks for non-developers (e.g., QA teams)?
    • Are there complex mocking scenarios (e.g., dynamic responses based on headers) that Http::fake() can’t handle?
  2. How will this integrate with Laravel’s HTTP stack?
    • Will we use Symfony’s HttpClient directly (breaking Laravel conventions) or wrap Guzzle?
    • What’s the fallback strategy if the bundle fails (e.g., during CI runs)?
  3. Maintenance Burden
    • Who will manage YAML mock files? How will they be version-controlled?
    • How will mocks be shared across test suites (e.g., unit vs. integration tests)?
  4. Performance Impact
    • Does YAML parsing add measurable overhead compared to PHP-based fakes?
    • How will large mock datasets (e.g., paginated APIs) be handled?
  5. Long-Term Viability
    • Is the bundle actively maintained? What’s the upgrade path if it becomes obsolete?
    • Are there alternatives (e.g., vcrphp/vcr, laravel-mockhttp) with better Laravel support?

Integration Approach

Stack Fit

  • Laravel Compatibility Layer:

    • Option 1: Symfony HttpClient Bridge
      • Use symfony/http-client as a secondary HTTP client in tests, decorated by the bundle.
      • Pros: Clean separation; avoids polluting Laravel’s Guzzle stack.
      • Cons: Requires dual HTTP client management in tests.
    • Option 2: Guzzle Middleware Wrapper
      • Create a custom Guzzle middleware to intercept requests and route them to the bundle’s mock system.
      • Pros: Seamless with Laravel’s Http facade.
      • Cons: Higher development effort; risk of middleware conflicts.
    • Option 3: Laravel Service Provider Override
      • Replace Laravel’s HttpClient with a decorated version that checks for mocks.
      • Pros: Transparent to tests.
      • Cons: Tight coupling; harder to debug.
  • Configuration Strategy:

    • Hybrid YAML/PHP: Parse YAML mocks into PHP arrays for use with Http::fake().
      • Example: Use spatie/array-to-xml or custom loader to convert YAML to Laravel-compatible stubs.
    • Directory Structure:
      tests/
      ├── mocks/               # YAML files (bundle default)
      └── Http/                # Laravel's Http::fake() stubs (optional fallback)
      

Migration Path

  1. Pilot Phase:
    • Start with one critical API integration (e.g., payment gateway).
    • Compare test coverage and maintenance effort vs. Http::fake().
  2. Incremental Adoption:
    • Gradually replace Http::fake() calls with YAML mocks for stable APIs.
    • Use feature flags to toggle between mocking strategies.
  3. Tooling Integration:
    • Add custom Artisan commands to generate YAML mocks from real API responses (e.g., php artisan mock:record).
    • Integrate with GitHub Actions to validate mocks against schema changes.

Compatibility

  • Laravel Versions:
    • Tested on Laravel 8+ (Symfony 5.4+ compatibility).
    • Laravel 9/10: May require Symfony 6.x adjustments (e.g., HttpClient changes).
  • HTTP Client Backends:
    • Works with Guzzle (default) and Symfony HttpClient if explicitly configured.
    • Edge Case: Fails silently if the request URL/body doesn’t match any mock (design choice).
  • Testing Frameworks:
    • Primarily for PHPUnit/Pest; may need custom assertions for Livewire/Inertia tests.

Sequencing

  1. Phase 1: Proof of Concept (2 weeks)
    • Set up bundle in a dedicated test project.
    • Migrate 2–3 API tests from Http::fake() to YAML.
    • Measure test execution time and flakiness.
  2. Phase 2: Integration (3 weeks)
    • Build Guzzle middleware or Symfony bridge.
    • Update CI pipeline to validate mocks.
    • Train team on YAML mock conventions.
  3. Phase 3: Full Rollout (Ongoing)
    • Deprecate Http::fake() for new API tests.
    • Archive legacy mocks in a separate legacy_mocks/ directory.

Operational Impact

Maintenance

  • Mock File Management:
    • Pros: YAML is human-readable; easier for non-devs to update.
    • Cons: No built-in validation (e.g., schema checks for responses).
    • Mitigation: Use JSON Schema or custom validation in tests.
  • Dependency Updates:
    • Bundle relies on Symfony packages; Laravel’s locked versions may cause conflicts.
    • Solution: Pin Symfony dependencies in composer.json or use platform configs.
  • Test Data Versioning:
    • Mocks should be committed to Git but may drift from production APIs.
    • Strategy: Use semantic versioning for mock files (e.g., v1/mock_payment.yml).

Support

  • Debugging Complexity:
    • Harder to debug than Http::fake() (no IDE autocompletion for YAML).
    • Workaround: Add logging middleware to dump unmatched requests.
  • Onboarding:
    • Steep learning curve for teams unfamiliar with YAML + Symfony.
    • Solution: Create internal docs with examples for common APIs (e.g., Stripe, AWS).
  • Community Support:
    • Limited due to low adoption. Expect self-service troubleshooting.

Scaling

  • Performance:
    • YAML parsing adds ~5–10ms per test (negligible for most cases).
    • Bottleneck: Large mock files (e.g., 100+ endpoints) may slow test suite startup.
    • Optimization: Use
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony