IntegrationTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\Alias;
  14. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. /**
  18. * This class tests the integration of the different compiler passes.
  19. */
  20. class IntegrationTest extends TestCase
  21. {
  22. /**
  23. * This tests that dependencies are correctly processed.
  24. *
  25. * We're checking that:
  26. *
  27. * * A is public, B/C are private
  28. * * A -> C
  29. * * B -> C
  30. */
  31. public function testProcessRemovesAndInlinesRecursively()
  32. {
  33. $container = new ContainerBuilder();
  34. $container->setResourceTracking(false);
  35. $a = $container
  36. ->register('a', '\stdClass')
  37. ->addArgument(new Reference('c'))
  38. ;
  39. $b = $container
  40. ->register('b', '\stdClass')
  41. ->addArgument(new Reference('c'))
  42. ->setPublic(false)
  43. ;
  44. $c = $container
  45. ->register('c', '\stdClass')
  46. ->setPublic(false)
  47. ;
  48. $container->compile();
  49. $this->assertTrue($container->hasDefinition('a'));
  50. $arguments = $a->getArguments();
  51. $this->assertSame($c, $arguments[0]);
  52. $this->assertFalse($container->hasDefinition('b'));
  53. $this->assertFalse($container->hasDefinition('c'));
  54. }
  55. public function testProcessInlinesReferencesToAliases()
  56. {
  57. $container = new ContainerBuilder();
  58. $container->setResourceTracking(false);
  59. $a = $container
  60. ->register('a', '\stdClass')
  61. ->addArgument(new Reference('b'))
  62. ;
  63. $container->setAlias('b', new Alias('c', false));
  64. $c = $container
  65. ->register('c', '\stdClass')
  66. ->setPublic(false)
  67. ;
  68. $container->compile();
  69. $this->assertTrue($container->hasDefinition('a'));
  70. $arguments = $a->getArguments();
  71. $this->assertSame($c, $arguments[0]);
  72. $this->assertFalse($container->hasAlias('b'));
  73. $this->assertFalse($container->hasDefinition('c'));
  74. }
  75. public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDefinition()
  76. {
  77. $container = new ContainerBuilder();
  78. $container->setResourceTracking(false);
  79. $container
  80. ->register('a', '\stdClass')
  81. ->addArgument(new Reference('b'))
  82. ->addMethodCall('setC', array(new Reference('c')))
  83. ;
  84. $container
  85. ->register('b', '\stdClass')
  86. ->addArgument(new Reference('c'))
  87. ->setPublic(false)
  88. ;
  89. $container
  90. ->register('c', '\stdClass')
  91. ->setPublic(false)
  92. ;
  93. $container->compile();
  94. $this->assertTrue($container->hasDefinition('a'));
  95. $this->assertFalse($container->hasDefinition('b'));
  96. $this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
  97. }
  98. /**
  99. * @dataProvider getYamlCompileTests
  100. */
  101. public function testYamlContainerCompiles($directory, $actualServiceId, $expectedServiceId, ContainerBuilder $mainContainer = null)
  102. {
  103. // allow a container to be passed in, which might have autoconfigure settings
  104. $container = $mainContainer ? $mainContainer : new ContainerBuilder();
  105. $container->setResourceTracking(false);
  106. $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/yaml/integration/'.$directory));
  107. $loader->load('main.yml');
  108. $container->compile();
  109. $actualService = $container->getDefinition($actualServiceId);
  110. // create a fresh ContainerBuilder, to avoid autoconfigure stuff
  111. $container = new ContainerBuilder();
  112. $container->setResourceTracking(false);
  113. $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/yaml/integration/'.$directory));
  114. $loader->load('expected.yml');
  115. $container->compile();
  116. $expectedService = $container->getDefinition($expectedServiceId);
  117. // reset changes, we don't care if these differ
  118. $actualService->setChanges(array());
  119. $expectedService->setChanges(array());
  120. $this->assertEquals($expectedService, $actualService);
  121. }
  122. public function getYamlCompileTests()
  123. {
  124. $container = new ContainerBuilder();
  125. $container->registerForAutoconfiguration(IntegrationTestStub::class);
  126. yield array(
  127. 'autoconfigure_child_not_applied',
  128. 'child_service',
  129. 'child_service_expected',
  130. $container,
  131. );
  132. $container = new ContainerBuilder();
  133. $container->registerForAutoconfiguration(IntegrationTestStub::class);
  134. yield array(
  135. 'autoconfigure_parent_child',
  136. 'child_service',
  137. 'child_service_expected',
  138. $container,
  139. );
  140. $container = new ContainerBuilder();
  141. $container->registerForAutoconfiguration(IntegrationTestStub::class)
  142. ->addTag('from_autoconfigure');
  143. yield array(
  144. 'autoconfigure_parent_child_tags',
  145. 'child_service',
  146. 'child_service_expected',
  147. $container,
  148. );
  149. yield array(
  150. 'child_parent',
  151. 'child_service',
  152. 'child_service_expected',
  153. );
  154. yield array(
  155. 'defaults_child_tags',
  156. 'child_service',
  157. 'child_service_expected',
  158. );
  159. yield array(
  160. 'defaults_instanceof_importance',
  161. 'main_service',
  162. 'main_service_expected',
  163. );
  164. yield array(
  165. 'defaults_parent_child',
  166. 'child_service',
  167. 'child_service_expected',
  168. );
  169. yield array(
  170. 'instanceof_parent_child',
  171. 'child_service',
  172. 'child_service_expected',
  173. );
  174. }
  175. }
  176. class IntegrationTestStub extends IntegrationTestStubParent
  177. {
  178. }
  179. class IntegrationTestStubParent
  180. {
  181. public function enableSummer($enable)
  182. {
  183. // methods used in calls - added here to prevent errors for not existing
  184. }
  185. public function setSunshine($type)
  186. {
  187. }
  188. }