ResolveDefinitionTemplatesPassTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  13. use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testProcess()
  18. {
  19. $container = new ContainerBuilder();
  20. $container->register('parent', 'foo')->setArguments(array('moo', 'b'))->setProperty('foo', 'moo');
  21. $container->setDefinition('child', new DefinitionDecorator('parent'))
  22. ->replaceArgument(0, 'a')
  23. ->setProperty('foo', 'bar')
  24. ->setClass('bar')
  25. ;
  26. $this->process($container);
  27. $def = $container->getDefinition('child');
  28. $this->assertNotInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $def);
  29. $this->assertEquals('bar', $def->getClass());
  30. $this->assertEquals(array('a', 'b'), $def->getArguments());
  31. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  32. }
  33. public function testProcessAppendsMethodCallsAlways()
  34. {
  35. $container = new ContainerBuilder();
  36. $container
  37. ->register('parent')
  38. ->addMethodCall('foo', array('bar'))
  39. ;
  40. $container
  41. ->setDefinition('child', new DefinitionDecorator('parent'))
  42. ->addMethodCall('bar', array('foo'))
  43. ;
  44. $this->process($container);
  45. $def = $container->getDefinition('child');
  46. $this->assertEquals(array(
  47. array('foo', array('bar')),
  48. array('bar', array('foo')),
  49. ), $def->getMethodCalls());
  50. }
  51. public function testProcessDoesNotCopyAbstract()
  52. {
  53. $container = new ContainerBuilder();
  54. $container
  55. ->register('parent')
  56. ->setAbstract(true)
  57. ;
  58. $container
  59. ->setDefinition('child', new DefinitionDecorator('parent'))
  60. ;
  61. $this->process($container);
  62. $def = $container->getDefinition('child');
  63. $this->assertFalse($def->isAbstract());
  64. }
  65. public function testProcessDoesNotCopyScope()
  66. {
  67. $container = new ContainerBuilder();
  68. $container
  69. ->register('parent')
  70. ->setScope('foo')
  71. ;
  72. $container
  73. ->setDefinition('child', new DefinitionDecorator('parent'))
  74. ;
  75. $this->process($container);
  76. $def = $container->getDefinition('child');
  77. $this->assertEquals(ContainerInterface::SCOPE_CONTAINER, $def->getScope());
  78. }
  79. public function testProcessDoesNotCopyTags()
  80. {
  81. $container = new ContainerBuilder();
  82. $container
  83. ->register('parent')
  84. ->addTag('foo')
  85. ;
  86. $container
  87. ->setDefinition('child', new DefinitionDecorator('parent'))
  88. ;
  89. $this->process($container);
  90. $def = $container->getDefinition('child');
  91. $this->assertEquals(array(), $def->getTags());
  92. }
  93. public function testProcessHandlesMultipleInheritance()
  94. {
  95. $container = new ContainerBuilder();
  96. $container
  97. ->register('parent', 'foo')
  98. ->setArguments(array('foo', 'bar', 'c'))
  99. ;
  100. $container
  101. ->setDefinition('child2', new DefinitionDecorator('child1'))
  102. ->replaceArgument(1, 'b')
  103. ;
  104. $container
  105. ->setDefinition('child1', new DefinitionDecorator('parent'))
  106. ->replaceArgument(0, 'a')
  107. ;
  108. $this->process($container);
  109. $def = $container->getDefinition('child2');
  110. $this->assertEquals(array('a', 'b', 'c'), $def->getArguments());
  111. $this->assertEquals('foo', $def->getClass());
  112. }
  113. public function testSetLazyOnServiceHasParent()
  114. {
  115. $container = new ContainerBuilder();
  116. $container->register('parent','stdClass');
  117. $container->setDefinition('child1',new DefinitionDecorator('parent'))
  118. ->setLazy(true)
  119. ;
  120. $this->process($container);
  121. $this->assertTrue($container->getDefinition('child1')->isLazy());
  122. }
  123. public function testSetLazyOnServiceIsParent()
  124. {
  125. $container = new ContainerBuilder();
  126. $container->register('parent','stdClass')
  127. ->setLazy(true)
  128. ;
  129. $container->setDefinition('child1',new DefinitionDecorator('parent'));
  130. $this->process($container);
  131. $this->assertTrue($container->getDefinition('child1')->isLazy());
  132. }
  133. protected function process(ContainerBuilder $container)
  134. {
  135. $pass = new ResolveDefinitionTemplatesPass();
  136. $pass->process($container);
  137. }
  138. }