Example #1
0
// ->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');
$t->is($builder->getAliases(), array('bar' => 'foo', 'foobar' => 'foo'), '->getAliases() returns all service aliases');
$builder->register('bar', 'stdClass');
$t->is($builder->getAliases(), array('foobar' => 'foo'), '->getAliases() does not return aliased services that have been overridden');
 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->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 (\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');
     }
 }