Ejemplo n.º 1
0
 /**
  * @covers Symfony\Components\DependencyInjection\Builder::setAlias
  * @covers Symfony\Components\DependencyInjection\Builder::hasAlias
  * @covers Symfony\Components\DependencyInjection\Builder::getAlias
  */
 public function testAliases()
 {
     $builder = new Builder();
     $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', $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->getAlias('foobar');
         $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->getAlias() throws an InvalidArgumentException if the alias does not exist');
         $this->assertEquals('The service alias "foobar" does not exist.', $e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
     }
 }
Ejemplo n.º 2
0
}
$builder->register('foobar', 'stdClass')->setShared(true);
$t->ok($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared');
// ->getServiceIds()
$t->diag('->getServiceIds()');
$builder = new Builder();
$builder->register('foo', 'stdClass');
$builder->bar = $bar = new stdClass();
$builder->register('bar', 'stdClass');
$t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids');
// ->setAlias() ->getAlias() ->hasAlias()
$t->diag('->setAlias() ->getAlias() ->hasAlias()');
$builder = new Builder();
$builder->register('foo', 'stdClass');
$builder->setAlias('bar', 'foo');
$t->ok($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
$t->ok(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
$t->is($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service');
$t->ok($builder->hasService('bar'), '->setAlias() defines a new service');
$t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
try {
    $builder->getAlias('foobar');
    $t->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
} catch (\InvalidArgumentException $e) {
    $t->pass('->getAlias() throws an InvalidArgumentException if the alias does not exist');
}
// ->getAliases()
$t->diag('->getAliases()');
$builder = new Builder();
$builder->setAlias('bar', 'foo');
$builder->setAlias('foobar', 'foo');
Ejemplo n.º 3
0
 public function testAliases()
 {
     $builder = new Builder();
     $builder->register('foo', 'stdClass');
     $builder->setAlias('bar', 'foo');
     $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
     $this->assertTrue(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
     $this->assertEquals($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service');
     $this->assertTrue($builder->hasService('bar'), '->setAlias() defines a new service');
     $this->assertTrue($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
     try {
         $builder->getAlias('foobar');
         $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
     } catch (\InvalidArgumentException $e) {
     }
 }