FormExtensionDivLayoutTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\Bridge\Twig\Tests\Extension;
  11. use Symfony\Bridge\Twig\Extension\FormExtension;
  12. use Symfony\Bridge\Twig\Form\TwigRenderer;
  13. use Symfony\Bridge\Twig\Form\TwigRendererEngine;
  14. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  15. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
  16. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  17. use Symfony\Component\Form\ChoiceList\View\ChoiceView;
  18. use Symfony\Component\Form\FormView;
  19. use Symfony\Component\Form\Tests\AbstractDivLayoutTest;
  20. use Twig\Environment;
  21. class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
  22. {
  23. use RuntimeLoaderProvider;
  24. private $renderer;
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $loader = new StubFilesystemLoader(array(
  29. __DIR__.'/../../Resources/views/Form',
  30. __DIR__.'/Fixtures/templates/form',
  31. ));
  32. $environment = new Environment($loader, array('strict_variables' => true));
  33. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  34. $environment->addGlobal('global', '');
  35. // the value can be any template that exists
  36. $environment->addGlobal('dynamic_template_name', 'child_label');
  37. $environment->addExtension(new FormExtension());
  38. $rendererEngine = new TwigRendererEngine(array(
  39. 'form_div_layout.html.twig',
  40. 'custom_widgets.html.twig',
  41. ), $environment);
  42. $this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
  43. $this->registerTwigRuntimeLoader($environment, $this->renderer);
  44. }
  45. public function testThemeBlockInheritanceUsingUse()
  46. {
  47. $view = $this->factory
  48. ->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\EmailType')
  49. ->createView()
  50. ;
  51. $this->setTheme($view, array('theme_use.html.twig'));
  52. $this->assertMatchesXpath(
  53. $this->renderWidget($view),
  54. '/input[@type="email"][@rel="theme"]'
  55. );
  56. }
  57. public function testThemeBlockInheritanceUsingExtend()
  58. {
  59. $view = $this->factory
  60. ->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\EmailType')
  61. ->createView()
  62. ;
  63. $this->setTheme($view, array('theme_extends.html.twig'));
  64. $this->assertMatchesXpath(
  65. $this->renderWidget($view),
  66. '/input[@type="email"][@rel="theme"]'
  67. );
  68. }
  69. public function testThemeBlockInheritanceUsingDynamicExtend()
  70. {
  71. $view = $this->factory
  72. ->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\EmailType')
  73. ->createView()
  74. ;
  75. $this->renderer->setTheme($view, array('page_dynamic_extends.html.twig'));
  76. $this->assertMatchesXpath(
  77. $this->renderer->searchAndRenderBlock($view, 'row'),
  78. '/div/label[text()="child"]'
  79. );
  80. }
  81. public function isSelectedChoiceProvider()
  82. {
  83. return array(
  84. array(true, '0', '0'),
  85. array(true, '1', '1'),
  86. array(true, '', ''),
  87. array(true, '1.23', '1.23'),
  88. array(true, 'foo', 'foo'),
  89. array(true, 'foo10', 'foo10'),
  90. array(true, 'foo', array(1, 'foo', 'foo10')),
  91. array(false, 10, array(1, 'foo', 'foo10')),
  92. array(false, 0, array(1, 'foo', 'foo10')),
  93. );
  94. }
  95. /**
  96. * @dataProvider isSelectedChoiceProvider
  97. */
  98. public function testIsChoiceSelected($expected, $choice, $value)
  99. {
  100. $choice = new ChoiceView($choice, $choice, $choice.' label');
  101. $this->assertSame($expected, \Symfony\Bridge\Twig\Extension\twig_is_selected_choice($choice, $value));
  102. }
  103. public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
  104. {
  105. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  106. 'method' => 'get',
  107. 'action' => '',
  108. ));
  109. $html = $this->renderStart($form->createView());
  110. $this->assertSame('<form name="form" method="get">', $html);
  111. }
  112. public function testStartTagHasActionAttributeWhenActionIsZero()
  113. {
  114. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  115. 'method' => 'get',
  116. 'action' => '0',
  117. ));
  118. $html = $this->renderStart($form->createView());
  119. $this->assertSame('<form name="form" method="get" action="0">', $html);
  120. }
  121. protected function renderForm(FormView $view, array $vars = array())
  122. {
  123. return (string) $this->renderer->renderBlock($view, 'form', $vars);
  124. }
  125. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  126. {
  127. if ($label !== null) {
  128. $vars += array('label' => $label);
  129. }
  130. return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
  131. }
  132. protected function renderErrors(FormView $view)
  133. {
  134. return (string) $this->renderer->searchAndRenderBlock($view, 'errors');
  135. }
  136. protected function renderWidget(FormView $view, array $vars = array())
  137. {
  138. return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
  139. }
  140. protected function renderRow(FormView $view, array $vars = array())
  141. {
  142. return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
  143. }
  144. protected function renderRest(FormView $view, array $vars = array())
  145. {
  146. return (string) $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
  147. }
  148. protected function renderStart(FormView $view, array $vars = array())
  149. {
  150. return (string) $this->renderer->renderBlock($view, 'form_start', $vars);
  151. }
  152. protected function renderEnd(FormView $view, array $vars = array())
  153. {
  154. return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
  155. }
  156. protected function setTheme(FormView $view, array $themes)
  157. {
  158. $this->renderer->setTheme($view, $themes);
  159. }
  160. public static function themeBlockInheritanceProvider()
  161. {
  162. return array(
  163. array(array('theme.html.twig')),
  164. );
  165. }
  166. public static function themeInheritanceProvider()
  167. {
  168. return array(
  169. array(array('parent_label.html.twig'), array('child_label.html.twig')),
  170. );
  171. }
  172. }