RemoveUnusedDefinitionsPassTest.php 3.3 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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
  13. use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
  14. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. class RemoveUnusedDefinitionsPassTest extends TestCase
  19. {
  20. public function testProcess()
  21. {
  22. $container = new ContainerBuilder();
  23. $container
  24. ->register('foo')
  25. ->setPublic(false)
  26. ;
  27. $container
  28. ->register('bar')
  29. ->setPublic(false)
  30. ;
  31. $container
  32. ->register('moo')
  33. ->setArguments(array(new Reference('bar')))
  34. ;
  35. $this->process($container);
  36. $this->assertFalse($container->hasDefinition('foo'));
  37. $this->assertTrue($container->hasDefinition('bar'));
  38. $this->assertTrue($container->hasDefinition('moo'));
  39. }
  40. public function testProcessRemovesUnusedDefinitionsRecursively()
  41. {
  42. $container = new ContainerBuilder();
  43. $container
  44. ->register('foo')
  45. ->setPublic(false)
  46. ;
  47. $container
  48. ->register('bar')
  49. ->setArguments(array(new Reference('foo')))
  50. ->setPublic(false)
  51. ;
  52. $this->process($container);
  53. $this->assertFalse($container->hasDefinition('foo'));
  54. $this->assertFalse($container->hasDefinition('bar'));
  55. }
  56. public function testProcessWorksWithInlinedDefinitions()
  57. {
  58. $container = new ContainerBuilder();
  59. $container
  60. ->register('foo')
  61. ->setPublic(false)
  62. ;
  63. $container
  64. ->register('bar')
  65. ->setArguments(array(new Definition(null, array(new Reference('foo')))))
  66. ;
  67. $this->process($container);
  68. $this->assertTrue($container->hasDefinition('foo'));
  69. $this->assertTrue($container->hasDefinition('bar'));
  70. }
  71. public function testProcessWontRemovePrivateFactory()
  72. {
  73. $container = new ContainerBuilder();
  74. $container
  75. ->register('foo', 'stdClass')
  76. ->setFactory(array('stdClass', 'getInstance'))
  77. ->setPublic(false);
  78. $container
  79. ->register('bar', 'stdClass')
  80. ->setFactory(array(new Reference('foo'), 'getInstance'))
  81. ->setPublic(false);
  82. $container
  83. ->register('foobar')
  84. ->addArgument(new Reference('bar'));
  85. $this->process($container);
  86. $this->assertTrue($container->hasDefinition('foo'));
  87. $this->assertTrue($container->hasDefinition('bar'));
  88. $this->assertTrue($container->hasDefinition('foobar'));
  89. }
  90. protected function process(ContainerBuilder $container)
  91. {
  92. $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new RemoveUnusedDefinitionsPass()));
  93. $repeatedPass->process($container);
  94. }
  95. }