FormExtensionDivLayoutTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Tests\Bridge\Twig\Extension;
  11. require_once __DIR__.'/Fixtures/StubTranslator.php';
  12. require_once __DIR__.'/Fixtures/StubFilesystemLoader.php';
  13. use Symfony\Component\Form\FormView;
  14. use Symfony\Bridge\Twig\Extension\FormExtension;
  15. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  16. use Symfony\Tests\Component\Form\AbstractDivLayoutTest;
  17. use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubTranslator;
  18. use Symfony\Tests\Bridge\Twig\Extension\Fixtures\StubFilesystemLoader;
  19. class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
  20. {
  21. protected function setUp()
  22. {
  23. if (!class_exists('Twig_Environment')) {
  24. $this->markTestSkipped('Twig is not available.');
  25. }
  26. parent::setUp();
  27. $loader = new StubFilesystemLoader(array(
  28. __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
  29. __DIR__,
  30. ));
  31. $this->extension = new FormExtension(array(
  32. 'form_div_layout.html.twig',
  33. 'custom_widgets.html.twig',
  34. ));
  35. $environment = new \Twig_Environment($loader);
  36. $environment->addExtension($this->extension);
  37. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  38. $this->extension->initRuntime($environment);
  39. }
  40. protected function tearDown()
  41. {
  42. parent::tearDown();
  43. $this->extension = null;
  44. }
  45. public function testThemeBlockInheritance()
  46. {
  47. $view = $this->factory
  48. ->createNamed('email', 'name')
  49. ->createView()
  50. ;
  51. $this->extension->setTheme($view, array('theme.html.twig'));
  52. $this->assertMatchesXpath(
  53. $this->renderWidget($view),
  54. '/input[@type="email"][@rel="theme"]'
  55. );
  56. }
  57. public function testThemeBlockInheritanceUsingUse()
  58. {
  59. $view = $this->factory
  60. ->createNamed('email', 'name')
  61. ->createView()
  62. ;
  63. $this->extension->setTheme($view, array('theme_use.html.twig'));
  64. $this->assertMatchesXpath(
  65. $this->renderWidget($view),
  66. '/input[@type="email"][@rel="theme"]'
  67. );
  68. }
  69. public function testThemeBlockInheritanceUsingExtend()
  70. {
  71. $view = $this->factory
  72. ->createNamed('email', 'name')
  73. ->createView()
  74. ;
  75. $this->extension->setTheme($view, array('theme_extends.html.twig'));
  76. $this->assertMatchesXpath(
  77. $this->renderWidget($view),
  78. '/input[@type="email"][@rel="theme"]'
  79. );
  80. }
  81. public function testThemeInheritance()
  82. {
  83. $child = $this->factory->createNamedBuilder('form', 'child')
  84. ->add('field', 'text')
  85. ->getForm();
  86. $view = $this->factory->createNamedBuilder('form', 'parent')
  87. ->add('field', 'text')
  88. ->getForm()
  89. ->add($child)
  90. ->createView()
  91. ;
  92. $this->extension->setTheme($view, array('parent_label.html.twig'));
  93. $this->extension->setTheme($view['child'], array('child_label.html.twig'));
  94. $this->assertWidgetMatchesXpath($view, array(),
  95. '/div
  96. [
  97. ./input[@type="hidden"]
  98. /following-sibling::div
  99. [
  100. ./label[.="parent"]
  101. /following-sibling::input[@type="text"]
  102. ]
  103. /following-sibling::div
  104. [
  105. ./label
  106. /following-sibling::div
  107. [
  108. ./div
  109. [
  110. ./label[.="child"]
  111. /following-sibling::input[@type="text"]
  112. ]
  113. ]
  114. ]
  115. ]
  116. '
  117. );
  118. }
  119. protected function renderEnctype(FormView $view)
  120. {
  121. return (string)$this->extension->renderEnctype($view);
  122. }
  123. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  124. {
  125. return (string)$this->extension->renderLabel($view, $label, $vars);
  126. }
  127. protected function renderErrors(FormView $view)
  128. {
  129. return (string)$this->extension->renderErrors($view);
  130. }
  131. protected function renderWidget(FormView $view, array $vars = array())
  132. {
  133. return (string)$this->extension->renderWidget($view, $vars);
  134. }
  135. protected function renderRow(FormView $view, array $vars = array())
  136. {
  137. return (string)$this->extension->renderRow($view, $vars);
  138. }
  139. protected function renderRest(FormView $view, array $vars = array())
  140. {
  141. return (string)$this->extension->renderRest($view, $vars);
  142. }
  143. }