ResolveDefinitionTemplatesPassTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\ResolveDefinitionTemplatesPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  16. class ResolveDefinitionTemplatesPassTest extends TestCase
  17. {
  18. public function testProcess()
  19. {
  20. $container = new ContainerBuilder();
  21. $container->register('parent', 'foo')->setArguments(array('moo', 'b'))->setProperty('foo', 'moo');
  22. $container->setDefinition('child', new DefinitionDecorator('parent'))
  23. ->replaceArgument(0, 'a')
  24. ->setProperty('foo', 'bar')
  25. ->setClass('bar')
  26. ;
  27. $this->process($container);
  28. $def = $container->getDefinition('child');
  29. $this->assertNotInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $def);
  30. $this->assertEquals('bar', $def->getClass());
  31. $this->assertEquals(array('a', 'b'), $def->getArguments());
  32. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  33. }
  34. public function testProcessAppendsMethodCallsAlways()
  35. {
  36. $container = new ContainerBuilder();
  37. $container
  38. ->register('parent')
  39. ->addMethodCall('foo', array('bar'))
  40. ;
  41. $container
  42. ->setDefinition('child', new DefinitionDecorator('parent'))
  43. ->addMethodCall('bar', array('foo'))
  44. ;
  45. $this->process($container);
  46. $def = $container->getDefinition('child');
  47. $this->assertEquals(array(
  48. array('foo', array('bar')),
  49. array('bar', array('foo')),
  50. ), $def->getMethodCalls());
  51. }
  52. public function testProcessDoesNotCopyAbstract()
  53. {
  54. $container = new ContainerBuilder();
  55. $container
  56. ->register('parent')
  57. ->setAbstract(true)
  58. ;
  59. $container
  60. ->setDefinition('child', new DefinitionDecorator('parent'))
  61. ;
  62. $this->process($container);
  63. $def = $container->getDefinition('child');
  64. $this->assertFalse($def->isAbstract());
  65. }
  66. /**
  67. * @group legacy
  68. */
  69. public function testProcessDoesNotCopyScope()
  70. {
  71. $container = new ContainerBuilder();
  72. $container
  73. ->register('parent')
  74. ->setScope('foo')
  75. ;
  76. $container
  77. ->setDefinition('child', new DefinitionDecorator('parent'))
  78. ;
  79. $this->process($container);
  80. $def = $container->getDefinition('child');
  81. $this->assertEquals(ContainerInterface::SCOPE_CONTAINER, $def->getScope());
  82. }
  83. public function testProcessDoesNotCopyShared()
  84. {
  85. $container = new ContainerBuilder();
  86. $container
  87. ->register('parent')
  88. ->setShared(false)
  89. ;
  90. $container
  91. ->setDefinition('child', new DefinitionDecorator('parent'))
  92. ;
  93. $this->process($container);
  94. $def = $container->getDefinition('child');
  95. $this->assertTrue($def->isShared());
  96. }
  97. public function testProcessDoesNotCopyTags()
  98. {
  99. $container = new ContainerBuilder();
  100. $container
  101. ->register('parent')
  102. ->addTag('foo')
  103. ;
  104. $container
  105. ->setDefinition('child', new DefinitionDecorator('parent'))
  106. ;
  107. $this->process($container);
  108. $def = $container->getDefinition('child');
  109. $this->assertEquals(array(), $def->getTags());
  110. }
  111. public function testProcessDoesNotCopyDecoratedService()
  112. {
  113. $container = new ContainerBuilder();
  114. $container
  115. ->register('parent')
  116. ->setDecoratedService('foo')
  117. ;
  118. $container
  119. ->setDefinition('child', new DefinitionDecorator('parent'))
  120. ;
  121. $this->process($container);
  122. $def = $container->getDefinition('child');
  123. $this->assertNull($def->getDecoratedService());
  124. }
  125. public function testProcessDoesNotDropShared()
  126. {
  127. $container = new ContainerBuilder();
  128. $container
  129. ->register('parent')
  130. ;
  131. $container
  132. ->setDefinition('child', new DefinitionDecorator('parent'))
  133. ->setShared(false)
  134. ;
  135. $this->process($container);
  136. $def = $container->getDefinition('child');
  137. $this->assertFalse($def->isShared());
  138. }
  139. public function testProcessHandlesMultipleInheritance()
  140. {
  141. $container = new ContainerBuilder();
  142. $container
  143. ->register('parent', 'foo')
  144. ->setArguments(array('foo', 'bar', 'c'))
  145. ;
  146. $container
  147. ->setDefinition('child2', new DefinitionDecorator('child1'))
  148. ->replaceArgument(1, 'b')
  149. ;
  150. $container
  151. ->setDefinition('child1', new DefinitionDecorator('parent'))
  152. ->replaceArgument(0, 'a')
  153. ;
  154. $this->process($container);
  155. $def = $container->getDefinition('child2');
  156. $this->assertEquals(array('a', 'b', 'c'), $def->getArguments());
  157. $this->assertEquals('foo', $def->getClass());
  158. }
  159. public function testSetLazyOnServiceHasParent()
  160. {
  161. $container = new ContainerBuilder();
  162. $container->register('parent', 'stdClass');
  163. $container->setDefinition('child1', new DefinitionDecorator('parent'))
  164. ->setLazy(true)
  165. ;
  166. $this->process($container);
  167. $this->assertTrue($container->getDefinition('child1')->isLazy());
  168. }
  169. public function testSetLazyOnServiceIsParent()
  170. {
  171. $container = new ContainerBuilder();
  172. $container->register('parent', 'stdClass')
  173. ->setLazy(true)
  174. ;
  175. $container->setDefinition('child1', new DefinitionDecorator('parent'));
  176. $this->process($container);
  177. $this->assertTrue($container->getDefinition('child1')->isLazy());
  178. }
  179. public function testSetAutowiredOnServiceHasParent()
  180. {
  181. $container = new ContainerBuilder();
  182. $container->register('parent', 'stdClass');
  183. $container->setDefinition('child1', new DefinitionDecorator('parent'))
  184. ->setAutowired(true)
  185. ;
  186. $this->process($container);
  187. $this->assertTrue($container->getDefinition('child1')->isAutowired());
  188. }
  189. public function testSetAutowiredOnServiceIsParent()
  190. {
  191. $container = new ContainerBuilder();
  192. $container->register('parent', 'stdClass')
  193. ->setAutowired(true)
  194. ;
  195. $container->setDefinition('child1', new DefinitionDecorator('parent'));
  196. $this->process($container);
  197. $this->assertTrue($container->getDefinition('child1')->isAutowired());
  198. }
  199. public function testDeepDefinitionsResolving()
  200. {
  201. $container = new ContainerBuilder();
  202. $container->register('parent', 'parentClass');
  203. $container->register('sibling', 'siblingClass')
  204. ->setConfigurator(new DefinitionDecorator('parent'), 'foo')
  205. ->setFactory(array(new DefinitionDecorator('parent'), 'foo'))
  206. ->addArgument(new DefinitionDecorator('parent'))
  207. ->setProperty('prop', new DefinitionDecorator('parent'))
  208. ->addMethodCall('meth', array(new DefinitionDecorator('parent')))
  209. ;
  210. $this->process($container);
  211. $configurator = $container->getDefinition('sibling')->getConfigurator();
  212. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $configurator);
  213. $this->assertSame('parentClass', $configurator->getClass());
  214. $factory = $container->getDefinition('sibling')->getFactory();
  215. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $factory[0]);
  216. $this->assertSame('parentClass', $factory[0]->getClass());
  217. $argument = $container->getDefinition('sibling')->getArgument(0);
  218. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $argument);
  219. $this->assertSame('parentClass', $argument->getClass());
  220. $properties = $container->getDefinition('sibling')->getProperties();
  221. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $properties['prop']);
  222. $this->assertSame('parentClass', $properties['prop']->getClass());
  223. $methodCalls = $container->getDefinition('sibling')->getMethodCalls();
  224. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $methodCalls[0][1][0]);
  225. $this->assertSame('parentClass', $methodCalls[0][1][0]->getClass());
  226. }
  227. public function testSetDecoratedServiceOnServiceHasParent()
  228. {
  229. $container = new ContainerBuilder();
  230. $container->register('parent', 'stdClass');
  231. $container->setDefinition('child1', new DefinitionDecorator('parent'))
  232. ->setDecoratedService('foo', 'foo_inner', 5)
  233. ;
  234. $this->process($container);
  235. $this->assertEquals(array('foo', 'foo_inner', 5), $container->getDefinition('child1')->getDecoratedService());
  236. }
  237. public function testDecoratedServiceCopiesDeprecatedStatusFromParent()
  238. {
  239. $container = new ContainerBuilder();
  240. $container->register('deprecated_parent')
  241. ->setDeprecated(true)
  242. ;
  243. $container->setDefinition('decorated_deprecated_parent', new DefinitionDecorator('deprecated_parent'));
  244. $this->process($container);
  245. $this->assertTrue($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
  246. }
  247. public function testDecoratedServiceCanOverwriteDeprecatedParentStatus()
  248. {
  249. $container = new ContainerBuilder();
  250. $container->register('deprecated_parent')
  251. ->setDeprecated(true)
  252. ;
  253. $container->setDefinition('decorated_deprecated_parent', new DefinitionDecorator('deprecated_parent'))
  254. ->setDeprecated(false)
  255. ;
  256. $this->process($container);
  257. $this->assertFalse($container->getDefinition('decorated_deprecated_parent')->isDeprecated());
  258. }
  259. public function testProcessMergeAutowiringTypes()
  260. {
  261. $container = new ContainerBuilder();
  262. $container
  263. ->register('parent')
  264. ->addAutowiringType('Foo')
  265. ;
  266. $container
  267. ->setDefinition('child', new DefinitionDecorator('parent'))
  268. ->addAutowiringType('Bar')
  269. ;
  270. $this->process($container);
  271. $childDef = $container->getDefinition('child');
  272. $this->assertEquals(array('Foo', 'Bar'), $childDef->getAutowiringTypes());
  273. $parentDef = $container->getDefinition('parent');
  274. $this->assertSame(array('Foo'), $parentDef->getAutowiringTypes());
  275. }
  276. protected function process(ContainerBuilder $container)
  277. {
  278. $pass = new ResolveDefinitionTemplatesPass();
  279. $pass->process($container);
  280. }
  281. }