FormExtensionDivLayoutTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 $extension;
  22. protected function setUp()
  23. {
  24. if (!class_exists('Twig_Environment')) {
  25. $this->markTestSkipped('Twig is not available.');
  26. }
  27. parent::setUp();
  28. $loader = new StubFilesystemLoader(array(
  29. __DIR__.'/../../../../../../src/Symfony/Bridge/Twig/Resources/views/Form',
  30. __DIR__,
  31. ));
  32. $this->extension = new FormExtension(array(
  33. 'form_div_layout.html.twig',
  34. 'custom_widgets.html.twig',
  35. ));
  36. $environment = new \Twig_Environment($loader);
  37. $environment->addExtension($this->extension);
  38. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  39. $this->extension->initRuntime($environment);
  40. }
  41. protected function tearDown()
  42. {
  43. parent::tearDown();
  44. $this->extension = null;
  45. }
  46. public function testThemeBlockInheritanceUsingUse()
  47. {
  48. $view = $this->factory
  49. ->createNamed('email', 'name')
  50. ->createView()
  51. ;
  52. $this->setTheme($view, array('theme_use.html.twig'));
  53. $this->assertMatchesXpath(
  54. $this->renderWidget($view),
  55. '/input[@type="email"][@rel="theme"]'
  56. );
  57. }
  58. public function testThemeBlockInheritanceUsingExtend()
  59. {
  60. $view = $this->factory
  61. ->createNamed('email', 'name')
  62. ->createView()
  63. ;
  64. $this->setTheme($view, array('theme_extends.html.twig'));
  65. $this->assertMatchesXpath(
  66. $this->renderWidget($view),
  67. '/input[@type="email"][@rel="theme"]'
  68. );
  69. }
  70. protected function renderEnctype(FormView $view)
  71. {
  72. return (string) $this->extension->renderEnctype($view);
  73. }
  74. protected function renderLabel(FormView $view, $label = null, array $vars = array())
  75. {
  76. return (string) $this->extension->renderLabel($view, $label, $vars);
  77. }
  78. protected function renderErrors(FormView $view)
  79. {
  80. return (string) $this->extension->renderErrors($view);
  81. }
  82. protected function renderWidget(FormView $view, array $vars = array())
  83. {
  84. return (string) $this->extension->renderWidget($view, $vars);
  85. }
  86. protected function renderRow(FormView $view, array $vars = array())
  87. {
  88. return (string) $this->extension->renderRow($view, $vars);
  89. }
  90. protected function renderRest(FormView $view, array $vars = array())
  91. {
  92. return (string) $this->extension->renderRest($view, $vars);
  93. }
  94. protected function setTheme(FormView $view, array $themes)
  95. {
  96. $this->extension->setTheme($view, $themes);
  97. }
  98. public static function themeBlockInheritanceProvider()
  99. {
  100. return array(
  101. array(array('theme.html.twig'))
  102. );
  103. }
  104. public static function themeInheritanceProvider()
  105. {
  106. return array(
  107. array(array('parent_label.html.twig'), array('child_label.html.twig'))
  108. );
  109. }
  110. }