You are here

public function ContainerBuilderTest::testCreateServiceFactory in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php \Symfony\Component\DependencyInjection\Tests\ContainerBuilderTest::testCreateServiceFactory()

@covers Symfony\Component\DependencyInjection\ContainerBuilder::createService

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php, line 329

Class

ContainerBuilderTest

Namespace

Symfony\Component\DependencyInjection\Tests

Code

public function testCreateServiceFactory() {
  $builder = new ContainerBuilder();
  $builder
    ->register('foo', 'Bar\\FooClass')
    ->setFactory('Bar\\FooClass::getInstance');
  $builder
    ->register('qux', 'Bar\\FooClass')
    ->setFactory(array(
    'Bar\\FooClass',
    'getInstance',
  ));
  $builder
    ->register('bar', 'Bar\\FooClass')
    ->setFactory(array(
    new Definition('Bar\\FooClass'),
    'getInstance',
  ));
  $builder
    ->register('baz', 'Bar\\FooClass')
    ->setFactory(array(
    new Reference('bar'),
    'getInstance',
  ));
  $this
    ->assertTrue($builder
    ->get('foo')->called, '->createService() calls the factory method to create the service instance');
  $this
    ->assertTrue($builder
    ->get('qux')->called, '->createService() calls the factory method to create the service instance');
  $this
    ->assertTrue($builder
    ->get('bar')->called, '->createService() uses anonymous service as factory');
  $this
    ->assertTrue($builder
    ->get('baz')->called, '->createService() uses another service as factory');
}