Ejemplo n.º 1
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new PhpDumper($container);
     $this->assertEquals($dumper->dump(), str_replace('%path%', self::$fixturesPath . '/includes', file_get_contents(self::$fixturesPath . '/php/services9.php')), '->dump() dumps services');
     $dumper = new PhpDumper($container = new Builder());
     $container->register('foo', 'FooClass')->addArgument(new \stdClass());
     try {
         $dumper->dump();
         $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     } catch (\RuntimeException $e) {
     }
 }
Ejemplo n.º 2
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new XmlDumper($container);
     $this->assertEquals(str_replace('%path%', self::$fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath . '/xml/services9.xml')), $dumper->dump(), '->dump() dumps services');
     $dumper = new XmlDumper($container = new Builder());
     $container->register('foo', 'FooClass')->addArgument(new \stdClass());
     try {
         $dumper->dump();
         $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
         $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     }
 }
Ejemplo n.º 3
0
<?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;
Ejemplo n.º 4
0
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder();
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$config = new BuilderConfiguration();
$config->setDefinition('baz', new Definition('BazClass'));
$config->setAlias('alias_for_foo', 'foo');
$container->merge($config);
$t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones');
$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
$container = new Builder();
$container->register('foo', 'FooClass');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
$t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');
// ->findAnnotatedServiceIds()
$t->diag('->findAnnotatedServiceIds()');
$builder = new Builder();
$builder->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('bar', array('bar' => 'bar'))->addAnnotation('foo', array('foofoo' => 'foofoo'));
$t->is($builder->findAnnotatedServiceIds('foo'), array('foo' => array(array('foo' => 'foo'), array('foofoo' => 'foofoo'))), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes');
$t->is($builder->findAnnotatedServiceIds('foobar'), array(), '->findAnnotatedServiceIds() returns an empty array if there is annotated services');
Ejemplo n.º 5
0
 /**
  * @covers Symfony\Components\DependencyInjection\Builder::findAnnotatedServiceIds
  */
 public function testFindAnnotatedServiceIds()
 {
     $builder = new Builder();
     $builder->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('bar', array('bar' => 'bar'))->addAnnotation('foo', array('foofoo' => 'foofoo'));
     $this->assertEquals($builder->findAnnotatedServiceIds('foo'), array('foo' => array(array('foo' => 'foo'), array('foofoo' => 'foofoo'))), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes');
     $this->assertEquals(array(), $builder->findAnnotatedServiceIds('foobar'), '->findAnnotatedServiceIds() returns an empty array if there is annotated services');
 }
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../../../../bootstrap.php';
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Dumper\XmlDumper;
$t = new LimeTest(4);
$fixturesPath = realpath(__DIR__ . '/../../../../../fixtures/Symfony/Components/DependencyInjection/');
// ->dump()
$t->diag('->dump()');
$dumper = new XmlDumper($container = new Builder());
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/xml/services1.xml'), '->dump() dumps an empty container as an empty XML file');
$container = new Builder();
$dumper = new XmlDumper($container);
// ->addParameters()
$t->diag('->addParameters()');
$container = (include $fixturesPath . '//containers/container8.php');
$dumper = new XmlDumper($container);
$t->is($dumper->dump(), file_get_contents($fixturesPath . '/xml/services8.xml'), '->dump() dumps parameters');
// ->addService()
$t->diag('->addService()');
$container = (include $fixturesPath . '/containers/container9.php');
$dumper = new XmlDumper($container);
$t->is($dumper->dump(), str_replace('%path%', $fixturesPath . '/includes', file_get_contents($fixturesPath . '/xml/services9.xml')), '->dump() dumps services');
$dumper = new XmlDumper($container = new Builder());
$container->register('foo', 'FooClass')->addArgument(new stdClass());
try {
    $dumper->dump();
    $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
} catch (RuntimeException $e) {
    $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
}
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones');
$container = new Builder(array('bar' => 'foo', 'foo' => 'baz'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => 'bar'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder(array('bar' => 'foo'));
$config = new BuilderConfiguration();
$config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
$container->merge($config);
$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
$container = new Builder();
$container->register('foo', 'FooClass');
$container->register('bar', 'BarClass');
$config = new BuilderConfiguration();
$config->setDefinition('baz', new Definition('BazClass'));
$config->setAlias('alias_for_foo', 'foo');
$container->merge($config);
$t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->load() merges definitions already defined ones');
$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
$container = new Builder();
$container->register('foo', 'FooClass');
$config->setDefinition('foo', new Definition('BazClass'));
$container->merge($config);
$t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');
Ejemplo n.º 8
0
<?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'))
;
Ejemplo n.º 9
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
$container = new Builder();
$container->register('foo', 'FooClass')->addArgument(new Reference('bar'));
return $container;