FormHelperTableLayoutTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Templating\Helper;
  11. use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
  12. use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser;
  13. use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
  14. use Symfony\Component\Form\Extension\Templating\TemplatingExtension;
  15. use Symfony\Component\Form\FormView;
  16. use Symfony\Component\Form\Tests\AbstractTableLayoutTest;
  17. use Symfony\Component\Templating\Loader\FilesystemLoader;
  18. use Symfony\Component\Templating\PhpEngine;
  19. class FormHelperTableLayoutTest extends AbstractTableLayoutTest
  20. {
  21. /**
  22. * @var PhpEngine
  23. */
  24. protected $engine;
  25. protected $testableFeatures = array(
  26. 'choice_attr',
  27. );
  28. public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
  29. {
  30. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  31. 'method' => 'get',
  32. 'action' => '',
  33. ));
  34. $html = $this->renderStart($form->createView());
  35. $this->assertSame('<form name="form" method="get">', $html);
  36. }
  37. public function testStartTagHasActionAttributeWhenActionIsZero()
  38. {
  39. $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
  40. 'method' => 'get',
  41. 'action' => '0',
  42. ));
  43. $html = $this->renderStart($form->createView());
  44. $this->assertSame('<form name="form" method="get" action="0">', $html);
  45. }
  46. protected function getExtensions()
  47. {
  48. // should be moved to the Form component once absolute file paths are supported
  49. // by the default name parser in the Templating component
  50. $reflClass = new \ReflectionClass('Symfony\Bundle\FrameworkBundle\FrameworkBundle');
  51. $root = realpath(\dirname($reflClass->getFileName()).'/Resources/views');
  52. $rootTheme = realpath(__DIR__.'/Resources');
  53. $templateNameParser = new StubTemplateNameParser($root, $rootTheme);
  54. $loader = new FilesystemLoader(array());
  55. $this->engine = new PhpEngine($templateNameParser, $loader);
  56. $this->engine->addGlobal('global', '');
  57. $this->engine->setHelpers(array(
  58. new TranslatorHelper(new StubTranslator()),
  59. ));
  60. return array_merge(parent::getExtensions(), array(
  61. new TemplatingExtension($this->engine, $this->csrfTokenManager, array(
  62. 'FrameworkBundle:Form',
  63. 'FrameworkBundle:FormTable',
  64. )),
  65. ));
  66. }
  67. protected function tearDown()
  68. {
  69. $this->engine = null;
  70. parent::tearDown();
  71. }
  72. protected function renderForm(FormView $view, array $vars = array())
  73. {
  74. return (string) $this->engine->get('form')->form($view, $vars);
  75. }
  76. protected function renderEnctype(FormView $view)
  77. {
  78. if (!method_exists($form = $this->engine->get('form'), 'enctype')) {
  79. $this->markTestSkipped(sprintf('Deprecated method %s->enctype() is not implemented.', \get_class($form)));
  80. }
  81. return (string) $form->enctype($view);
  82. }
  83. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  84. {
  85. return (string) $this->engine->get('form')->label($view, $label, $vars);
  86. }
  87. protected function renderErrors(FormView $view)
  88. {
  89. return (string) $this->engine->get('form')->errors($view);
  90. }
  91. protected function renderWidget(FormView $view, array $vars = array())
  92. {
  93. return (string) $this->engine->get('form')->widget($view, $vars);
  94. }
  95. protected function renderRow(FormView $view, array $vars = array())
  96. {
  97. return (string) $this->engine->get('form')->row($view, $vars);
  98. }
  99. protected function renderRest(FormView $view, array $vars = array())
  100. {
  101. return (string) $this->engine->get('form')->rest($view, $vars);
  102. }
  103. protected function renderStart(FormView $view, array $vars = array())
  104. {
  105. return (string) $this->engine->get('form')->start($view, $vars);
  106. }
  107. protected function renderEnd(FormView $view, array $vars = array())
  108. {
  109. return (string) $this->engine->get('form')->end($view, $vars);
  110. }
  111. protected function setTheme(FormView $view, array $themes)
  112. {
  113. $this->engine->get('form')->setTheme($view, $themes);
  114. }
  115. }