You are here

public function ContainerBuilderTest::testDefinitions 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::testDefinitions()

@covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinitions @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinitions @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinition @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinition

File

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

Class

ContainerBuilderTest

Namespace

Symfony\Component\DependencyInjection\Tests

Code

public function testDefinitions() {
  $builder = new ContainerBuilder();
  $definitions = array(
    'foo' => new Definition('Bar\\FooClass'),
    'bar' => new Definition('BarClass'),
  );
  $builder
    ->setDefinitions($definitions);
  $this
    ->assertEquals($definitions, $builder
    ->getDefinitions(), '->setDefinitions() sets the service definitions');
  $this
    ->assertTrue($builder
    ->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
  $this
    ->assertFalse($builder
    ->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
  $builder
    ->setDefinition('foobar', $foo = new Definition('FooBarClass'));
  $this
    ->assertEquals($foo, $builder
    ->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
  $this
    ->assertTrue($builder
    ->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fluid interface by returning the service reference');
  $builder
    ->addDefinitions($defs = array(
    'foobar' => new Definition('FooBarClass'),
  ));
  $this
    ->assertEquals(array_merge($definitions, $defs), $builder
    ->getDefinitions(), '->addDefinitions() adds the service definitions');
  try {
    $builder
      ->getDefinition('baz');
    $this
      ->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  } catch (\InvalidArgumentException $e) {
    $this
      ->assertEquals('The service definition "baz" does not exist.', $e
      ->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  }
}