2011-01-05 11:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-01-27 07:44:59 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Alias;
|
2011-01-05 11:13:27 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class ResolveReferencesToAliasesPassTest extends TestCase
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
|
|
|
public function testProcess()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->setAlias('bar', 'foo');
|
|
|
|
$def = $container
|
|
|
|
->register('moo')
|
2019-01-16 09:39:14 +00:00
|
|
|
->setArguments([new Reference('bar')])
|
2011-01-05 11:13:27 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
|
|
|
|
$arguments = $def->getArguments();
|
|
|
|
$this->assertEquals('foo', (string) $arguments[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testProcessRecursively()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->setAlias('bar', 'foo');
|
|
|
|
$container->setAlias('moo', 'bar');
|
|
|
|
$def = $container
|
|
|
|
->register('foobar')
|
2019-01-16 09:39:14 +00:00
|
|
|
->setArguments([new Reference('moo')])
|
2011-01-05 11:13:27 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
|
|
|
|
$arguments = $def->getArguments();
|
|
|
|
$this->assertEquals('foo', (string) $arguments[0]);
|
|
|
|
}
|
|
|
|
|
2014-10-25 14:19:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
|
|
|
|
*/
|
|
|
|
public function testAliasCircularReference()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->setAlias('bar', 'foo');
|
|
|
|
$container->setAlias('foo', 'bar');
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2016-01-27 07:44:59 +00:00
|
|
|
public function testResolveFactory()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
$container->register('factory', 'Factory');
|
|
|
|
$container->setAlias('factory_alias', new Alias('factory'));
|
|
|
|
$foo = new Definition();
|
2019-01-16 09:39:14 +00:00
|
|
|
$foo->setFactory([new Reference('factory_alias'), 'createFoo']);
|
2016-01-27 07:44:59 +00:00
|
|
|
$container->setDefinition('foo', $foo);
|
|
|
|
$bar = new Definition();
|
2019-01-16 09:39:14 +00:00
|
|
|
$bar->setFactory(['Factory', 'createFoo']);
|
2016-01-27 07:44:59 +00:00
|
|
|
$container->setDefinition('bar', $bar);
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
|
|
|
|
$resolvedFooFactory = $container->getDefinition('foo')->getFactory();
|
|
|
|
$resolvedBarFactory = $container->getDefinition('bar')->getFactory();
|
|
|
|
|
|
|
|
$this->assertSame('factory', (string) $resolvedFooFactory[0]);
|
|
|
|
$this->assertSame('Factory', (string) $resolvedBarFactory[0]);
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:13:13 +00:00
|
|
|
/**
|
|
|
|
* @group legacy
|
2019-01-26 21:26:02 +00:00
|
|
|
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "alias" alias.
|
2019-01-23 16:13:13 +00:00
|
|
|
*/
|
|
|
|
public function testDeprecationNoticeWhenReferencedByAlias()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
|
|
|
|
$container->register('foo', 'stdClass');
|
|
|
|
|
|
|
|
$aliasDeprecated = new Alias('foo');
|
|
|
|
$aliasDeprecated->setDeprecated(true);
|
|
|
|
$container->setAlias('deprecated_foo_alias', $aliasDeprecated);
|
|
|
|
|
|
|
|
$alias = new Alias('deprecated_foo_alias');
|
|
|
|
$container->setAlias('alias', $alias);
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group legacy
|
2019-01-26 21:26:02 +00:00
|
|
|
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "definition" service.
|
2019-01-23 16:13:13 +00:00
|
|
|
*/
|
|
|
|
public function testDeprecationNoticeWhenReferencedByDefinition()
|
|
|
|
{
|
|
|
|
$container = new ContainerBuilder();
|
|
|
|
|
|
|
|
$container->register('foo', 'stdClass');
|
|
|
|
|
|
|
|
$aliasDeprecated = new Alias('foo');
|
|
|
|
$aliasDeprecated->setDeprecated(true);
|
|
|
|
$container->setAlias('foo_aliased', $aliasDeprecated);
|
|
|
|
|
|
|
|
$container
|
|
|
|
->register('definition')
|
|
|
|
->setArguments([new Reference('foo_aliased')])
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->process($container);
|
|
|
|
}
|
|
|
|
|
2011-01-05 11:13:27 +00:00
|
|
|
protected function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$pass = new ResolveReferencesToAliasesPass();
|
|
|
|
$pass->process($container);
|
|
|
|
}
|
2011-06-08 18:56:59 +01:00
|
|
|
}
|