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/ResolveReferencesToAliasesPassTest.php

134 lines
4.2 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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]);
}
/**
* @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]);
}
/**
* @group legacy
* @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.
*/
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
* @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.
*/
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
}