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
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-12-13 17:37:51 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
2011-01-17 22:28:59 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
|
2011-01-17 22:28:59 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\Compiler;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2011-01-17 22:28:59 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class CheckCircularReferencesPassTest extends TestCase
|
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 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')
|
2019-01-16 09:39:14 +00:00
|
|
|
->setFactory([new Reference('b'), 'getInstance']);
|
2014-08-11 10:11:49 +01:00
|
|
|
|
|
|
|
$container
|
|
|
|
->register('b', 'stdClass')
|
2019-01-16 09:39:14 +00:00
|
|
|
->setFactory([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')
|
2019-01-16 09:39:14 +00:00
|
|
|
->setFactory([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'));
|
2019-01-16 09:39:14 +00:00
|
|
|
$container->register('b')->addMethodCall('setA', [new Reference('a')]);
|
2011-01-17 22:28:59 +00:00
|
|
|
|
|
|
|
$this->process($container);
|
2017-03-18 09:10:35 +00:00
|
|
|
|
|
|
|
$this->addToAssertionCount(1);
|
2011-01-17 22:28:59 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:37:51 +00:00
|
|
|
public function testProcessIgnoresLazyServices()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->setLazy(true)->addArgument(new Reference('b'));
|
|
|
|
$container->register('b')->addArgument(new Reference('a'));
|
|
|
|
|
|
|
|
$this->process($container);
|
2017-04-12 19:52:58 +01:00
|
|
|
|
|
|
|
// just make sure that a lazily loaded service does not trigger a CircularReferenceException
|
|
|
|
$this->addToAssertionCount(1);
|
2016-12-13 17:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessIgnoresIteratorArguments()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('a')->addArgument(new Reference('b'));
|
2019-01-16 09:39:14 +00:00
|
|
|
$container->register('b')->addArgument(new IteratorArgument([new Reference('a')]));
|
2016-12-13 17:37:51 +00:00
|
|
|
|
|
|
|
$this->process($container);
|
2017-04-12 19:52:58 +01:00
|
|
|
|
|
|
|
// just make sure that an IteratorArgument does not trigger a CircularReferenceException
|
|
|
|
$this->addToAssertionCount(1);
|
2016-12-13 17:37:51 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 22:28:59 +00:00
|
|
|
protected function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$compiler = new Compiler();
|
|
|
|
$passConfig = $compiler->getPassConfig();
|
2019-01-16 09:39:14 +00:00
|
|
|
$passConfig->setOptimizationPasses([
|
2011-01-17 22:28:59 +00:00
|
|
|
new AnalyzeServiceReferencesPass(true),
|
|
|
|
new CheckCircularReferencesPass(),
|
2019-01-16 09:39:14 +00:00
|
|
|
]);
|
|
|
|
$passConfig->setRemovingPasses([]);
|
2011-01-17 22:28:59 +00:00
|
|
|
|
|
|
|
$compiler->compile($container);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|