IntegrationTest.php 3.2 KB

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