2011-01-17 22:28:59 +00:00
|
|
|
<?php
|
|
|
|
|
2011-05-31 09:57:06 +01:00
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-05-31 09:57:06 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-05-31 09:57:06 +01:00
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
2011-01-17 22:28:59 +00:00
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
|
|
|
|
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
|
|
|
|
use Symfony\Component\DependencyInjection\Compiler\Compiler;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
|
|
|
|
class CheckCircularReferencesPassTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
2013-04-17 15:22:08 +01:00
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
2011-01-17 22:28:59 +00:00
|
|
|
*/
|
|
|
|
public function testProcess()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
$container->register('b')->addArgument(new Reference('a'));
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2011-07-11 17:25:40 +01:00
|
|
|
/**
|
2013-04-17 15:22:08 +01:00
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
2011-07-11 17:25:40 +01:00
|
|
|
*/
|
|
|
|
public function testProcessWithAliases()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
$container->setAlias('b', 'c');
|
|
|
|
$container->setAlias('c', 'a');
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2014-08-11 10:11:49 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testProcessWithFactory()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
|
|
|
|
$container
|
|
|
|
->register('a', 'stdClass')
|
2015-01-04 20:27:00 +00:00
|
|
|
->setFactory(array(new Reference('b'), 'getInstance'));
|
2014-08-11 10:11:49 +01:00
|
|
|
|
|
|
|
$container
|
|
|
|
->register('b', 'stdClass')
|
2015-01-04 20:27:00 +00:00
|
|
|
->setFactory(array(new Reference('a'), 'getInstance'));
|
2014-08-11 10:11:49 +01:00
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
/**
|
2013-04-17 15:22:08 +01:00
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
2011-01-17 22:28:59 +00:00
|
|
|
*/
|
|
|
|
public function testProcessDetectsIndirectCircularReference()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
$container->register('b')->addArgument(new Reference('c'));
|
|
|
|
$container->register('c')->addArgument(new Reference('a'));
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2014-08-11 10:11:49 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testProcessDetectsIndirectCircularReferenceWithFactory()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
|
|
|
|
$container
|
|
|
|
->register('b', 'stdClass')
|
2015-01-04 20:27:00 +00:00
|
|
|
->setFactory(array(new Reference('c'), 'getInstance'));
|
2014-08-11 10:11:49 +01:00
|
|
|
|
|
|
|
$container->register('c')->addArgument(new Reference('a'));
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2013-04-17 15:22:08 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testDeepCircularReference()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
$container->register('b')->addArgument(new Reference('c'));
|
|
|
|
$container->register('c')->addArgument(new Reference('b'));
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
public function testProcessIgnoresMethodCalls()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
|
|
|
$container->register('b')->addMethodCall('setA', array(new Reference('a')));
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$compiler = new Compiler();
|
|
|
|
$passConfig = $compiler->getPassConfig();
|
|
|
|
$passConfig->setOptimizationPasses(array(
|
|
|
|
new AnalyzeServiceReferencesPass(true),
|
|
|
|
new CheckCircularReferencesPass(),
|
|
|
|
));
|
|
|
|
$passConfig->setRemovingPasses(array());
|
|
|
|
|
|
|
|
$compiler->compile($container);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|