polishsymfonycommunity/symfony-mocker-container
Installation:
composer require polishsymfonycommunity/symfony-mocker-container
Add the package to your composer.json under require (as shown in the README).
Configure Kernel:
Override getContainerBaseClass() in AppKernel.php (or Kernel.php in Symfony 4+):
protected function getContainerBaseClass()
{
if ('test' === $this->getEnvironment()) {
return \PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer::class;
}
return parent::getContainerBaseClass();
}
Clear cache:
php bin/console cache:clear
First Use Case: In a functional test (or Behat scenario), mock a service:
$mock = $this->getMockBuilder('App\Service\MyService')
->disableOriginalConstructor()
->getMock();
$this->container->set('app.my_service', $mock);
Functional Tests:
MockerContainer to replace real services with mocks/stubs.$mockDb = $this->getMockBuilder('Doctrine\DBAL\Connection')
->disableOriginalConstructor()
->getMock();
$mockDb->method('fetchAll')
->willReturn([['id' => 1, 'name' => 'Test']]);
$this->container->set('database_connection', $mockDb);
Behat Integration:
Symfony2MockerExtension for scenario-level mocking.use Behat\MinkExtension\Context\MinkContext;
use PSS\SymfonyMockerExtension\Context\MockerContext;
class FeatureContext extends MinkContext implements MockerContext {
public function mockService($serviceId, $mockClass) {
$mock = $this->getMockBuilder($mockClass)
->disableOriginalConstructor()
->getMock();
$this->container->set($serviceId, $mock);
}
}
Partial Mocking:
$partialMock = $this->getMockBuilder('App\Service\Logger')
->setMethods(['log'])
->getMock();
$partialMock->expects($this->once())
->method('log')
->with('Test message');
$this->container->set('app.logger', $partialMock);
BrowserKitDriver (Symfony functional tests/Behat). HTTP-based drivers (e.g., Goutte) cannot use this.$realService = $this->container->get('app.real_service');
$mock = $this->getMockBuilder(get_class($realService))->getMock();
$this->container->set('app.alias', $mock);
Cache Dependency:
AppKernel.php will cause the mocker container to not activate.php bin/console cache:clear
Environment Mismatch:
test environment. Running tests in dev or prod will ignore the override.Non-Test Drivers:
Goutte, Selenium2Driver, or other HTTP-based drivers will fail silently. The mocker only works with BrowserKitDriver.Lazy Services:
lazy: true in YAML/XML) may not be mockable directly. Resolve them first:
$this->container->get('app.lazy_service'); // Force initialization
$mock = $this->getMockBuilder(get_class($service))->getMock();
$this->container->set('app.lazy_service', $mock);
Constructor Injection:
->disableOriginalConstructor() or$this->assertInstanceOf(
\PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer::class,
$this->container
);
$this->container->set() fails, the service ID might be incorrect. Verify with:
$this->container->has('app.my_service'); // Returns bool
->shouldReceive() for older PHPUnit versions:
$mock->shouldReceive('methodName')
->once()
->andReturn('mocked value');
Custom Mock Factories: Extend the mocker to support custom mock creation:
$this->container->set('app.custom_service', $this->createCustomMock());
Example factory method:
protected function createCustomMock() {
return $this->getMockBuilder('App\Service\CustomService')
->setMethods(['customMethod'])
->getMock();
}
Reset Mocks Between Tests:
Use PHPUnit's afterEach() to reset the container:
public function tearDown(): void {
$this->container = static::createClient()->getContainer();
}
Mocking Private Methods: Enable private method mocking (PHPUnit 5.3+):
$mock = $this->getMockBuilder('App\Service\PrivateService')
->setMethods(['privateMethod'])
->getMock();
$mock->expects($this->any())
->method('privateMethod')
->will($this->returnValue('mocked'));
How can I help you explore Laravel packages today?