do not mock the container builder in tests

This commit is contained in:
Christian Flothmann 2018-02-07 21:12:42 +01:00
parent edd507af78
commit 777acfbac9
19 changed files with 307 additions and 512 deletions

View File

@ -44,27 +44,17 @@ class CachePoolPrunerPassTest extends TestCase
public function testCompilePassIsIgnoredIfCommandDoesNotExist()
{
$container = $this
->getMockBuilder(ContainerBuilder::class)
->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds'))
->getMock();
$container = new ContainerBuilder();
$container
->expects($this->atLeastOnce())
->method('hasDefinition')
->with(CachePoolPruneCommand::class)
->will($this->returnValue(false));
$container
->expects($this->never())
->method('getDefinition');
$container
->expects($this->never())
->method('findTaggedServiceIds');
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$pass = new CachePoolPrunerPass();
$pass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
/**

View File

@ -44,13 +44,16 @@ class ConfigCachePassTest extends TestCase
public function testThatCheckersCanBeMissing()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container = new ContainerBuilder();
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue(array()));
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$pass = new ConfigCachePass();
$pass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

@ -13,96 +13,69 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class LoggingTranslatorPassTest extends TestCase
{
public function testProcess()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock();
$parameterBag = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface')->getMock();
$container->expects($this->exactly(2))
->method('hasAlias')
->will($this->returnValue(true));
$container->expects($this->once())
->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('Symfony\Bundle\FrameworkBundle\Translation\Translator'));
$parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnValue("Symfony\Bundle\FrameworkBundle\Translation\Translator"));
$container->expects($this->once())
->method('getParameterBag')
->will($this->returnValue($parameterBag));
$container->expects($this->once())
->method('getReflectionClass')
->with('Symfony\Bundle\FrameworkBundle\Translation\Translator')
->will($this->returnValue(new \ReflectionClass('Symfony\Bundle\FrameworkBundle\Translation\Translator')));
$definition->expects($this->once())
->method('getTag')
->with('container.service_subscriber')
->willReturn(array(array('id' => 'translator'), array('id' => 'foo')));
$definition->expects($this->once())
->method('clearTag')
->with('container.service_subscriber');
$definition->expects($this->any())
->method('addTag')
->withConsecutive(
array('container.service_subscriber', array('id' => 'foo')),
array('container.service_subscriber', array('key' => 'translator', 'id' => 'translator.logging.inner'))
);
$container = new ContainerBuilder();
$container->setParameter('translator.logging', true);
$container->setParameter('translator.class', 'Symfony\Component\Translation\Translator');
$container->register('monolog.logger');
$container->setAlias('logger', 'monolog.logger');
$container->register('translator.default', '%translator.class%');
$container->register('translator.logging', '%translator.class%');
$container->setAlias('translator', 'translator.default');
$translationWarmerDefinition = $container->register('translation.warmer')
->addArgument(new Reference('translator'))
->addTag('container.service_subscriber', array('id' => 'translator'))
->addTag('container.service_subscriber', array('id' => 'foo'));
$pass = new LoggingTranslatorPass();
$pass->process($container);
$this->assertEquals(
array('container.service_subscriber' => array(
array('id' => 'foo'),
array('key' => 'translator', 'id' => 'translator.logging.inner'),
)),
$translationWarmerDefinition->getTags()
);
}
public function testThatCompilerPassIsIgnoredIfThereIsNotLoggerDefinition()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock();
$container->expects($this->once())
->method('hasAlias')
->will($this->returnValue(false));
$container = new ContainerBuilder();
$container->register('identity_translator');
$container->setAlias('translator', 'identity_translator');
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$pass = new LoggingTranslatorPass();
$pass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
public function testThatCompilerPassIsIgnoredIfThereIsNotTranslatorDefinition()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->getMock();
$container->expects($this->at(0))
->method('hasAlias')
->will($this->returnValue(true));
$container = new ContainerBuilder();
$container->register('monolog.logger');
$container->setAlias('logger', 'monolog.logger');
$container->expects($this->at(0))
->method('hasAlias')
->will($this->returnValue(false));
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$pass = new LoggingTranslatorPass();
$pass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

@ -57,24 +57,16 @@ class PropertyInfoPassTest extends TestCase
public function testReturningEmptyArrayWhenNoService()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container
->expects($this->any())
->method('findTaggedServiceIds')
->will($this->returnValue(array()))
;
$container = new ContainerBuilder();
$propertyInfoExtractorDefinition = $container->register('property_info')
->setArguments(array(array(), array(), array(), array()));
$propertyInfoPass = new PropertyInfoPass();
$propertyInfoPass->process($container);
$method = new \ReflectionMethod(
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass',
'findAndSortTaggedServices'
);
$method->setAccessible(true);
$actual = $method->invoke($propertyInfoPass, 'tag', $container);
$this->assertEquals(array(), $actual);
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(0));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(1));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(2));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(3));
}
}

View File

@ -25,48 +25,30 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
*/
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()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock();
$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');
$container = new ContainerBuilder();
$container->register('serializer');
$serializerPass = new SerializerPass();
$serializerPass->process($container);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must tag at least one service as "serializer.encoder" to use the "serializer" service
*/
public function testThrowExceptionWhenNoEncoders()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$container->expects($this->once())
->method('hasDefinition')
->with('serializer')
->will($this->returnValue(true));
$container->expects($this->any())
->method('findTaggedServiceIds')
->will($this->onConsecutiveCalls(
array('n' => array('serializer.normalizer')),
array()
));
$container->expects($this->any())
->method('getDefinition')
->will($this->returnValue($definition));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('RuntimeException');
$container = new ContainerBuilder();
$container->register('serializer')
->addArgument(array())
->addArgument(array());
$container->register('normalizer')->addTag('serializer.normalizer');
$serializerPass = new SerializerPass();
$serializerPass->process($container);

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class UnusedTagsPassTest extends TestCase
{
@ -20,24 +21,14 @@ class UnusedTagsPassTest extends TestCase
{
$pass = new UnusedTagsPass();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'findUnusedTags', 'findTags', 'log'))->getMock();
$container->expects($this->once())
->method('log')
->with($pass, 'Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?');
$container->expects($this->once())
->method('findTags')
->will($this->returnValue(array('kenrel.event_subscriber')));
$container->expects($this->once())
->method('findUnusedTags')
->will($this->returnValue(array('kenrel.event_subscriber', 'form.type')));
$container->expects($this->once())
->method('findTaggedServiceIds')
->with('kenrel.event_subscriber')
->will($this->returnValue(array(
'foo' => array(),
'bar' => array(),
)));
$container = new ContainerBuilder();
$container->register('foo')
->addTag('kenrel.event_subscriber');
$container->register('bar')
->addTag('kenrel.event_subscriber');
$pass->process($container);
$this->assertSame(array(sprintf('%s: Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?', UnusedTagsPass::class)), $container->getCompiler()->getLog());
}
}

View File

@ -14,7 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Validator;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ConstraintValidator;
@ -26,16 +25,8 @@ class ConstraintValidatorFactoryTest extends TestCase
{
public function testGetInstanceCreatesValidator()
{
$class = get_class($this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator'));
$constraint = $this->getMockBuilder('Symfony\\Component\\Validator\\Constraint')->getMock();
$constraint
->expects($this->exactly(2))
->method('validatedBy')
->will($this->returnValue($class));
$factory = new ConstraintValidatorFactory(new Container());
$this->assertInstanceOf($class, $factory->getInstance($constraint));
$this->assertInstanceOf(DummyConstraintValidator::class, $factory->getInstance(new DummyConstraint()));
}
public function testGetInstanceReturnsExistingValidator()
@ -48,54 +39,24 @@ class ConstraintValidatorFactoryTest extends TestCase
public function testGetInstanceReturnsService()
{
$service = 'validator_constraint_service';
$validator = $this->getMockForAbstractClass(ConstraintValidator::class);
// mock ContainerBuilder b/c it implements TaggedContainerInterface
$container = $this->getMockBuilder(ContainerBuilder::class)->setMethods(array('get', 'has'))->getMock();
$container
->expects($this->once())
->method('get')
->with($service)
->willReturn($validator);
$container
->expects($this->once())
->method('has')
->with($service)
->willReturn(true);
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
$constraint
->expects($this->exactly(2))
->method('validatedBy')
->will($this->returnValue($service));
$validator = new DummyConstraintValidator();
$container = new Container();
$container->set(DummyConstraintValidator::class, $validator);
$factory = new ConstraintValidatorFactory($container);
$this->assertSame($validator, $factory->getInstance($constraint));
$this->assertSame($validator, $factory->getInstance(new DummyConstraint()));
}
public function testGetInstanceReturnsServiceWithAlias()
{
$service = 'validator_constraint_service';
$alias = 'validator_constraint_alias';
$validator = $this->getMockForAbstractClass('Symfony\\Component\\Validator\\ConstraintValidator');
$validator = new DummyConstraintValidator();
// mock ContainerBuilder b/c it implements TaggedContainerInterface
$container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')->setMethods(array('get'))->getMock();
$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));
$container = new Container();
$container->set('validator_constraint_service', $validator);
$factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service'));
$this->assertSame($validator, $factory->getInstance($constraint));
$this->assertSame($validator, $factory->getInstance(new ConstraintAliasStub()));
}
/**
@ -113,3 +74,26 @@ class ConstraintValidatorFactoryTest extends TestCase
$factory->getInstance($constraint);
}
}
class ConstraintAliasStub extends Constraint
{
public function validatedBy()
{
return 'validator_constraint_alias';
}
}
class DummyConstraint extends Constraint
{
public function validatedBy()
{
return DummyConstraintValidator::class;
}
}
class DummyConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
}
}

View File

@ -12,7 +12,6 @@
namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass;

View File

@ -44,13 +44,16 @@ class ConfigCachePassTest extends TestCase
public function testThatCheckersCanBeMissing()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container = new ContainerBuilder();
$container->expects($this->atLeastOnce())
->method('findTaggedServiceIds')
->will($this->returnValue(array()));
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$pass = new ConfigCachePass();
$pass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

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

@ -27,29 +27,10 @@ class RegisterListenersPassTest extends TestCase
*/
public function testEventSubscriberWithoutInterface()
{
// one service, not implementing any interface
$services = array(
'my_event_subscriber' => array(0 => array()),
);
$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.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 = new ContainerBuilder();
$builder->register('event_dispatcher');
$builder->register('my_event_subscriber', 'stdClass')
->addTag('kernel.event_subscriber');
$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($builder);
@ -61,31 +42,25 @@ class RegisterListenersPassTest extends TestCase
'my_event_subscriber' => array(0 => array()),
);
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$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));
$builder = new ContainerBuilder();
$eventDispatcherDefinition = $builder->register('event_dispatcher');
$builder->register('my_event_subscriber', 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService')
->addTag('kernel.event_subscriber');
$registerListenersPass = new RegisterListenersPass();
$registerListenersPass->process($builder);
$expectedCalls = array(
array(
'addListener',
array(
'event',
array(new ServiceClosureArgument(new Reference('my_event_subscriber')), 'onEvent'),
0,
),
),
);
$this->assertEquals($expectedCalls, $eventDispatcherDefinition->getMethodCalls());
}
/**

View File

@ -12,8 +12,10 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
@ -22,73 +24,37 @@ class FragmentRendererPassTest extends TestCase
{
/**
* Tests that content rendering not implementing FragmentRendererInterface
* trigger an exception.
* triggers an exception.
*
* @expectedException \InvalidArgumentException
*/
public function testContentRendererWithoutInterface()
{
// one service, not implementing any interface
$services = array(
'my_content_renderer' => array(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));
$builder = new ContainerBuilder();
$fragmentHandlerDefinition = $builder->register('fragment.handler');
$builder->register('my_content_renderer', 'Symfony\Component\DependencyInjection\Definition')
->addTag('kernel.fragment_renderer', array('alias' => 'foo'));
$pass = new FragmentRendererPass();
$pass->process($builder);
$this->assertEquals(array(array('addRendererService', array('foo', 'my_content_renderer'))), $fragmentHandlerDefinition->getMethodCalls());
}
public function testValidContentRenderer()
{
$services = array(
'my_content_renderer' => array(array('alias' => 'foo')),
);
$renderer = new Definition('', array(null));
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())
->method('getClass')
->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
$builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition', 'getReflectionClass'))->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));
$builder->expects($this->atLeastOnce())
->method('getReflectionClass')
->with('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
->will($this->returnValue(new \ReflectionClass('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')));
$builder = new ContainerBuilder();
$fragmentHandlerDefinition = $builder->register('fragment.handler')
->addArgument(null);
$builder->register('my_content_renderer', 'Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService')
->addTag('kernel.fragment_renderer', array('alias' => 'foo'));
$pass = new FragmentRendererPass();
$pass->process($builder);
$this->assertInstanceOf(Reference::class, $renderer->getArgument(0));
$serviceLocatorDefinition = $builder->getDefinition((string) $fragmentHandlerDefinition->getArgument(0));
$this->assertSame(ServiceLocator::class, $serviceLocatorDefinition->getClass());
$this->assertEquals(array('foo' => new ServiceClosureArgument(new Reference('my_content_renderer'))), $serviceLocatorDefinition->getArgument(0));
}
}

View File

@ -54,24 +54,16 @@ class PropertyInfoPassTest extends TestCase
public function testReturningEmptyArrayWhenNoService()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container
->expects($this->any())
->method('findTaggedServiceIds')
->will($this->returnValue(array()))
;
$container = new ContainerBuilder();
$propertyInfoExtractorDefinition = $container->register('property_info')
->setArguments(array(array(), array(), array(), array()));
$propertyInfoPass = new PropertyInfoPass();
$propertyInfoPass->process($container);
$method = new \ReflectionMethod(
'Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass',
'findAndSortTaggedServices'
);
$method->setAccessible(true);
$actual = $method->invoke($propertyInfoPass, 'tag', $container);
$this->assertEquals(array(), $actual);
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(0));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(1));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(2));
$this->assertEquals(new IteratorArgument(array()), $propertyInfoExtractorDefinition->getArgument(3));
}
}

View File

@ -23,48 +23,30 @@ use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
*/
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()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock();
$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::class);
$container = new ContainerBuilder();
$container->register('serializer');
$serializerPass = new SerializerPass();
$serializerPass->process($container);
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage You must tag at least one service as "serializer.encoder" to use the "serializer" service
*/
public function testThrowExceptionWhenNoEncoders()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
$container->expects($this->once())
->method('hasDefinition')
->with('serializer')
->will($this->returnValue(true));
$container->expects($this->any())
->method('findTaggedServiceIds')
->will($this->onConsecutiveCalls(
array('n' => array('serializer.normalizer')),
array()
));
$container->expects($this->any())
->method('getDefinition')
->will($this->returnValue($definition));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(\RuntimeException::class);
$container = new ContainerBuilder();
$container->register('serializer')
->addArgument(array())
->addArgument(array());
$container->register('normalizer')->addTag('serializer.normalizer');
$serializerPass = new SerializerPass();
$serializerPass->process($container);

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
@ -19,48 +20,29 @@ class TranslationDumperPassTest extends TestCase
{
public function testProcess()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock();
$container->expects($this->once())
->method('hasDefinition')
->with('translation.writer')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getDefinition')
->with('translation.writer')
->will($this->returnValue($definition));
$valueTaggedServiceIdsFound = array(
'foo.id' => array(
array('alias' => 'bar.alias'),
),
);
$container->expects($this->once())
->method('findTaggedServiceIds')
->with('translation.dumper', true)
->will($this->returnValue($valueTaggedServiceIdsFound));
$definition->expects($this->once())->method('addMethodCall')->with('addDumper', array('bar.alias', new Reference('foo.id')));
$container = new ContainerBuilder();
$writerDefinition = $container->register('translation.writer');
$container->register('foo.id')
->addTag('translation.dumper', array('alias' => 'bar.alias'));
$translationDumperPass = new TranslationDumperPass();
$translationDumperPass->process($container);
$this->assertEquals(array(array('addDumper', array('bar.alias', new Reference('foo.id')))), $writerDefinition->getMethodCalls());
}
public function testProcessNoDefinitionFound()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock();
$container = new ContainerBuilder();
$container->expects($this->once())
->method('hasDefinition')
->with('translation.writer')
->will($this->returnValue(false));
$container->expects($this->never())->method('getDefinition');
$container->expects($this->never())->method('findTaggedServiceIds');
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$translationDumperPass = new TranslationDumperPass();
$translationDumperPass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
@ -19,49 +20,30 @@ class TranslationExtractorPassTest extends TestCase
{
public function testProcess()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock();
$container->expects($this->once())
->method('hasDefinition')
->with('translation.extractor')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getDefinition')
->with('translation.extractor')
->will($this->returnValue($definition));
$valueTaggedServiceIdsFound = array(
'foo.id' => array(
array('alias' => 'bar.alias'),
),
);
$container->expects($this->once())
->method('findTaggedServiceIds')
->with('translation.extractor', true)
->will($this->returnValue($valueTaggedServiceIdsFound));
$definition->expects($this->once())->method('addMethodCall')->with('addExtractor', array('bar.alias', new Reference('foo.id')));
$container = new ContainerBuilder();
$extractorDefinition = $container->register('translation.extractor');
$container->register('foo.id')
->addTag('translation.extractor', array('alias' => 'bar.alias'));
$translationDumperPass = new TranslationExtractorPass();
$translationDumperPass->process($container);
$this->assertEquals(array(array('addExtractor', array('bar.alias', new Reference('foo.id')))), $extractorDefinition->getMethodCalls());
}
public function testProcessNoDefinitionFound()
{
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock();
$container = new ContainerBuilder();
$container->expects($this->once())
->method('hasDefinition')
->with('translation.extractor')
->will($this->returnValue(false));
$container->expects($this->never())->method('getDefinition');
$container->expects($this->never())->method('findTaggedServiceIds');
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$translationDumperPass = new TranslationExtractorPass();
$translationDumperPass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
/**
@ -71,25 +53,10 @@ class TranslationExtractorPassTest extends TestCase
public function testProcessMissingAlias()
{
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock();
$container->expects($this->once())
->method('hasDefinition')
->with('translation.extractor')
->will($this->returnValue(true));
$container->expects($this->once())
->method('getDefinition')
->with('translation.extractor')
->will($this->returnValue($definition));
$valueTaggedServiceIdsFound = array(
'foo.id' => array(),
);
$container->expects($this->once())
->method('findTaggedServiceIds')
->with('translation.extractor', true)
->will($this->returnValue($valueTaggedServiceIdsFound));
$container = new ContainerBuilder();
$container->register('translation.extractor');
$container->register('foo.id')
->addTag('translation.extractor', array());
$definition->expects($this->never())->method('addMethodCall');

View File

@ -13,7 +13,6 @@ namespace Symfony\Component\Validator\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Blank as BlankConstraint;
use Symfony\Component\Validator\ConstraintValidator;
@ -23,16 +22,8 @@ class ContainerConstraintValidatorFactoryTest extends TestCase
{
public function testGetInstanceCreatesValidator()
{
$class = get_class($this->getMockForAbstractClass(ConstraintValidator::class));
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue($class));
$factory = new ContainerConstraintValidatorFactory(new Container());
$this->assertInstanceOf($class, $factory->getInstance($constraint));
$this->assertInstanceOf(DummyConstraintValidator::class, $factory->getInstance(new DummyConstraint()));
}
public function testGetInstanceReturnsExistingValidator()
@ -45,30 +36,13 @@ class ContainerConstraintValidatorFactoryTest extends TestCase
public function testGetInstanceReturnsService()
{
$service = 'validator_constraint_service';
$validator = $this->getMockForAbstractClass(ConstraintValidator::class);
// mock ContainerBuilder b/c it implements TaggedContainerInterface
$container = $this->getMockBuilder(ContainerBuilder::class)->setMethods(array('get', 'has'))->getMock();
$container
->expects($this->once())
->method('get')
->with($service)
->willReturn($validator);
$container
->expects($this->once())
->method('has')
->with($service)
->willReturn(true);
$constraint = $this->getMockBuilder(Constraint::class)->getMock();
$constraint
->expects($this->once())
->method('validatedBy')
->will($this->returnValue($service));
$validator = new DummyConstraintValidator();
$container = new Container();
$container->set(DummyConstraintValidator::class, $validator);
$factory = new ContainerConstraintValidatorFactory($container);
$this->assertSame($validator, $factory->getInstance($constraint));
$this->assertSame($validator, $factory->getInstance(new DummyConstraint()));
}
/**
@ -86,3 +60,18 @@ class ContainerConstraintValidatorFactoryTest extends TestCase
$factory->getInstance($constraint);
}
}
class DummyConstraint extends Constraint
{
public function validatedBy()
{
return DummyConstraintValidator::class;
}
}
class DummyConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
}
}

View File

@ -50,7 +50,7 @@ class AddConstraintValidatorsPassTest extends TestCase
public function testAbstractConstraintValidator()
{
$container = new ContainerBuilder();
$validatorFactory = $container->register('validator.validator_factory')
$container->register('validator.validator_factory')
->addArgument(array());
$container->register('my_abstract_constraint_validator')
@ -63,18 +63,16 @@ class AddConstraintValidatorsPassTest extends TestCase
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 = new ContainerBuilder();
$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');
$definitionsBefore = count($container->getDefinitions());
$aliasesBefore = count($container->getAliases());
$addConstraintValidatorsPass = new AddConstraintValidatorsPass();
$addConstraintValidatorsPass->process($container);
// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}

View File

@ -4,7 +4,8 @@ namespace Symfony\Component\Workflow\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Workflow\Definition as WorkflowDefinition;
use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass;
use Symfony\Component\Workflow\Transition;
@ -12,19 +13,20 @@ class ValidateWorkflowsPassTest extends TestCase
{
public function testProcess()
{
$container = $this->getMockBuilder(ContainerBuilder::class)->getMock();
$container
->expects($this->once())
->method('findTaggedServiceIds')
->with('workflow.definition')
->willReturn(array('definition1' => array('workflow.definition' => array('name' => 'wf1', 'type' => 'state_machine', 'marking_store' => 'foo'))));
$container
->expects($this->once())
->method('get')
->with('definition1')
->willReturn(new Definition(array('a', 'b', 'c'), array(new Transition('t1', 'a', 'b'), new Transition('t2', 'a', 'c'))));
$container = new ContainerBuilder();
$container->register('definition1', WorkflowDefinition::class)
->addArgument(array('a', 'b', 'c'))
->addArgument(array(
new Definition(Transition::class, array('t1', 'a', 'b')),
new Definition(Transition::class, array('t2', 'a', 'c')),
))
->addTag('workflow.definition', array('name' => 'wf1', 'type' => 'state_machine', 'marking_store' => 'foo'));
(new ValidateWorkflowsPass())->process($container);
$workflowDefinition = $container->get('definition1');
$this->assertSame(array('a' => 'a', 'b' => 'b', 'c' => 'c'), $workflowDefinition->getPlaces());
$this->assertEquals(array(new Transition('t1', 'a', 'b'), new Transition('t2', 'a', 'c')), $workflowDefinition->getTransitions());
}
}