ReplaceAliasByActualDefinitionPassTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Compiler\ReplaceAliasByActualDefinitionPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. require_once __DIR__.'/../Fixtures/includes/foo.php';
  17. class ReplaceAliasByActualDefinitionPassTest extends TestCase
  18. {
  19. public function testProcess()
  20. {
  21. $container = new ContainerBuilder();
  22. $aDefinition = $container->register('a', '\stdClass');
  23. $aDefinition->setFactoryService('b', false);
  24. $aDefinition->setFactory(array(new Reference('b'), 'createA'));
  25. $bDefinition = new Definition('\stdClass');
  26. $bDefinition->setPublic(false);
  27. $container->setDefinition('b', $bDefinition);
  28. $container->setAlias('a_alias', 'a');
  29. $container->setAlias('b_alias', 'b');
  30. $container->setAlias('container', 'service_container');
  31. $this->process($container);
  32. $this->assertTrue($container->has('a'), '->process() does nothing to public definitions.');
  33. $this->assertTrue($container->hasAlias('a_alias'));
  34. $this->assertFalse($container->has('b'), '->process() removes non-public definitions.');
  35. $this->assertTrue(
  36. $container->has('b_alias') && !$container->hasAlias('b_alias'),
  37. '->process() replaces alias to actual.'
  38. );
  39. $this->assertSame('b_alias', $aDefinition->getFactoryService(false));
  40. $this->assertTrue($container->has('container'));
  41. $resolvedFactory = $aDefinition->getFactory();
  42. $this->assertSame('b_alias', (string) $resolvedFactory[0]);
  43. }
  44. /**
  45. * @group legacy
  46. */
  47. public function testPrivateAliasesInFactory()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->register('a', 'Bar\FooClass');
  51. $container->register('b', 'Bar\FooClass')
  52. ->setFactoryService('a')
  53. ->setFactoryMethod('getInstance');
  54. $container->register('c', 'stdClass')->setPublic(false);
  55. $container->setAlias('c_alias', 'c');
  56. $this->process($container);
  57. $this->assertInstanceOf('Bar\FooClass', $container->get('b'));
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. */
  62. public function testProcessWithInvalidAlias()
  63. {
  64. $container = new ContainerBuilder();
  65. $container->setAlias('a_alias', 'a');
  66. $this->process($container);
  67. }
  68. protected function process(ContainerBuilder $container)
  69. {
  70. $pass = new ReplaceAliasByActualDefinitionPass();
  71. $pass->process($container);
  72. }
  73. }