You are here

public function ContainerBuilderTest::testAliases in Service Container 7

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

@covers Symfony\Component\DependencyInjection\ContainerBuilder::setAlias @covers Symfony\Component\DependencyInjection\ContainerBuilder::hasAlias @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAlias

File

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

Class

ContainerBuilderTest

Namespace

Symfony\Component\DependencyInjection\Tests

Code

public function testAliases() {
  $builder = new ContainerBuilder();
  $builder
    ->register('foo', 'stdClass');
  $builder
    ->setAlias('bar', 'foo');
  $this
    ->assertTrue($builder
    ->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
  $this
    ->assertFalse($builder
    ->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
  $this
    ->assertEquals('foo', (string) $builder
    ->getAlias('bar'), '->getAlias() returns the aliased service');
  $this
    ->assertTrue($builder
    ->has('bar'), '->setAlias() defines a new service');
  $this
    ->assertTrue($builder
    ->get('bar') === $builder
    ->get('foo'), '->setAlias() creates a service that is an alias to another one');
  try {
    $builder
      ->setAlias('foobar', 'foobar');
    $this
      ->fail('->setAlias() throws an InvalidArgumentException if the alias references itself');
  } catch (\InvalidArgumentException $e) {
    $this
      ->assertEquals('An alias can not reference itself, got a circular reference on "foobar".', $e
      ->getMessage(), '->setAlias() throws an InvalidArgumentException if the alias references itself');
  }
  try {
    $builder
      ->getAlias('foobar');
    $this
      ->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
  } catch (\InvalidArgumentException $e) {
    $this
      ->assertEquals('The service alias "foobar" does not exist.', $e
      ->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
  }
}