Skip to content

Commit

Permalink
Fixed Factory services not within the ServiceReferenceGraph.
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi authored and fabpot committed Aug 27, 2014
1 parent 3cb9d7a commit e992f8e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
Expand Up @@ -69,7 +69,11 @@ public function process(ContainerBuilder $container)

$this->currentId = $id;
$this->currentDefinition = $definition;

$this->processArguments($definition->getArguments());
if ($definition->getFactoryService()) {
$this->processArguments(array(new Reference($definition->getFactoryService())));
}

if (!$this->onlyConstructorArguments) {
$this->processArguments($definition->getMethodCalls());
Expand Down
Expand Up @@ -97,6 +97,26 @@ public function testProcessDoesNotSaveDuplicateReferences()
$this->assertCount(2, $graph->getNode('a')->getInEdges());
}

public function testProcessDetectsFactoryReferences()
{
$container = new ContainerBuilder();

$container
->register('foo', 'stdClass')
->setFactoryClass('stdClass')
->setFactoryMethod('getInstance');

$container
->register('bar', 'stdClass')
->setFactoryService('foo')
->setFactoryMethod('getInstance');

$graph = $this->process($container);

$this->assertTrue($graph->hasNode('foo'));
$this->assertCount(1, $graph->getNode('foo')->getInEdges());
}

protected function process(ContainerBuilder $container)
{
$pass = new RepeatedPass(array(new AnalyzeServiceReferencesPass()));
Expand Down
Expand Up @@ -48,6 +48,26 @@ public function testProcessWithAliases()
$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcessWithFactory()
{
$container = new ContainerBuilder();

$container
->register('a', 'stdClass')
->setFactoryService('b')
->setFactoryMethod('getInstance');

$container
->register('b', 'stdClass')
->setFactoryService('a')
->setFactoryMethod('getInstance');

$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
Expand All @@ -61,6 +81,25 @@ public function testProcessDetectsIndirectCircularReference()
$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcessDetectsIndirectCircularReferenceWithFactory()
{
$container = new ContainerBuilder();

$container->register('a')->addArgument(new Reference('b'));

$container
->register('b', 'stdClass')
->setFactoryService('c')
->setFactoryMethod('getInstance');

$container->register('c')->addArgument(new Reference('a'));

$this->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
Expand Down
Expand Up @@ -81,6 +81,33 @@ public function testProcessWorksWithInlinedDefinitions()
$this->assertTrue($container->hasDefinition('bar'));
}

public function testProcessWontRemovePrivateFactory()
{
$container = new ContainerBuilder();

$container
->register('foo', 'stdClass')
->setFactoryClass('stdClass')
->setFactoryMethod('getInstance')
->setPublic(false);

$container
->register('bar', 'stdClass')
->setFactoryService('foo')
->setFactoryMethod('getInstance')
->setPublic(false);

$container
->register('foobar')
->addArgument(new Reference('bar'));

$this->process($container);

$this->assertTrue($container->hasDefinition('foo'));
$this->assertTrue($container->hasDefinition('bar'));
$this->assertTrue($container->hasDefinition('foobar'));
}

protected function process(ContainerBuilder $container)
{
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new RemoveUnusedDefinitionsPass()));
Expand Down

0 comments on commit e992f8e

Please sign in to comment.