IntegrationTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Tests\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. /**
  15. * This class tests the integration of the different compiler passes
  16. */
  17. class IntegrationTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * This tests that the following dependencies are correctly processed:
  21. *
  22. * A is public, B/C are private
  23. * A -> C
  24. * B -> C
  25. */
  26. public function testProcessRemovesAndInlinesRecursively()
  27. {
  28. $container = new ContainerBuilder();
  29. $a = $container
  30. ->register('a', '\stdClass')
  31. ->addArgument(new Reference('c'))
  32. ;
  33. $b = $container
  34. ->register('b', '\stdClass')
  35. ->addArgument(new Reference('c'))
  36. ->setPublic(false)
  37. ;
  38. $c = $container
  39. ->register('c', '\stdClass')
  40. ->setPublic(false)
  41. ;
  42. $container->compile();
  43. $this->assertTrue($container->hasDefinition('a'));
  44. $arguments = $a->getArguments();
  45. $this->assertSame($c, $arguments[0]);
  46. $this->assertFalse($container->hasDefinition('b'));
  47. $this->assertFalse($container->hasDefinition('c'));
  48. }
  49. public function testProcessInlinesReferencesToAliases()
  50. {
  51. $container = new ContainerBuilder();
  52. $a = $container
  53. ->register('a', '\stdClass')
  54. ->addArgument(new Reference('b'))
  55. ;
  56. $container->setAlias('b', new Alias('c', false));
  57. $c = $container
  58. ->register('c', '\stdClass')
  59. ->setPublic(false)
  60. ;
  61. $container->compile();
  62. $this->assertTrue($container->hasDefinition('a'));
  63. $arguments = $a->getArguments();
  64. $this->assertSame($c, $arguments[0]);
  65. $this->assertFalse($container->hasAlias('b'));
  66. $this->assertFalse($container->hasDefinition('c'));
  67. }
  68. public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
  69. {
  70. $container = new ContainerBuilder();
  71. $container
  72. ->register('a', '\stdClass')
  73. ->addArgument(new Reference('b'))
  74. ->addMethodCall('setC', array(new Reference('c')))
  75. ;
  76. $container
  77. ->register('b', '\stdClass')
  78. ->addArgument(new Reference('c'))
  79. ->setPublic(false)
  80. ;
  81. $container
  82. ->register('c', '\stdClass')
  83. ->setPublic(false)
  84. ;
  85. $container->compile();
  86. $this->assertTrue($container->hasDefinition('a'));
  87. $this->assertFalse($container->hasDefinition('b'));
  88. $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
  89. }
  90. }