FormHelperDivLayoutTest.php 5.2 KB

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