FormPassTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\Form\AbstractType;
  17. /**
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. class FormPassTest extends TestCase
  21. {
  22. public function testDoNothingIfFormExtensionNotLoaded()
  23. {
  24. $container = new ContainerBuilder();
  25. $container->addCompilerPass(new FormPass());
  26. $container->compile();
  27. $this->assertFalse($container->hasDefinition('form.extension'));
  28. }
  29. public function testAddTaggedTypes()
  30. {
  31. $container = new ContainerBuilder();
  32. $container->addCompilerPass(new FormPass());
  33. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
  34. $extDefinition->setArguments(array(
  35. new Reference('service_container'),
  36. array(),
  37. array(),
  38. array(),
  39. ));
  40. $definition1 = new Definition(__CLASS__.'_Type1');
  41. $definition1->addTag('form.type');
  42. $definition2 = new Definition(__CLASS__.'_Type2');
  43. $definition2->addTag('form.type');
  44. $container->setDefinition('form.extension', $extDefinition);
  45. $container->setDefinition('my.type1', $definition1);
  46. $container->setDefinition('my.type2', $definition2);
  47. $container->compile();
  48. $extDefinition = $container->getDefinition('form.extension');
  49. $this->assertEquals(array(
  50. // As of Symfony 2.8, the class is used to look up types
  51. __CLASS__.'_Type1' => 'my.type1',
  52. __CLASS__.'_Type2' => 'my.type2',
  53. // Before Symfony 2.8, the service ID was used as default alias
  54. 'my.type1' => 'my.type1',
  55. 'my.type2' => 'my.type2',
  56. ), $extDefinition->getArgument(1));
  57. }
  58. /**
  59. * @group legacy
  60. */
  61. public function testUseCustomAliasIfSet()
  62. {
  63. $container = new ContainerBuilder();
  64. $container->addCompilerPass(new FormPass());
  65. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
  66. $extDefinition->setArguments(array(
  67. new Reference('service_container'),
  68. array(),
  69. array(),
  70. array(),
  71. ));
  72. $definition1 = new Definition(__CLASS__.'_Type1');
  73. $definition1->addTag('form.type', array('alias' => 'mytype1'));
  74. $definition2 = new Definition(__CLASS__.'_Type2');
  75. $definition2->addTag('form.type', array('alias' => 'mytype2'));
  76. $container->setDefinition('form.extension', $extDefinition);
  77. $container->setDefinition('my.type1', $definition1);
  78. $container->setDefinition('my.type2', $definition2);
  79. $container->compile();
  80. $extDefinition = $container->getDefinition('form.extension');
  81. $this->assertEquals(array(
  82. __CLASS__.'_Type1' => 'my.type1',
  83. __CLASS__.'_Type2' => 'my.type2',
  84. 'mytype1' => 'my.type1',
  85. 'mytype2' => 'my.type2',
  86. ), $extDefinition->getArgument(1));
  87. }
  88. public function testAddTaggedTypeExtensions()
  89. {
  90. $container = new ContainerBuilder();
  91. $container->addCompilerPass(new FormPass());
  92. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension', array(
  93. new Reference('service_container'),
  94. array(),
  95. array(),
  96. array(),
  97. ));
  98. $container->setDefinition('form.extension', $extDefinition);
  99. $container->register('my.type_extension1', 'stdClass')
  100. ->addTag('form.type_extension', array('extended_type' => 'type1'));
  101. $container->register('my.type_extension2', 'stdClass')
  102. ->addTag('form.type_extension', array('extended_type' => 'type1'));
  103. $container->register('my.type_extension3', 'stdClass')
  104. ->addTag('form.type_extension', array('extended_type' => 'type2'));
  105. $container->compile();
  106. $extDefinition = $container->getDefinition('form.extension');
  107. $this->assertSame(array(
  108. 'type1' => array(
  109. 'my.type_extension1',
  110. 'my.type_extension2',
  111. ),
  112. 'type2' => array(
  113. 'my.type_extension3',
  114. ),
  115. ), $extDefinition->getArgument(2));
  116. }
  117. /**
  118. * @group legacy
  119. */
  120. public function testAliasOptionForTaggedTypeExtensions()
  121. {
  122. $container = new ContainerBuilder();
  123. $container->addCompilerPass(new FormPass());
  124. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension', array(
  125. new Reference('service_container'),
  126. array(),
  127. array(),
  128. array(),
  129. ));
  130. $container->setDefinition('form.extension', $extDefinition);
  131. $container->register('my.type_extension1', 'stdClass')
  132. ->addTag('form.type_extension', array('alias' => 'type1'));
  133. $container->register('my.type_extension2', 'stdClass')
  134. ->addTag('form.type_extension', array('alias' => 'type2'));
  135. $container->compile();
  136. $extDefinition = $container->getDefinition('form.extension');
  137. $this->assertSame(array(
  138. 'type1' => array(
  139. 'my.type_extension1',
  140. ),
  141. 'type2' => array(
  142. 'my.type_extension2',
  143. ),
  144. ), $extDefinition->getArgument(2));
  145. }
  146. public function testAddTaggedGuessers()
  147. {
  148. $container = new ContainerBuilder();
  149. $container->addCompilerPass(new FormPass());
  150. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
  151. $extDefinition->setArguments(array(
  152. new Reference('service_container'),
  153. array(),
  154. array(),
  155. array(),
  156. ));
  157. $definition1 = new Definition('stdClass');
  158. $definition1->addTag('form.type_guesser');
  159. $definition2 = new Definition('stdClass');
  160. $definition2->addTag('form.type_guesser');
  161. $container->setDefinition('form.extension', $extDefinition);
  162. $container->setDefinition('my.guesser1', $definition1);
  163. $container->setDefinition('my.guesser2', $definition2);
  164. $container->compile();
  165. $extDefinition = $container->getDefinition('form.extension');
  166. $this->assertSame(array(
  167. 'my.guesser1',
  168. 'my.guesser2',
  169. ), $extDefinition->getArgument(3));
  170. }
  171. /**
  172. * @dataProvider privateTaggedServicesProvider
  173. */
  174. public function testPrivateTaggedServices($id, $tagName, $expectedExceptionMessage)
  175. {
  176. $container = new ContainerBuilder();
  177. $container->addCompilerPass(new FormPass());
  178. $extDefinition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
  179. $extDefinition->setArguments(array(
  180. new Reference('service_container'),
  181. array(),
  182. array(),
  183. array(),
  184. ));
  185. $container->setDefinition('form.extension', $extDefinition);
  186. $container->register($id, 'stdClass')->setPublic(false)->addTag($tagName);
  187. if (method_exists($this, 'expectException')) {
  188. $this->expectException('InvalidArgumentException');
  189. $this->expectExceptionMessage($expectedExceptionMessage);
  190. } else {
  191. $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
  192. }
  193. $container->compile();
  194. }
  195. public function privateTaggedServicesProvider()
  196. {
  197. return array(
  198. array('my.type', 'form.type', 'The service "my.type" must be public as form types are lazy-loaded'),
  199. array('my.type_extension', 'form.type_extension', 'The service "my.type_extension" must be public as form type extensions are lazy-loaded'),
  200. array('my.guesser', 'form.type_guesser', 'The service "my.guesser" must be public as form type guessers are lazy-loaded'),
  201. );
  202. }
  203. }
  204. class FormPassTest_Type1 extends AbstractType
  205. {
  206. }
  207. class FormPassTest_Type2 extends AbstractType
  208. {
  209. }