RemoveUnusedDefinitionsPassTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Compiler\AnalyzeServiceReferencesPass;
  12. use Symfony\Component\DependencyInjection\Compiler\Compiler;
  13. use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
  14. use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. class RemoveUnusedDefinitionsPassTest extends \PHPUnit_Framework_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. protected function process(ContainerBuilder $container)
  72. {
  73. $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new RemoveUnusedDefinitionsPass()));
  74. $repeatedPass->process($container);
  75. }
  76. }