You are here

public function ContainerBuilderTest::testMerge in Service Container 7.2

Same name and namespace in other branches
  1. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php \Symfony\Component\DependencyInjection\Tests\ContainerBuilderTest::testMerge()

@covers Symfony\Component\DependencyInjection\ContainerBuilder::merge

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php, line 452

Class

ContainerBuilderTest

Namespace

Symfony\Component\DependencyInjection\Tests

Code

public function testMerge() {
  $container = new ContainerBuilder(new ParameterBag(array(
    'bar' => 'foo',
  )));
  $container
    ->setResourceTracking(false);
  $config = new ContainerBuilder(new ParameterBag(array(
    'foo' => 'bar',
  )));
  $container
    ->merge($config);
  $this
    ->assertEquals(array(
    'bar' => 'foo',
    'foo' => 'bar',
  ), $container
    ->getParameterBag()
    ->all(), '->merge() merges current parameters with the loaded ones');
  $container = new ContainerBuilder(new ParameterBag(array(
    'bar' => 'foo',
  )));
  $container
    ->setResourceTracking(false);
  $config = new ContainerBuilder(new ParameterBag(array(
    'foo' => '%bar%',
  )));
  $container
    ->merge($config);
  $container
    ->compile();
  $this
    ->assertEquals(array(
    'bar' => 'foo',
    'foo' => 'foo',
  ), $container
    ->getParameterBag()
    ->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  $container = new ContainerBuilder(new ParameterBag(array(
    'bar' => 'foo',
  )));
  $container
    ->setResourceTracking(false);
  $config = new ContainerBuilder(new ParameterBag(array(
    'foo' => '%bar%',
    'baz' => '%foo%',
  )));
  $container
    ->merge($config);
  $container
    ->compile();
  $this
    ->assertEquals(array(
    'bar' => 'foo',
    'foo' => 'foo',
    'baz' => 'foo',
  ), $container
    ->getParameterBag()
    ->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  $container = new ContainerBuilder();
  $container
    ->setResourceTracking(false);
  $container
    ->register('foo', 'Bar\\FooClass');
  $container
    ->register('bar', 'BarClass');
  $config = new ContainerBuilder();
  $config
    ->setDefinition('baz', new Definition('BazClass'));
  $config
    ->setAlias('alias_for_foo', 'foo');
  $container
    ->merge($config);
  $this
    ->assertEquals(array(
    'foo',
    'bar',
    'baz',
  ), array_keys($container
    ->getDefinitions()), '->merge() merges definitions already defined ones');
  $aliases = $container
    ->getAliases();
  $this
    ->assertTrue(isset($aliases['alias_for_foo']));
  $this
    ->assertEquals('foo', (string) $aliases['alias_for_foo']);
  $container = new ContainerBuilder();
  $container
    ->setResourceTracking(false);
  $container
    ->register('foo', 'Bar\\FooClass');
  $config
    ->setDefinition('foo', new Definition('BazClass'));
  $container
    ->merge($config);
  $this
    ->assertEquals('BazClass', $container
    ->getDefinition('foo')
    ->getClass(), '->merge() overrides already defined services');
}