This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckCircularReferencesPassTest.php

159 lines
5.2 KiB
PHP
Raw Normal View History

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
*/
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;
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
{
/**
* @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);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
*/
public function testProcessWithAliases()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->setAlias('b', 'c');
$container->setAlias('c', 'a');
$this->process($container);
}
/**
* @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'));
$container
->register('b', 'stdClass')
2015-01-04 20:27:00 +00:00
->setFactory(array(new Reference('a'), 'getInstance'));
$this->process($container);
}
2011-01-17 22:28:59 +00: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);
}
/**
* @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'));
$container->register('c')->addArgument(new Reference('a'));
$this->process($container);
}
/**
* @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);
2017-03-18 09:10:35 +00:00
$this->addToAssertionCount(1);
2011-01-17 22:28:59 +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);
}
public function testProcessIgnoresIteratorArguments()
{
$container = new ContainerBuilder();
$container->register('a')->addArgument(new Reference('b'));
$container->register('b')->addArgument(new IteratorArgument(array(new Reference('a'))));
$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);
}
2011-01-17 22:28:59 +00:00
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
}