BaseWidgetTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Form\Widget;
  11. use Symfony\Bridge\Twig\Extension\FormExtension;
  12. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  13. use Symfony\Bridge\Twig\Form\TwigRenderer;
  14. use Symfony\Bridge\Twig\Form\TwigRendererEngine;
  15. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  16. use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
  17. use Symfony\Component\Form\FormView;
  18. use Symfony\Component\Form\Test\TypeTestCase;
  19. use Symfony\Component\HttpKernel\Kernel;
  20. /**
  21. * Class BaseWidgetTest.
  22. *
  23. * Base class for tests checking rendering of form widgets with form_admin_fields.html.twig and
  24. * filter_admin_fields.html.twig. Template to use is defined by $this->type variable, that needs to be overridden in
  25. * child classes.
  26. */
  27. abstract class BaseWidgetTest extends TypeTestCase
  28. {
  29. /**
  30. * @var FormExtension
  31. */
  32. protected $extension;
  33. /**
  34. * @var \Twig_Environment
  35. */
  36. protected $environment;
  37. /**
  38. * Current template type, form or filter.
  39. *
  40. * @var string
  41. */
  42. protected $type = null;
  43. /**
  44. * @var array
  45. */
  46. protected $sonataAdmin = array(
  47. 'name' => null,
  48. 'admin' => null,
  49. 'value' => null,
  50. 'edit' => 'standard',
  51. 'inline' => 'natural',
  52. 'field_description' => null,
  53. 'block_name' => false,
  54. 'options' => array(
  55. 'form_type' => 'vertical',
  56. 'use_icheck' => true,
  57. ),
  58. );
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function setUp()
  63. {
  64. parent::setUp();
  65. if (!in_array($this->type, array('form', 'filter'))) {
  66. throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
  67. }
  68. $rendererEngine = new TwigRendererEngine(array(
  69. $this->type.'_admin_fields.html.twig',
  70. ));
  71. if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
  72. $csrfManagerClass = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface';
  73. } else {
  74. $csrfManagerClass = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
  75. }
  76. $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
  77. $this->extension = new FormExtension($renderer);
  78. $twigPaths = array(__DIR__.'/../../../Resources/views/Form');
  79. //this is ugly workaround for different build strategies and, possibly,
  80. //different TwigBridge installation directories
  81. if (is_dir(__DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form')) {
  82. $twigPaths[] = __DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form';
  83. } elseif (is_dir(__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) {
  84. $twigPaths[] = __DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
  85. } else {
  86. $twigPaths[] = __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
  87. }
  88. $loader = new StubFilesystemLoader($twigPaths);
  89. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
  90. $this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
  91. $this->environment->addExtension(new TranslationExtension(new StubTranslator()));
  92. $this->environment->addExtension($this->extension);
  93. $this->extension->initRuntime($this->environment);
  94. }
  95. protected function getSonataAdmin()
  96. {
  97. return $this->sonataAdmin;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function tearDown()
  103. {
  104. parent::tearDown();
  105. $this->extension = null;
  106. }
  107. /**
  108. * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
  109. *
  110. * @param FormView $view
  111. * @param array $vars
  112. *
  113. * @return string
  114. */
  115. protected function renderWidget(FormView $view, array $vars = array())
  116. {
  117. return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
  118. }
  119. /**
  120. * Helper method to strip newline and space characters from html string to make comparing easier.
  121. *
  122. * @param string $html
  123. *
  124. * @return string
  125. */
  126. protected function cleanHtmlWhitespace($html)
  127. {
  128. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  129. return '>'.trim($value[1]).'<';
  130. }, $html);
  131. return $html;
  132. }
  133. }