minor #26087 do not mock the container builder in tests (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

do not mock the container builder in tests

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

dab4222 do not mock the container builder in tests
This commit is contained in:
Nicolas Grekas 2018-02-11 11:53:40 +01:00
commit 65bab998e5
14 changed files with 295 additions and 660 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
@ -19,73 +20,44 @@ class AddCacheWarmerPassTest extends TestCase
{ {
public function testThatCacheWarmersAreProcessedInPriorityOrder() public function testThatCacheWarmersAreProcessedInPriorityOrder()
{ {
$services = array( $container = new ContainerBuilder();
'my_cache_warmer_service1' => array(0 => array('priority' => 100)), $cacheWarmerDefinition = $container->register('cache_warmer')->addArgument(array());
'my_cache_warmer_service2' => array(0 => array('priority' => 200)), $container->register('my_cache_warmer_service1')->addTag('kernel.cache_warmer', array('priority' => 100));
'my_cache_warmer_service3' => array(), $container->register('my_cache_warmer_service2')->addTag('kernel.cache_warmer', array('priority' => 200));
); $container->register('my_cache_warmer_service3')->addTag('kernel.cache_warmer');
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'getDefinition', 'hasDefinition'))->getMock();
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$container->expects($this->atLeastOnce())
->method('getDefinition')
->with('cache_warmer')
->will($this->returnValue($definition));
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('cache_warmer')
->will($this->returnValue(true));
$definition->expects($this->once())
->method('replaceArgument')
->with(0, array(
new Reference('my_cache_warmer_service2'),
new Reference('my_cache_warmer_service1'),
new Reference('my_cache_warmer_service3'),
));
$addCacheWarmerPass = new AddCacheWarmerPass(); $addCacheWarmerPass = new AddCacheWarmerPass();
$addCacheWarmerPass->process($container); $addCacheWarmerPass->process($container);
$this->assertEquals(
array(
new Reference('my_cache_warmer_service2'),
new Reference('my_cache_warmer_service1'),
new Reference('my_cache_warmer_service3'),
),
$cacheWarmerDefinition->getArgument(0)
);
} }
public function testThatCompilerPassIsIgnoredIfThereIsNoCacheWarmerDefinition() public function testThatCompilerPassIsIgnoredIfThereIsNoCacheWarmerDefinition()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container = new ContainerBuilder();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$container->expects($this->never())->method('findTaggedServiceIds');
$container->expects($this->never())->method('getDefinition');
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('cache_warmer')
->will($this->returnValue(false));
$definition->expects($this->never())->method('replaceArgument');
$addCacheWarmerPass = new AddCacheWarmerPass(); $addCacheWarmerPass = new AddCacheWarmerPass();
$addCacheWarmerPass->process($container); $addCacheWarmerPass->process($container);
// we just check that the pass does not break if no cache warmer is registered
$this->addToAssertionCount(1);
} }
public function testThatCacheWarmersMightBeNotDefined() public function testThatCacheWarmersMightBeNotDefined()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container = new ContainerBuilder();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); $cacheWarmerDefinition = $container->register('cache_warmer')->addArgument(array());
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue(array()));
$container->expects($this->never())->method('getDefinition');
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('cache_warmer')
->will($this->returnValue(true));
$definition->expects($this->never())->method('replaceArgument');
$addCacheWarmerPass = new AddCacheWarmerPass(); $addCacheWarmerPass = new AddCacheWarmerPass();
$addCacheWarmerPass->process($container); $addCacheWarmerPass->process($container);
$this->assertSame(array(), $cacheWarmerDefinition->getArgument(0));
} }
} }

View File

@ -13,72 +13,40 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class AddConstraintValidatorsPassTest extends TestCase class AddConstraintValidatorsPassTest extends TestCase
{ {
public function testThatConstraintValidatorServicesAreProcessed() public function testThatConstraintValidatorServicesAreProcessed()
{ {
$services = array( $container = new ContainerBuilder();
'my_constraint_validator_service1' => array(0 => array('alias' => 'my_constraint_validator_alias1')), $constraintValidatorFactoryDefinition = $container->register('validator.validator_factory')
'my_constraint_validator_service2' => array(), ->setArguments(array(new Reference('service_container'), array()));
); $container->register('my_constraint_validator_service1', 'My\Fully\Qualified\Class\Named\Validator1')
->addTag('validator.constraint_validator', array('alias' => 'my_constraint_validator_alias1'));
$validatorFactoryDefinition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container->register('my_constraint_validator_service2', 'My\Fully\Qualified\Class\Named\Validator2')
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'getDefinition', 'hasDefinition'))->getMock(); ->addTag('validator.constraint_validator');
$validatorDefinition1 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->setMethods(array('getClass'))->getMock();
$validatorDefinition2 = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->setMethods(array('getClass'))->getMock();
$validatorDefinition1->expects($this->atLeastOnce())
->method('getClass')
->willReturn('My\Fully\Qualified\Class\Named\Validator1');
$validatorDefinition2->expects($this->atLeastOnce())
->method('getClass')
->willReturn('My\Fully\Qualified\Class\Named\Validator2');
$container->expects($this->any())
->method('getDefinition')
->with($this->anything())
->will($this->returnValueMap(array(
array('my_constraint_validator_service1', $validatorDefinition1),
array('my_constraint_validator_service2', $validatorDefinition2),
array('validator.validator_factory', $validatorFactoryDefinition),
)));
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('validator.validator_factory')
->will($this->returnValue(true));
$validatorFactoryDefinition->expects($this->once())
->method('replaceArgument')
->with(1, array(
'My\Fully\Qualified\Class\Named\Validator1' => 'my_constraint_validator_service1',
'my_constraint_validator_alias1' => 'my_constraint_validator_service1',
'My\Fully\Qualified\Class\Named\Validator2' => 'my_constraint_validator_service2',
));
$addConstraintValidatorsPass = new AddConstraintValidatorsPass(); $addConstraintValidatorsPass = new AddConstraintValidatorsPass();
$addConstraintValidatorsPass->process($container); $addConstraintValidatorsPass->process($container);
$this->assertEquals(
array(
'My\Fully\Qualified\Class\Named\Validator1' => 'my_constraint_validator_service1',
'my_constraint_validator_alias1' => 'my_constraint_validator_service1',
'My\Fully\Qualified\Class\Named\Validator2' => 'my_constraint_validator_service2',
),
$constraintValidatorFactoryDefinition->getArgument(1)
);
} }
public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition() public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$container->expects($this->never())->method('findTaggedServiceIds');
$container->expects($this->never())->method('getDefinition');
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('validator.validator_factory')
->will($this->returnValue(false));
$definition->expects($this->never())->method('replaceArgument');
$addConstraintValidatorsPass = new AddConstraintValidatorsPass(); $addConstraintValidatorsPass = new AddConstraintValidatorsPass();
$addConstraintValidatorsPass->process($container); $addConstraintValidatorsPass->process(new ContainerBuilder());
// we just check that the pass does not fail if no constraint validator factory is registered
$this->addToAssertionCount(1);
} }
} }

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FragmentRendererPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FragmentRendererPass;
@ -29,29 +30,10 @@ class LegacyFragmentRendererPassTest extends TestCase
*/ */
public function testContentRendererWithoutInterface() public function testContentRendererWithoutInterface()
{ {
// one service, not implementing any interface $builder = new ContainerBuilder();
$services = array( $builder->register('fragment.handler');
'my_content_renderer' => array(), $builder->register('my_content_renderer', 'stdClass')
); ->addTag('kernel.fragment_renderer');
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('stdClass'));
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.fragment_renderer here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->returnValue($definition));
$pass = new FragmentRendererPass(); $pass = new FragmentRendererPass();
$pass->process($builder); $pass->process($builder);
@ -59,38 +41,15 @@ class LegacyFragmentRendererPassTest extends TestCase
public function testValidContentRenderer() public function testValidContentRenderer()
{ {
$services = array( $builder = new ContainerBuilder();
'my_content_renderer' => array(), $fragmentHandlerDefinition = $builder->register('fragment.handler');
); $builder->register('my_content_renderer', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\RendererService')
->addTag('kernel.fragment_renderer');
$renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$renderer
->expects($this->once())
->method('addMethodCall')
->with('addRenderer', array(new Reference('my_content_renderer')))
;
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\RendererService'));
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.fragment_renderer here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->onConsecutiveCalls($renderer, $definition));
$pass = new FragmentRendererPass(); $pass = new FragmentRendererPass();
$pass->process($builder); $pass->process($builder);
$this->assertEquals(array(array('addRenderer', array(new Reference('my_content_renderer')))), $fragmentHandlerDefinition->getMethodCalls());
} }
} }

View File

@ -13,75 +13,52 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class LoggingTranslatorPassTest extends TestCase class LoggingTranslatorPassTest extends TestCase
{ {
public function testProcess() public function testProcess()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container = new ContainerBuilder();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); $container->setParameter('translator.logging', true);
$parameterBag = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface')->getMock(); $container->setParameter('translator.class', 'Symfony\Component\Translation\Translator');
$container->register('monolog.logger');
$container->expects($this->exactly(2)) $container->setAlias('logger', 'monolog.logger');
->method('hasAlias') $container->register('translator.default', '%translator.class%');
->will($this->returnValue(true)); $container->register('translator.logging', '%translator.class%');
$container->setAlias('translator', 'translator.default');
$container->expects($this->once()) $translationWarmerDefinition = $container->register('translation.warmer')->addArgument(new Reference('translator'));
->method('getParameter')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getAlias')
->will($this->returnValue('translation.default'));
$container->expects($this->exactly(3))
->method('getDefinition')
->will($this->returnValue($definition));
$container->expects($this->once())
->method('hasParameter')
->with('translator.logging')
->will($this->returnValue(true));
$definition->expects($this->once())
->method('getClass')
->will($this->returnValue('%translator.class%'));
$parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnValue("Symfony\Bundle\FrameworkBundle\Translation\Translator"));
$container->expects($this->once())
->method('getParameterBag')
->will($this->returnValue($parameterBag));
$pass = new LoggingTranslatorPass(); $pass = new LoggingTranslatorPass();
$pass->process($container); $pass->process($container);
$this->assertEquals(new Reference('translator.logging.inner'), $translationWarmerDefinition->getArgument(0));
} }
public function testThatCompilerPassIsIgnoredIfThereIsNotLoggerDefinition() public function testThatCompilerPassIsIgnoredIfThereIsNotLoggerDefinition()
{ {
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); $container = new ContainerBuilder();
$container->expects($this->once()) $container->register('identity_translator');
->method('hasAlias') $container->setAlias('translator', 'identity_translator');
->will($this->returnValue(false));
$pass = new LoggingTranslatorPass(); $pass = new LoggingTranslatorPass();
$pass->process($container); $pass->process($container);
// we just check that the compiler pass does not break if a logger is not registered
$this->addToAssertionCount(1);
} }
public function testThatCompilerPassIsIgnoredIfThereIsNotTranslatorDefinition() public function testThatCompilerPassIsIgnoredIfThereIsNotTranslatorDefinition()
{ {
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); $container = new ContainerBuilder();
$container->expects($this->at(0)) $container->register('monolog.logger');
->method('hasAlias') $container->setAlias('logger', 'monolog.logger');
->will($this->returnValue(true));
$container->expects($this->at(0))
->method('hasAlias')
->will($this->returnValue(false));
$pass = new LoggingTranslatorPass(); $pass = new LoggingTranslatorPass();
$pass->process($container); $pass->process($container);
// we just check that the compiler pass does not break if a translator is not registered
$this->addToAssertionCount(1);
} }
} }

View File

@ -12,18 +12,11 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
class ProfilerPassTest extends TestCase class ProfilerPassTest extends TestCase
{ {
private $profilerDefinition;
protected function setUp()
{
$this->profilerDefinition = new Definition('ProfilerClass');
}
/** /**
* Tests that collectors that specify a template but no "id" will throw * Tests that collectors that specify a template but no "id" will throw
* an exception (both are needed if the template is specified). * an exception (both are needed if the template is specified).
@ -31,17 +24,15 @@ class ProfilerPassTest extends TestCase
* Thus, a fully-valid tag looks something like this: * Thus, a fully-valid tag looks something like this:
* *
* <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" /> * <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" />
*
* @expectedException \InvalidArgumentException
*/ */
public function testTemplateNoIdThrowsException() public function testTemplateNoIdThrowsException()
{ {
// one service, with a template key, but no id $builder = new ContainerBuilder();
$services = array( $builder->register('profiler', 'ProfilerClass');
'my_collector_service' => array(0 => array('template' => 'foo')), $builder->register('my_collector_service')
); ->addTag('data_collector', array('template' => 'foo'));
$builder = $this->createContainerMock($services);
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('InvalidArgumentException');
$profilerPass = new ProfilerPass(); $profilerPass = new ProfilerPass();
$profilerPass->process($builder); $profilerPass->process($builder);
@ -49,45 +40,19 @@ class ProfilerPassTest extends TestCase
public function testValidCollector() public function testValidCollector()
{ {
// one service, with a template key, but no id $container = new ContainerBuilder();
$services = array( $profilerDefinition = $container->register('profiler', 'ProfilerClass');
'my_collector_service' => array(0 => array('template' => 'foo', 'id' => 'my_collector')), $container->register('my_collector_service')
); ->addTag('data_collector', array('template' => 'foo', 'id' => 'my_collector'));
$container = $this->createContainerMock($services);
// fake the getDefinition() to return a Profiler definition
$container->expects($this->atLeastOnce())
->method('getDefinition');
// assert that the data_collector.templates parameter should be set
$container->expects($this->once())
->method('setParameter')
->with('data_collector.templates', array('my_collector_service' => array('my_collector', 'foo')));
$profilerPass = new ProfilerPass(); $profilerPass = new ProfilerPass();
$profilerPass->process($container); $profilerPass->process($container);
$this->assertSame(array('my_collector_service' => array('my_collector', 'foo')), $container->getParameter('data_collector.templates'));
// grab the method calls off of the "profiler" definition // grab the method calls off of the "profiler" definition
$methodCalls = $this->profilerDefinition->getMethodCalls(); $methodCalls = $profilerDefinition->getMethodCalls();
$this->assertCount(1, $methodCalls); $this->assertCount(1, $methodCalls);
$this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call $this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call
} }
private function createContainerMock($services)
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'setParameter'))->getMock();
$container->expects($this->any())
->method('hasDefinition')
->with($this->equalTo('profiler'))
->will($this->returnValue(true));
$container->expects($this->any())
->method('getDefinition')
->will($this->returnValue($this->profilerDefinition));
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
return $container;
}
} }

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
@ -22,48 +23,30 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
*/ */
class SerializerPassTest extends TestCase class SerializerPassTest extends TestCase
{ {
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must tag at least one service as "serializer.normalizer" to use the Serializer service
*/
public function testThrowExceptionWhenNoNormalizers() public function testThrowExceptionWhenNoNormalizers()
{ {
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock(); $container = new ContainerBuilder();
$container->register('serializer');
$container->expects($this->once())
->method('hasDefinition')
->with('serializer')
->will($this->returnValue(true));
$container->expects($this->once())
->method('findTaggedServiceIds')
->with('serializer.normalizer')
->will($this->returnValue(array()));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
$serializerPass = new SerializerPass(); $serializerPass = new SerializerPass();
$serializerPass->process($container); $serializerPass->process($container);
} }
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must tag at least one service as "serializer.encoder" to use the Serializer service
*/
public function testThrowExceptionWhenNoEncoders() public function testThrowExceptionWhenNoEncoders()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container = new ContainerBuilder();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock(); $container->register('serializer')
->addArgument(array())
$container->expects($this->once()) ->addArgument(array());
->method('hasDefinition') $container->register('normalizer')->addTag('serializer.normalizer');
->with('serializer')
->will($this->returnValue(true));
$container->expects($this->any())
->method('findTaggedServiceIds')
->will($this->onConsecutiveCalls(
array('n' => array('serializer.normalizer')),
array()
));
$container->expects($this->once())
->method('getDefinition')
->will($this->returnValue($definition));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
$serializerPass = new SerializerPass(); $serializerPass = new SerializerPass();
$serializerPass->process($container); $serializerPass->process($container);
@ -71,34 +54,18 @@ class SerializerPassTest extends TestCase
public function testServicesAreOrderedAccordingToPriority() public function testServicesAreOrderedAccordingToPriority()
{ {
$services = array( $container = new ContainerBuilder();
'n3' => array(array()), $serializerDefinition = $container->register('serializer')
'n1' => array(array('priority' => 200)), ->addArgument(array())
'n2' => array(array('priority' => 100)), ->addArgument(array());
); $container->register('normalizer3')->addTag('serializer.normalizer');
$container->register('normalizer1')->addTag('serializer.normalizer', array('priority' => 200));
$expected = array( $container->register('normalizer2')->addTag('serializer.normalizer', array('priority' => 100));
new Reference('n1'), $container->register('encoder')->addTag('serializer.encoder');
new Reference('n2'),
new Reference('n3'),
);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$serializerPass = new SerializerPass(); $serializerPass = new SerializerPass();
$serializerPass->process($container);
$method = new \ReflectionMethod( $this->assertEquals(array(new Reference('normalizer1'), new Reference('normalizer2'), new Reference('normalizer3')), $serializerDefinition->getArgument(0));
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass',
'findAndSortTaggedServices'
);
$method->setAccessible(true);
$actual = $method->invoke($serializerPass, 'tag', $container);
$this->assertEquals($expected, $actual);
} }
} }

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
@ -19,28 +20,22 @@ class TranslatorPassTest extends TestCase
{ {
public function testValidCollector() public function testValidCollector()
{ {
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $container = new ContainerBuilder();
$definition->expects($this->at(0)) $container->register('translator.default')
->method('addMethodCall') ->setArguments(array(null, null, array()));
->with('addLoader', array('xliff', new Reference('xliff'))); $translationLoaderDefinition = $container->register('translation.loader');
$definition->expects($this->at(1)) $container->register('xliff')
->method('addMethodCall') ->addTag('translation.loader', array('alias' => 'xliff', 'legacy-alias' => 'xlf'));
->with('addLoader', array('xlf', new Reference('xliff')));
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'findDefinition'))->getMock();
$container->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getDefinition')
->will($this->returnValue($definition));
$container->expects($this->once())
->method('findTaggedServiceIds')
->will($this->returnValue(array('xliff' => array(array('alias' => 'xliff', 'legacy-alias' => 'xlf')))));
$container->expects($this->once())
->method('findDefinition')
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock()));
$pass = new TranslatorPass(); $pass = new TranslatorPass();
$pass->process($container); $pass->process($container);
$this->assertEquals(
array(
array('addLoader', array('xliff', new Reference('xliff'))),
array('addLoader', array('xlf', new Reference('xliff'))),
),
$translationLoaderDefinition->getMethodCalls()
);
} }
} }

View File

@ -14,7 +14,9 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Validator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory; use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint; use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ConstraintValidator;
class ConstraintValidatorFactoryTest extends TestCase class ConstraintValidatorFactoryTest extends TestCase
{ {
@ -42,26 +44,13 @@ class ConstraintValidatorFactoryTest extends TestCase
public function testGetInstanceReturnsService() public function testGetInstanceReturnsService()
{ {
$service = 'validator_constraint_service'; $validator = new DummyConstraintValidator();
$alias = 'validator_constraint_alias';
$validator = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator');
// mock ContainerBuilder b/c it implements TaggedContainerInterface $container = new Container();
$container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')->setMethods(array('get'))->getMock(); $container->set('validator_constraint_service', $validator);
$container
->expects($this->once())
->method('get')
->with($service)
->will($this->returnValue($validator));
$constraint = $this->getMockBuilder('Symfony\\Component\\Validator\\Constraint')->getMock();
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue($alias));
$factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service')); $factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service'));
$this->assertSame($validator, $factory->getInstance($constraint)); $this->assertSame($validator, $factory->getInstance(new ConstraintStub()));
} }
/** /**
@ -79,3 +68,18 @@ class ConstraintValidatorFactoryTest extends TestCase
$factory->getInstance($constraint); $factory->getInstance($constraint);
} }
} }
class ConstraintStub extends Constraint
{
public function validatedBy()
{
return 'validator_constraint_alias';
}
}
class DummyConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
}
}

View File

@ -12,13 +12,14 @@
namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Definition;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass; use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;
class TwigLoaderPassTest extends TestCase class TwigLoaderPassTest extends TestCase
{ {
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject * @var ContainerBuilder
*/ */
private $builder; private $builder;
/** /**
@ -32,62 +33,33 @@ class TwigLoaderPassTest extends TestCase
protected function setUp() protected function setUp()
{ {
$this->builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'setAlias', 'getDefinition'))->getMock(); $this->builder = new ContainerBuilder();
$this->builder->register('twig');
$this->chainLoader = new Definition('loader'); $this->chainLoader = new Definition('loader');
$this->pass = new TwigLoaderPass(); $this->pass = new TwigLoaderPass();
} }
public function testMapperPassWithOneTaggedLoaders() public function testMapperPassWithOneTaggedLoader()
{ {
$serviceIds = array( $this->builder->register('test_loader_1')
'test_loader_1' => array( ->addTag('twig.loader');
array(),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'test_loader_1');
$this->pass->process($this->builder); $this->pass->process($this->builder);
$this->assertSame('test_loader_1', (string) $this->builder->getAlias('twig.loader'));
} }
public function testMapperPassWithTwoTaggedLoaders() public function testMapperPassWithTwoTaggedLoaders()
{ {
$serviceIds = array( $this->builder->setDefinition('twig.loader.chain', $this->chainLoader);
'test_loader_1' => array( $this->builder->register('test_loader_1')
array(), ->addTag('twig.loader');
), $this->builder->register('test_loader_2')
'test_loader_2' => array( ->addTag('twig.loader');
array(),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('getDefinition')
->with('twig.loader.chain')
->will($this->returnValue($this->chainLoader));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'twig.loader.chain');
$this->pass->process($this->builder); $this->pass->process($this->builder);
$this->assertSame('twig.loader.chain', (string) $this->builder->getAlias('twig.loader'));
$calls = $this->chainLoader->getMethodCalls(); $calls = $this->chainLoader->getMethodCalls();
$this->assertCount(2, $calls); $this->assertCount(2, $calls);
$this->assertEquals('addLoader', $calls[0][0]); $this->assertEquals('addLoader', $calls[0][0]);
@ -98,32 +70,15 @@ class TwigLoaderPassTest extends TestCase
public function testMapperPassWithTwoTaggedLoadersWithPriority() public function testMapperPassWithTwoTaggedLoadersWithPriority()
{ {
$serviceIds = array( $this->builder->setDefinition('twig.loader.chain', $this->chainLoader);
'test_loader_1' => array( $this->builder->register('test_loader_1')
array('priority' => 100), ->addTag('twig.loader', array('priority' => 100));
), $this->builder->register('test_loader_2')
'test_loader_2' => array( ->addTag('twig.loader', array('priority' => 200));
array('priority' => 200),
),
);
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue($serviceIds));
$this->builder->expects($this->once())
->method('getDefinition')
->with('twig.loader.chain')
->will($this->returnValue($this->chainLoader));
$this->builder->expects($this->once())
->method('setAlias')
->with('twig.loader', 'twig.loader.chain');
$this->pass->process($this->builder); $this->pass->process($this->builder);
$this->assertSame('twig.loader.chain', (string) $this->builder->getAlias('twig.loader'));
$calls = $this->chainLoader->getMethodCalls(); $calls = $this->chainLoader->getMethodCalls();
$this->assertCount(2, $calls); $this->assertCount(2, $calls);
$this->assertEquals('addLoader', $calls[0][0]); $this->assertEquals('addLoader', $calls[0][0]);
@ -137,15 +92,6 @@ class TwigLoaderPassTest extends TestCase
*/ */
public function testMapperPassWithZeroTaggedLoaders() public function testMapperPassWithZeroTaggedLoaders()
{ {
$this->builder->expects($this->once())
->method('hasDefinition')
->with('twig')
->will($this->returnValue(true));
$this->builder->expects($this->once())
->method('findTaggedServiceIds')
->with('twig.loader')
->will($this->returnValue(array()));
$this->pass->process($this->builder); $this->pass->process($this->builder);
} }
} }

View File

@ -12,7 +12,10 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler; namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ExtensionCompilerPass; use Symfony\Component\DependencyInjection\Compiler\ExtensionCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
/** /**
* @author Wouter J <wouter@wouterj.nl> * @author Wouter J <wouter@wouterj.nl>
@ -24,33 +27,52 @@ class ExtensionCompilerPassTest extends TestCase
protected function setUp() protected function setUp()
{ {
$this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock(); $this->container = new ContainerBuilder();
$this->pass = new ExtensionCompilerPass(); $this->pass = new ExtensionCompilerPass();
} }
public function testProcess() public function testProcess()
{ {
$extension1 = $this->createExtensionMock(true); $extension1 = new CompilerPassExtension('extension1');
$extension1->expects($this->once())->method('process'); $extension2 = new DummyExtension('extension2');
$extension2 = $this->createExtensionMock(false); $extension3 = new DummyExtension('extension3');
$extension3 = $this->createExtensionMock(false); $extension4 = new CompilerPassExtension('extension4');
$extension4 = $this->createExtensionMock(true);
$extension4->expects($this->once())->method('process');
$this->container->expects($this->any()) $this->container->registerExtension($extension1);
->method('getExtensions') $this->container->registerExtension($extension2);
->will($this->returnValue(array($extension1, $extension2, $extension3, $extension4))) $this->container->registerExtension($extension3);
; $this->container->registerExtension($extension4);
$this->pass->process($this->container); $this->pass->process($this->container);
}
private function createExtensionMock($hasInlineCompile) $this->assertCount(2, $this->container->getDefinitions());
{
return $this->getMockBuilder('Symfony\Component\DependencyInjection\\'.(
$hasInlineCompile
? 'Compiler\CompilerPassInterface'
: 'Extension\ExtensionInterface'
))->getMock();
} }
} }
class DummyExtension extends Extension
{
private $alias;
public function __construct($alias)
{
$this->alias = $alias;
}
public function getAlias()
{
return $this->alias;
}
public function load(array $configs, ContainerBuilder $container)
{
}
public function process(ContainerBuilder $container)
{
$container->register($this->alias);
}
}
class CompilerPassExtension extends DummyExtension implements CompilerPassInterface
{
}

View File

@ -12,6 +12,8 @@
namespace Symfony\Component\DependencyInjection\Tests\Extension; namespace Symfony\Component\DependencyInjection\Tests\Extension;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
class ExtensionTest extends TestCase class ExtensionTest extends TestCase
{ {
@ -20,36 +22,8 @@ class ExtensionTest extends TestCase
*/ */
public function testIsConfigEnabledReturnsTheResolvedValue($enabled) public function testIsConfigEnabledReturnsTheResolvedValue($enabled)
{ {
$pb = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBag') $extension = new EnableableExtension();
->setMethods(array('resolveValue')) $this->assertSame($enabled, $extension->isConfigEnabled(new ContainerBuilder(), array('enabled' => $enabled)));
->getMock()
;
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('getParameterBag'))
->getMock()
;
$pb->expects($this->once())
->method('resolveValue')
->with($this->equalTo($enabled))
->will($this->returnValue($enabled))
;
$container->expects($this->once())
->method('getParameterBag')
->will($this->returnValue($pb))
;
$extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
->setMethods(array())
->getMockForAbstractClass()
;
$r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled');
$r->setAccessible(true);
$r->invoke($extension, $container, array('enabled' => $enabled));
} }
public function getResolvedEnabledFixtures() public function getResolvedEnabledFixtures()
@ -66,18 +40,20 @@ class ExtensionTest extends TestCase
*/ */
public function testIsConfigEnabledOnNonEnableableConfig() public function testIsConfigEnabledOnNonEnableableConfig()
{ {
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') $extension = new EnableableExtension();
->getMock()
;
$extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension') $extension->isConfigEnabled(new ContainerBuilder(), array());
->setMethods(array()) }
->getMockForAbstractClass() }
;
class EnableableExtension extends Extension
$r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled'); {
$r->setAccessible(true); public function load(array $configs, ContainerBuilder $container)
{
$r->invoke($extension, $container, array()); }
public function isConfigEnabled(ContainerBuilder $container, array $config)
{
return parent::isConfigEnabled($container, $config);
} }
} }

View File

@ -25,32 +25,10 @@ class RegisterListenersPassTest extends TestCase
*/ */
public function testEventSubscriberWithoutInterface() public function testEventSubscriberWithoutInterface()
{ {
// one service, not implementing any interface $builder = new ContainerBuilder();
$services = array( $builder->register('event_dispatcher');
'my_event_subscriber' => array(0 => array()), $builder->register('my_event_subscriber', 'stdClass')
); ->addTag('kernel.event_subscriber');
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('isPublic')
->will($this->returnValue(true));
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('stdClass'));
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.event_listener here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->onConsecutiveCalls(array(), $services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->returnValue($definition));
$registerListenersPass = new RegisterListenersPass(); $registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($builder); $registerListenersPass->process($builder);
@ -62,34 +40,15 @@ class RegisterListenersPassTest extends TestCase
'my_event_subscriber' => array(0 => array()), 'my_event_subscriber' => array(0 => array()),
); );
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $builder = new ContainerBuilder();
$definition->expects($this->atLeastOnce()) $eventDispatcherDefinition = $builder->register('event_dispatcher');
->method('isPublic') $builder->register('my_event_subscriber', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService')
->will($this->returnValue(true)); ->addTag('kernel.event_subscriber');
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService'));
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition', 'findDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.event_listener here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->onConsecutiveCalls(array(), $services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->returnValue($definition));
$builder->expects($this->atLeastOnce())
->method('findDefinition')
->will($this->returnValue($definition));
$registerListenersPass = new RegisterListenersPass(); $registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($builder); $registerListenersPass->process($builder);
$this->assertEquals(array(array('addSubscriberService', array('my_event_subscriber', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService'))), $eventDispatcherDefinition->getMethodCalls());
} }
/** /**

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass; use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
@ -24,118 +25,47 @@ class FragmentRendererPassTest extends TestCase
*/ */
public function testLegacyFragmentRedererWithoutAlias() public function testLegacyFragmentRedererWithoutAlias()
{ {
// no alias $builder = new ContainerBuilder();
$services = array( $fragmentHandlerDefinition = $builder->register('fragment.handler');
'my_content_renderer' => array(array()), $builder->register('my_content_renderer', 'Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
); ->addTag('kernel.fragment_renderer');
$renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$renderer
->expects($this->once())
->method('addMethodCall')
->with('addRenderer', array(new Reference('my_content_renderer')))
;
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
$definition
->expects($this->once())
->method('isPublic')
->will($this->returnValue(true))
;
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.fragment_renderer here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->onConsecutiveCalls($renderer, $definition));
$pass = new FragmentRendererPass(); $pass = new FragmentRendererPass();
$pass->process($builder); $pass->process($builder);
$this->assertEquals(array(array('addRenderer', array(new Reference('my_content_renderer')))), $fragmentHandlerDefinition->getMethodCalls());
} }
/** /**
* Tests that content rendering not implementing FragmentRendererInterface * Tests that content rendering not implementing FragmentRendererInterface
* trigger an exception. * triggers an exception.
* *
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testContentRendererWithoutInterface() public function testContentRendererWithoutInterface()
{ {
// one service, not implementing any interface $builder = new ContainerBuilder();
$services = array( $fragmentHandlerDefinition = $builder->register('fragment.handler');
'my_content_renderer' => array(array('alias' => 'foo')), $builder->register('my_content_renderer', 'Symfony\Component\DependencyInjection\Definition')
); ->addTag('kernel.fragment_renderer', array('alias' => 'foo'));
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.fragment_renderer here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->returnValue($definition));
$pass = new FragmentRendererPass(); $pass = new FragmentRendererPass();
$pass->process($builder); $pass->process($builder);
$this->assertEquals(array(array('addRendererService', array('foo', 'my_content_renderer'))), $fragmentHandlerDefinition->getMethodCalls());
} }
public function testValidContentRenderer() public function testValidContentRenderer()
{ {
$services = array( $builder = new ContainerBuilder();
'my_content_renderer' => array(array('alias' => 'foo')), $fragmentHandlerDefinition = $builder->register('fragment.handler');
); $builder->register('my_content_renderer', 'Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
->addTag('kernel.fragment_renderer', array('alias' => 'foo'));
$renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$renderer
->expects($this->once())
->method('addMethodCall')
->with('addRendererService', array('foo', 'my_content_renderer'))
;
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
$definition
->expects($this->once())
->method('isPublic')
->will($this->returnValue(true))
;
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$builder->expects($this->any())
->method('hasDefinition')
->will($this->returnValue(true));
// We don't test kernel.fragment_renderer here
$builder->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$builder->expects($this->atLeastOnce())
->method('getDefinition')
->will($this->onConsecutiveCalls($renderer, $definition));
$pass = new FragmentRendererPass(); $pass = new FragmentRendererPass();
$pass->process($builder); $pass->process($builder);
$this->assertEquals(array(array('addRendererService', array('foo', 'my_content_renderer'))), $fragmentHandlerDefinition->getMethodCalls());
} }
} }

View File

@ -12,44 +12,39 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection; namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass; use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
class MergeExtensionConfigurationPassTest extends TestCase class MergeExtensionConfigurationPassTest extends TestCase
{ {
public function testAutoloadMainExtension() public function testAutoloadMainExtension()
{ {
$container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')->setMethods(array('getExtensionConfig', 'loadFromExtension', 'getParameterBag', 'getDefinitions', 'getAliases', 'getExtensions'))->getMock(); $container = new ContainerBuilder();
$params = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBag')->getMock(); $container->registerExtension(new LoadedExtension());
$container->registerExtension(new NotLoadedExtension());
$container->loadFromExtension('loaded', array());
$container->expects($this->at(0)) $configPass = new MergeExtensionConfigurationPass(array('loaded', 'not_loaded'));
->method('getExtensionConfig')
->with('loaded')
->will($this->returnValue(array(array())));
$container->expects($this->at(1))
->method('getExtensionConfig')
->with('notloaded')
->will($this->returnValue(array()));
$container->expects($this->once())
->method('loadFromExtension')
->with('notloaded', array());
$container->expects($this->any())
->method('getParameterBag')
->will($this->returnValue($params));
$params->expects($this->any())
->method('all')
->will($this->returnValue(array()));
$container->expects($this->any())
->method('getDefinitions')
->will($this->returnValue(array()));
$container->expects($this->any())
->method('getAliases')
->will($this->returnValue(array()));
$container->expects($this->any())
->method('getExtensions')
->will($this->returnValue(array()));
$configPass = new MergeExtensionConfigurationPass(array('loaded', 'notloaded'));
$configPass->process($container); $configPass->process($container);
$this->assertTrue($container->hasDefinition('loaded.foo'));
$this->assertTrue($container->hasDefinition('not_loaded.bar'));
}
}
class LoadedExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container->register('loaded.foo');
}
}
class NotLoadedExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container->register('not_loaded.bar');
} }
} }