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

Getting Started

Minimal Setup

  1. Installation:

    composer require polishsymfonycommunity/symfony2-mocker-extension
    

    Add to behat.yml:

    extensions:
        PolishSymfonyCommunity\Symfony2MockerExtension\Extension: ~
    
  2. First Use Case: Mock a service in a Behat context:

    use Behat\Behat\Context\Context;
    use PolishSymfonyCommunity\Symfony2MockerExtension\Context\MockerContext;
    
    class FeatureContext implements Context, MockerContext {
        public function mockService($serviceId, $mock) {
            // Mockery mock object or closure
        }
    }
    
  3. Key Files:

    • behat.yml (extension config)
    • Feature context classes (where mocks are defined)
    • Symfony services.yml (for service IDs)

Implementation Patterns

Common Workflows

  1. Mocking Services in Contexts:

    // In a Behat context
    public function __construct() {
        $this->mockService('app.mailer', function($mock) {
            $mock->shouldReceive('send')
                ->once()
                ->with('test@example.com', 'Hello');
        });
    }
    
  2. Partial Mocking:

    $this->mockService('app.user_repository', function($mock) {
        $mock->shouldReceive('findByEmail')
            ->andReturn(new User());
    });
    
  3. Integration with SymfonyMockerContainer:

    // For complex scenarios, inject the container
    public function __construct(SymfonyMockerContainer $container) {
        $this->container = $container;
    }
    

Best Practices

  • Isolate Mocks: Define mocks in dedicated context methods (e.g., mockMailer()).
  • Reset State: Use ->shouldReceive() instead of ->shouldIgnore() to avoid side effects.
  • Avoid Over-Mocking: Prefer real services where possible (e.g., database tests).

Gotchas and Tips

Pitfalls

  1. Service Not Found:

    • Ensure the service ID matches exactly (case-sensitive) with services.yml.
    • Debug with dump($this->getServiceIds()) (if available).
  2. Mockery vs. PHPUnit:

    • Syntax differs from PHPUnit mocks (e.g., shouldReceive() vs. expects()).
    • Use Mockery::mock() for standalone mocks outside the container.
  3. Stateful Mocks:

    • Mocks retain state between steps. Reset with:
      $mock->shouldReceive('method')->andReturnNull()->byDefault();
      

Debugging Tips

  • Verify Mocks:
    $this->mockService('app.service', function($mock) {
        $mock->shouldReceive('method')->andReturn('test');
        // Later, verify:
        $mock->shouldHaveReceived('method')->once();
    });
    
  • Inspect Container:
    $this->getContainer()->getServiceIds(); // List available services
    

Extension Points

  1. Custom Mock Factories: Override PolishSymfonyCommunity\Symfony2MockerExtension\MockFactory for reusable mock logic.

  2. Event Listeners: Attach to SymfonyMockerExtensionEvents (if extended):

    $dispatcher->addListener('mock.service', function($event) { ... });
    
  3. Legacy Code: For Symfony 2.8+, consider migrating to TestDoubleBundle (as suggested in the README). This package is not maintained post-2016.

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