InlineServiceDefinitionsPassTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Symfony\Component\DependencyInjection\Scope;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  14. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  15. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  16. use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. class InlineServiceDefinitionsPassTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function testProcess()
  22. {
  23. $container = new ContainerBuilder();
  24. $container
  25. ->register('inlinable.service')
  26. ->setPublic(false)
  27. ;
  28. $container
  29. ->register('service')
  30. ->setArguments(array(new Reference('inlinable.service')))
  31. ;
  32. $this->process($container);
  33. $arguments = $container->getDefinition('service')->getArguments();
  34. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $arguments[0]);
  35. $this->assertSame($container->getDefinition('inlinable.service'), $arguments[0]);
  36. }
  37. public function testProcessDoesNotInlineWhenAliasedServiceIsNotOfPrototypeScope()
  38. {
  39. $container = new ContainerBuilder();
  40. $container
  41. ->register('foo')
  42. ->setPublic(false)
  43. ;
  44. $container->setAlias('moo', 'foo');
  45. $container
  46. ->register('service')
  47. ->setArguments(array($ref = new Reference('foo')))
  48. ;
  49. $this->process($container);
  50. $arguments = $container->getDefinition('service')->getArguments();
  51. $this->assertSame($ref, $arguments[0]);
  52. }
  53. public function testProcessDoesInlineServiceOfPrototypeScope()
  54. {
  55. $container = new ContainerBuilder();
  56. $container
  57. ->register('foo')
  58. ->setScope('prototype')
  59. ;
  60. $container
  61. ->register('bar')
  62. ->setPublic(false)
  63. ->setScope('prototype')
  64. ;
  65. $container->setAlias('moo', 'bar');
  66. $container
  67. ->register('service')
  68. ->setArguments(array(new Reference('foo'), $ref = new Reference('moo'), new Reference('bar')))
  69. ;
  70. $this->process($container);
  71. $arguments = $container->getDefinition('service')->getArguments();
  72. $this->assertEquals($container->getDefinition('foo'), $arguments[0]);
  73. $this->assertNotSame($container->getDefinition('foo'), $arguments[0]);
  74. $this->assertSame($ref, $arguments[1]);
  75. $this->assertEquals($container->getDefinition('bar'), $arguments[2]);
  76. $this->assertNotSame($container->getDefinition('bar'), $arguments[2]);
  77. }
  78. public function testProcessInlinesIfMultipleReferencesButAllFromTheSameDefinition()
  79. {
  80. $container = new ContainerBuilder();
  81. $a = $container->register('a')->setPublic(false);
  82. $b = $container
  83. ->register('b')
  84. ->addArgument(new Reference('a'))
  85. ->addArgument(new Definition(null, array(new Reference('a'))))
  86. ;
  87. $this->process($container);
  88. $arguments = $b->getArguments();
  89. $this->assertSame($a, $arguments[0]);
  90. $inlinedArguments = $arguments[1]->getArguments();
  91. $this->assertSame($a, $inlinedArguments[0]);
  92. }
  93. public function testProcessInlinesOnlyIfSameScope()
  94. {
  95. $container = new ContainerBuilder();
  96. $container->addScope(new Scope('foo'));
  97. $a = $container->register('a')->setPublic(false)->setScope('foo');
  98. $b = $container->register('b')->addArgument(new Reference('a'));
  99. $this->process($container);
  100. $arguments = $b->getArguments();
  101. $this->assertEquals(new Reference('a'), $arguments[0]);
  102. $this->assertTrue($container->hasDefinition('a'));
  103. }
  104. protected function process(ContainerBuilder $container)
  105. {
  106. $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));
  107. $repeatedPass->process($container);
  108. }
  109. }