コード例 #1
0
try {
    $loader->load('foo');
    $t->fail('->load() throws a LogicException if no container is attached to the loader');
} catch (LogicException $e) {
    $t->pass('->load() throws a LogicException if no container is attached to the loader');
}
$loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo')));
$loader->load(array(array(), array('foo' => 'bar')));
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->load() merges current parameters with the loaded ones');
$loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo', 'foo' => 'baz')));
$loader->load(array(array(), array('foo' => 'bar')));
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->load() does not change the already defined parameters');
$loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo')));
$loader->load(array(array(), array('foo' => '%bar%')));
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->load() evaluates the values of the parameters towards already defined ones');
$loader->setServiceContainer($container = new sfServiceContainerBuilder(array('bar' => 'foo')));
$loader->load(array(array(), array('foo' => '%bar%', 'baz' => '%foo%')));
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->load() evaluates the values of the parameters towards already defined ones');
$loader->setServiceContainer($container = new sfServiceContainerBuilder());
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$loader->load(array(array('baz' => new sfServiceDefinition('BazClass'), 'alias_for_foo' => 'foo'), array()));
$t->is(array_keys($container->getServiceDefinitions()), array('foo', 'bar', 'baz'), '->load() merges definitions already defined ones');
$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->load() registers defined aliases');
$loader->setServiceContainer($container = new sfServiceContainerBuilder());
$container->register('foo', 'FooClass');
$loader->load(array(array('foo' => new sfServiceDefinition('BazClass')), array()));
$t->is($container->getServiceDefinition('foo')->getClass(), 'BazClass', '->load() overrides already defined services');
$loader->setServiceContainer($container = new sfServiceContainerBuilder());
$loader->load(array(array(), array('foo' => 'bar')), array(array(), array('bar' => 'foo')));
$t->is($container->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->load() accepts several resources as argument');
コード例 #2
0
$builder->register('foo', 'FooClass');
$t->ok($builder->hasServiceDefinition('foo'), '->register() registers a new service definition');
$t->ok($builder->getServiceDefinition('foo') instanceof sfServiceDefinition, '->register() returns the newly created sfServiceDefinition instance');
// ->setAlias()
$t->diag('->setAlias()');
$builder = new sfServiceContainerBuilder();
$builder->register('foo', 'stdClass');
$builder->setAlias('bar', 'foo');
$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');
// ->getAliases()
$t->diag('->getAliases()');
$builder = new sfServiceContainerBuilder();
$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');
// ->hasService()
$t->diag('->hasService()');
$builder = new sfServiceContainerBuilder();
$t->ok(!$builder->hasService('foo'), '->hasService() returns false if the service does not exist');
$builder->register('foo', 'FooClass');
$t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists');
$builder->setAlias('foobar', 'foo');
$t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists');
$builder->setService('bar', new stdClass());
$t->ok($builder->hasService('bar'), '->hasService() returns true if a service exists');
$builder->setAlias('foobar2', 'foo');