ramunasd / symfony-container-mocks
Provides extended Symfony dependency injection container enabling service mocking.
Installs: 87 656
Dependents: 2
Suggesters: 0
Security: 0
Stars: 22
Watchers: 4
Forks: 3
Open Issues: 1
Requires
- php: >=5.6
- phpspec/prophecy: ~1.6
- symfony/dependency-injection: >=2.3
Requires (Dev)
- phpunit/phpunit: ^5.7
- symfony/config: >=2.3
- symfony/framework-bundle: >=2.3
- symfony/http-kernel: >=2.3
README
This container enables you to mock services in the Symfony dependency injection container. It is particularly useful in functional tests.
Features
- Can replace any Symfony service or parameter
- Automatically detects service class from service definition
- Can be used with any mocking framework
- Compatible with Symfony versions 2.7 - 3.4
- Works on all supported version of PHP
OTB supported mocking frameworks
Installation
Add SymfonyContainerMocks using composer:
composer require "ramunasd/symfony-container-mocks"
or edit your composer.json:
{ "require": { "ramunasd/symfony-container-mocks": "*" } }
Replace base container class for test environment in app/AppKernel.php
<?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; use RDV\SymfonyContainerMocks\DependencyInjection\TestKernelTrait; class AppKernel extends Kernel { // use special container when env=test use TestKernelTrait; public function registerBundles() { return [ // bundles ]; } public function registerContainerConfiguration(LoaderInterface $loader) { } }
And clear application cache.
Examples
Inject mocked service
<?php namespace Acme\Bundle\AcmeBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Acme\Bundle\AcmeBundle\Service\Custom; class AcmeControllerTest extends WebTestCase { /** * @var \Symfony\Bundle\FrameworkBundle\Client $client */ private $client; public function setUp() { parent::setUp(); $this->client = static::createClient(); } public function tearDown() { $this->client->getContainer()->tearDown(); $this->client = null; parent::tearDown(); } public function testSomethingWithMockedService() { $this->client->getContainer()->prophesize('acme.service.custom', Custom::class) ->someMethod([]) ->willReturn(false) ->shouldBeCalledTimes(2); // ... } }
Inject automatically mocked service
feature works only with flag "debug" enabled.
$mock = $this->client->getContainer()->prophesize('acme.service.custom'); $mock ->myMethod() ->willReturn(true);
Other mocking frameworks
// create stub $mock = $this->getMock(Custom::class); // inject service mock self::$kernel->getContainer()->setMock('acme.service.custom', $mock); // reset container state self::$kernel->getContainer()->unMock('acme.service.custom');
Set specific framework parameter
// set custom value during test self::$kernel->getContainer()->setMockedParameter('acme.service.parameter1', 'customValue1'); // trigger service, assert results // reset all parameters to original values self::$kernel->getContainer()->clearMockedParameters();
Things TO DO
- Symfony 4.x support
- PSR-11 adoption
Similar/Related projects
- https://github.com/jakzal/phpunit-injector - inject Symfony services into PHPUnit test cases