Esempio n. 1
0
$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');
$builder->setService('foobar', 'stdClass');
$t->is($builder->getAliases(), array(), '->getAliases() does not return aliased services that have been overridden');
// ->createService() # file
$t->diag('->createService() # file');
$builder = new Builder();
$builder->register('foo1', 'FooClass')->setFile($fixturesPath . '/includes/foo.php');
$t->ok($builder->getService('foo1'), '->createService() requires the file defined by the service definition');
$builder->register('foo2', 'FooClass')->setFile($fixturesPath . '/includes/%file%.php');
$builder->setParameter('file', 'foo');
$t->ok($builder->getService('foo2'), '->createService() replaces parameters in the file provided by the service definition');
// ->createService() # class
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\Container;
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Parameter;
$container = new Builder();
$container->register('foo', 'FooClass')->setConstructor('getInstance')->setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->setFile(realpath(__DIR__ . '/../includes/foo.php'))->setShared(false)->addMethodCall('setBar', array('bar'))->addMethodCall('initialize')->setConfigurator('sc_configure');
$container->register('bar', 'FooClass')->setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->setShared(true)->setConfigurator(array(new Reference('foo.baz'), 'configure'));
$container->register('foo.baz', '%baz_class%')->setConstructor('getInstance')->setConfigurator(array('%baz_class%', 'configureStatic1'));
$container->register('foo_bar', '%foo_class%');
$container->setParameters(array('baz_class' => 'BazClass', 'foo_class' => 'FooClass', 'foo' => 'bar', 'foo_bar' => new Reference('foo_bar')));
$container->setAlias('alias_for_foo', 'foo');
$container->register('method_call1', 'FooClass')->addMethodCall('setBar', array(new Reference('foo')))->addMethodCall('setBar', array(new Reference('foo', Container::NULL_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foo', Container::IGNORE_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foobaz', Container::IGNORE_ON_INVALID_REFERENCE)));
return $container;
Esempio n. 3
0
 /**
  * @covers Symfony\Components\DependencyInjection\Builder::getAliases
  */
 public function testGetAliases()
 {
     $builder = new Builder();
     $builder->setAlias('bar', 'foo');
     $builder->setAlias('foobar', 'foo');
     $this->assertEquals(array('bar' => 'foo', 'foobar' => 'foo'), $builder->getAliases(), '->getAliases() returns all service aliases');
     $builder->register('bar', 'stdClass');
     $this->assertEquals(array('foobar' => 'foo'), $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
     $builder->set('foobar', 'stdClass');
     $this->assertEquals(array(), $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
 }