FormExtensionBootstrap3LayoutTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\FormView;
  18. use Symfony\Component\Form\Tests\AbstractBootstrap3LayoutTest;
  19. use Twig\Environment;
  20. class FormExtensionBootstrap3LayoutTest extends AbstractBootstrap3LayoutTest
  21. {
  22. use RuntimeLoaderProvider;
  23. private $renderer;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $loader = new StubFilesystemLoader(array(
  28. __DIR__.'/../../Resources/views/Form',
  29. __DIR__.'/Fixtures/templates/form',
  30. ));
  31. $environment = new Environment($loader, array('strict_variables' => true));
  32. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  33. $environment->addExtension(new FormExtension());
  34. $rendererEngine = new TwigRendererEngine(array(
  35. 'bootstrap_3_layout.html.twig',
  36. 'custom_widgets.html.twig',
  37. ), $environment);
  38. $this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
  39. $this->registerTwigRuntimeLoader($environment, $this->renderer);
  40. }
  41. public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
  42. {
  43. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  44. 'method' => 'get',
  45. 'action' => '',
  46. ));
  47. $html = $this->renderStart($form->createView());
  48. $this->assertSame('<form name="form" method="get">', $html);
  49. }
  50. public function testStartTagHasActionAttributeWhenActionIsZero()
  51. {
  52. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  53. 'method' => 'get',
  54. 'action' => '0',
  55. ));
  56. $html = $this->renderStart($form->createView());
  57. $this->assertSame('<form name="form" method="get" action="0">', $html);
  58. }
  59. protected function renderForm(FormView $view, array $vars = array())
  60. {
  61. return (string) $this->renderer->renderBlock($view, 'form', $vars);
  62. }
  63. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  64. {
  65. if ($label !== null) {
  66. $vars += array('label' => $label);
  67. }
  68. return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
  69. }
  70. protected function renderErrors(FormView $view)
  71. {
  72. return (string) $this->renderer->searchAndRenderBlock($view, 'errors');
  73. }
  74. protected function renderWidget(FormView $view, array $vars = array())
  75. {
  76. return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
  77. }
  78. protected function renderRow(FormView $view, array $vars = array())
  79. {
  80. return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
  81. }
  82. protected function renderRest(FormView $view, array $vars = array())
  83. {
  84. return (string) $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
  85. }
  86. protected function renderStart(FormView $view, array $vars = array())
  87. {
  88. return (string) $this->renderer->renderBlock($view, 'form_start', $vars);
  89. }
  90. protected function renderEnd(FormView $view, array $vars = array())
  91. {
  92. return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
  93. }
  94. protected function setTheme(FormView $view, array $themes)
  95. {
  96. $this->renderer->setTheme($view, $themes);
  97. }
  98. }