IntegrationTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\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. $container->setResourceTracking(false);
  30. $a = $container
  31. ->register('a', '\stdClass')
  32. ->addArgument(new Reference('c'))
  33. ;
  34. $b = $container
  35. ->register('b', '\stdClass')
  36. ->addArgument(new Reference('c'))
  37. ->setPublic(false)
  38. ;
  39. $c = $container
  40. ->register('c', '\stdClass')
  41. ->setPublic(false)
  42. ;
  43. $container->compile();
  44. $this->assertTrue($container->hasDefinition('a'));
  45. $arguments = $a->getArguments();
  46. $this->assertSame($c, $arguments[0]);
  47. $this->assertFalse($container->hasDefinition('b'));
  48. $this->assertFalse($container->hasDefinition('c'));
  49. }
  50. public function testProcessInlinesReferencesToAliases()
  51. {
  52. $container = new ContainerBuilder();
  53. $container->setResourceTracking(false);
  54. $a = $container
  55. ->register('a', '\stdClass')
  56. ->addArgument(new Reference('b'))
  57. ;
  58. $container->setAlias('b', new Alias('c', false));
  59. $c = $container
  60. ->register('c', '\stdClass')
  61. ->setPublic(false)
  62. ;
  63. $container->compile();
  64. $this->assertTrue($container->hasDefinition('a'));
  65. $arguments = $a->getArguments();
  66. $this->assertSame($c, $arguments[0]);
  67. $this->assertFalse($container->hasAlias('b'));
  68. $this->assertFalse($container->hasDefinition('c'));
  69. }
  70. public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
  71. {
  72. $container = new ContainerBuilder();
  73. $container->setResourceTracking(false);
  74. $container
  75. ->register('a', '\stdClass')
  76. ->addArgument(new Reference('b'))
  77. ->addMethodCall('setC', array(new Reference('c')))
  78. ;
  79. $container
  80. ->register('b', '\stdClass')
  81. ->addArgument(new Reference('c'))
  82. ->setPublic(false)
  83. ;
  84. $container
  85. ->register('c', '\stdClass')
  86. ->setPublic(false)
  87. ;
  88. $container->compile();
  89. $this->assertTrue($container->hasDefinition('a'));
  90. $this->assertFalse($container->hasDefinition('b'));
  91. $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
  92. }
  93. }