FormExtensionTableLayoutTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Component\Form\FormView;
  12. use Symfony\Bridge\Twig\Form\TwigRenderer;
  13. use Symfony\Bridge\Twig\Form\TwigRendererEngine;
  14. use Symfony\Bridge\Twig\Extension\FormExtension;
  15. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  16. use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
  17. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
  18. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  19. use Twig\Environment;
  20. class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
  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->addGlobal('global', '');
  34. $environment->addExtension(new FormExtension());
  35. $rendererEngine = new TwigRendererEngine(array(
  36. 'form_table_layout.html.twig',
  37. 'custom_widgets.html.twig',
  38. ), $environment);
  39. $this->renderer = new TwigRenderer($rendererEngine, $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock());
  40. $this->registerTwigRuntimeLoader($environment, $this->renderer);
  41. }
  42. public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
  43. {
  44. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  45. 'method' => 'get',
  46. 'action' => '',
  47. ));
  48. $html = $this->renderStart($form->createView());
  49. $this->assertSame('<form name="form" method="get">', $html);
  50. }
  51. public function testStartTagHasActionAttributeWhenActionIsZero()
  52. {
  53. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  54. 'method' => 'get',
  55. 'action' => '0',
  56. ));
  57. $html = $this->renderStart($form->createView());
  58. $this->assertSame('<form name="form" method="get" action="0">', $html);
  59. }
  60. protected function renderForm(FormView $view, array $vars = array())
  61. {
  62. return (string) $this->renderer->renderBlock($view, 'form', $vars);
  63. }
  64. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  65. {
  66. if ($label !== null) {
  67. $vars += array('label' => $label);
  68. }
  69. return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
  70. }
  71. protected function renderErrors(FormView $view)
  72. {
  73. return (string) $this->renderer->searchAndRenderBlock($view, 'errors');
  74. }
  75. protected function renderWidget(FormView $view, array $vars = array())
  76. {
  77. return (string) $this->renderer->searchAndRenderBlock($view, 'widget', $vars);
  78. }
  79. protected function renderRow(FormView $view, array $vars = array())
  80. {
  81. return (string) $this->renderer->searchAndRenderBlock($view, 'row', $vars);
  82. }
  83. protected function renderRest(FormView $view, array $vars = array())
  84. {
  85. return (string) $this->renderer->searchAndRenderBlock($view, 'rest', $vars);
  86. }
  87. protected function renderStart(FormView $view, array $vars = array())
  88. {
  89. return (string) $this->renderer->renderBlock($view, 'form_start', $vars);
  90. }
  91. protected function renderEnd(FormView $view, array $vars = array())
  92. {
  93. return (string) $this->renderer->renderBlock($view, 'form_end', $vars);
  94. }
  95. protected function setTheme(FormView $view, array $themes)
  96. {
  97. $this->renderer->setTheme($view, $themes);
  98. }
  99. }