ResolveReferencesToAliasesPassTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Alias;
  13. use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. class ResolveReferencesToAliasesPassTest extends TestCase
  18. {
  19. public function testProcess()
  20. {
  21. $container = new ContainerBuilder();
  22. $container->setAlias('bar', 'foo');
  23. $def = $container
  24. ->register('moo')
  25. ->setArguments(array(new Reference('bar')))
  26. ;
  27. $this->process($container);
  28. $arguments = $def->getArguments();
  29. $this->assertEquals('foo', (string) $arguments[0]);
  30. }
  31. public function testProcessRecursively()
  32. {
  33. $container = new ContainerBuilder();
  34. $container->setAlias('bar', 'foo');
  35. $container->setAlias('moo', 'bar');
  36. $def = $container
  37. ->register('foobar')
  38. ->setArguments(array(new Reference('moo')))
  39. ;
  40. $this->process($container);
  41. $arguments = $def->getArguments();
  42. $this->assertEquals('foo', (string) $arguments[0]);
  43. }
  44. /**
  45. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  46. */
  47. public function testAliasCircularReference()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->setAlias('bar', 'foo');
  51. $container->setAlias('foo', 'bar');
  52. $this->process($container);
  53. }
  54. public function testResolveFactory()
  55. {
  56. $container = new ContainerBuilder();
  57. $container->register('factory', 'Factory');
  58. $container->setAlias('factory_alias', new Alias('factory'));
  59. $foo = new Definition();
  60. $foo->setFactory(array(new Reference('factory_alias'), 'createFoo'));
  61. $container->setDefinition('foo', $foo);
  62. $bar = new Definition();
  63. $bar->setFactory(array('Factory', 'createFoo'));
  64. $container->setDefinition('bar', $bar);
  65. $this->process($container);
  66. $resolvedFooFactory = $container->getDefinition('foo')->getFactory();
  67. $resolvedBarFactory = $container->getDefinition('bar')->getFactory();
  68. $this->assertSame('factory', (string) $resolvedFooFactory[0]);
  69. $this->assertSame('Factory', (string) $resolvedBarFactory[0]);
  70. }
  71. /**
  72. * @group legacy
  73. */
  74. public function testResolveFactoryService()
  75. {
  76. $container = new ContainerBuilder();
  77. $container->register('factory', 'Factory');
  78. $container->setAlias('factory_alias', new Alias('factory'));
  79. $foo = new Definition();
  80. $foo->setFactoryService('factory_alias');
  81. $foo->setFactoryMethod('createFoo');
  82. $container->setDefinition('foo', $foo);
  83. $this->process($container);
  84. $this->assertSame('factory', $foo->getFactoryService());
  85. }
  86. protected function process(ContainerBuilder $container)
  87. {
  88. $pass = new ResolveReferencesToAliasesPass();
  89. $pass->process($container);
  90. }
  91. }