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

Symfony2 Mocker Extension Laravel Package

polishsymfonycommunity/symfony2-mocker-extension

Behat extension for Symfony2 that lets you mock services in the dependency injection container during tests. Built on Mockery and SymfonyMockerContainer. Consider it a hack and use sparingly; alternative: TestDoubleBundle.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Limited Fit for Laravel: This package is Symfony2-specific and leverages Symfony’s DI container, which is fundamentally different from Laravel’s Service Container (PSR-11 compliant). Laravel’s container uses bindings, singletons, and contextual binding, whereas Symfony2’s DI is more rigidly defined in XML/YAML/PHP.
  • Mockery Dependency: Relies on Mockery, a PHP mocking library, which Laravel already supports natively (via Mockery facade or phpunit/mock-objects). No added value here.
  • Behat Integration: Targets Symfony2’s Behat ecosystem, which Laravel replaces with PestPHP, Laravel Dusk, or PHPUnit. No direct equivalent in Laravel’s testing stack.

Integration Feasibility

  • Low Feasibility: Laravel’s DI container is not compatible with Symfony2’s ContainerInterface. Direct integration would require:
    • A wrapper layer to adapt Laravel’s container to Symfony’s expectations (high effort, fragile).
    • Manual service redefinition in tests (defeats the purpose of automation).
  • Alternative Approaches:
    • Use Laravel’s built-in mocking (partialMock, createMock, or Mockery).
    • Leverage Laravel’s Testing facade for HTTP-based mocking (e.g., Http::fake()).
    • For complex DI scenarios, consider orchestra/testbench (for testing packages in isolation).

Technical Risk

  • High Risk:
    • Breaking Changes: Laravel’s container evolves (e.g., PSR-11 compliance, improvements in Laravel 10+). This package hasn’t been updated since 2016 and lacks Laravel-specific awareness.
    • Maintenance Overhead: Requires custom adapters or forks to work, increasing technical debt.
    • False Sense of Security: Mocking the entire container can hide real integration issues (as the package itself warns).
  • Opportunity Cost: Time spent integrating this would be better allocated to native Laravel testing tools or design improvements (e.g., dependency inversion, interfaces).

Key Questions

  1. Why not use Laravel’s native testing tools?
    • Does the team lack familiarity with Mockery, PHPUnit, or PestPHP?
    • Are there specific Symfony2 dependencies that can’t be mocked otherwise?
  2. What’s the ROI of this integration?
    • Does it solve a critical gap in Laravel’s testing ecosystem, or is it a legacy workaround?
  3. Is there a minimal viable alternative?
    • Could a custom Laravel extension (e.g., a ServiceContainerMock trait) achieve the same goal with less risk?
  4. What’s the long-term maintenance plan?
    • Who will ensure compatibility with future Laravel versions?
  5. Has the team evaluated TestDoubleBundle (the recommended alternative)?
    • Could it be adapted for Laravel with lower effort?

Integration Approach

Stack Fit

  • Poor Fit: Designed for Symfony2 + Behat, not Laravel’s ecosystem.
    • Laravel Alternatives:
      • Unit Testing: PHPUnit + Mockery/phpunit/mock-objects.
      • Feature Testing: PestPHP or Laravel Dusk.
      • Package Testing: orchestra/testbench.
      • HTTP Mocking: Http::fake(), Route::fake().
  • Mockery Compatibility: Laravel already supports Mockery, so no new dependencies are needed for basic mocking.

Migration Path

  1. Assess Current Testing Strategy:
    • Audit existing tests to identify where container mocking is "needed."
    • Determine if the problem is design-related (e.g., tight coupling) or test isolation.
  2. Phase Out Dependency:
    • Replace Symfony2-specific mocks with Laravel’s partialMock or interface-based dependencies.
    • Example:
      // Instead of Symfony2MockerExtension:
      $mockService = Mockery::mock(Service::class);
      $this->app->instance(Service::class, $mockService);
      
  3. Leverage Native Tools:
    • Use Http::fake() for HTTP-related tests.
    • Use DatabaseMigrations or DatabaseTransactions for DB tests.
  4. Last Resort: Custom Adapter:
    • If absolutely necessary, build a minimal Laravel-compatible wrapper around SymfonyMockerContainer, but document this as technical debt.

Compatibility

  • No Direct Compatibility:
    • Symfony2’s ContainerInterface ≠ Laravel’s Illuminate\Contracts\Container\Container.
    • Behat’s event system in Symfony2 ≠ Laravel’s testing helpers.
  • Workarounds:
    • Service Provider Mocking: Manually mock services in setUp():
      public function setUp(): void
      {
          $this->app->instance(MyService::class, Mockery::mock(MyService::class));
      }
      
    • Trait-Based Mocking: Create a reusable trait for common mocks.

Sequencing

  1. Short-Term (0-2 Weeks):
    • Replace critical Symfony2MockerExtension usage with Laravel-native mocks.
    • Refactor tests to use interfaces instead of concrete container services.
  2. Medium-Term (2-4 Weeks):
    • Deprecate the package in favor of custom solutions (e.g., a MockContainer trait).
    • Update CI/CD to fail tests using the old package.
  3. Long-Term (1+ Month):
    • Fully migrate to PestPHP/Laravel Dusk for feature tests.
    • Archive the package or fork it as a legacy reference.

Operational Impact

Maintenance

  • High Maintenance Burden:
    • No Updates Since 2016: Risk of breaking with Laravel 9+ (e.g., PSR-11 changes).
    • Custom Code Required: Any integration will need ongoing adaptation as Laravel evolves.
  • Dependency Risks:
    • Relies on SymfonyMockerContainer, which may have its own compatibility issues.
    • Mockery is stable, but version conflicts could arise.

Support

  • Limited Community Support:
    • 26 stars, 0 dependents: Indicates low adoption and no active maintenance.
    • No Laravel-Specific Documentation: All usage examples are Symfony2-focused.
  • Debugging Challenges:
    • Errors will be obscure due to container adaptation layers.
    • Stack traces may not be helpful for Laravel-specific issues.

Scaling

  • Not Scalable:
    • Performance Overhead: Mocking the entire container is slow and brittle.
    • Test Fragility: Changes to DI definitions will break tests frequently.
  • Better Alternatives Scale Better:
    • PestPHP: Faster test execution with modern syntax.
    • Testbench: Isolated package testing without container mocking.

Failure Modes

  1. Container Mismatch Errors:
    • ClassNotFoundException or BindingResolutionException due to Laravel/Symfony2 DI differences.
  2. Test Flakiness:
    • Mocks may not align with real service behavior, leading to false positives/negatives.
  3. Upgrade Blockers:
    • Laravel minor updates (e.g., 9.x → 10.x) may break the integration.
  4. Design Debt Accumulation:
    • Over-reliance on container mocking hides poor architecture, leading to technical debt.

Ramp-Up

  • Steep Learning Curve:
    • Requires understanding of:
      • Symfony2 DI (unfamiliar to Laravel devs).
      • Mockery/SymfonyMockerContainer internals.
      • Custom adapter logic for Laravel.
  • Onboarding Time:
    • 1-2 weeks for a senior dev to prototype an integration.
    • Additional 2-4 weeks to stabilize and document.
  • Better Alternatives:
    • PestPHP: 1-day ramp-up for Laravel devs.
    • Mockery Basics: Already known by most PHP devs.
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.
nasirkhan/laravel-sharekit
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