BaseWidgetTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * Current template type, form or filter.
  35. *
  36. * @var string
  37. */
  38. protected $type = null;
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. if (!in_array($this->type, array('form', 'filter'))) {
  46. throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
  47. }
  48. $rendererEngine = new TwigRendererEngine(array(
  49. $this->type.'_admin_fields.html.twig',
  50. ));
  51. if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
  52. $csrfManagerClass = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface';
  53. } else {
  54. $csrfManagerClass = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
  55. }
  56. $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
  57. $this->extension = new FormExtension($renderer);
  58. $twigPaths = array(__DIR__.'/../../../Resources/views/Form');
  59. //this is ugly workaround for different build strategies and, possibly,
  60. //different TwigBridge installation directories
  61. if (is_dir(__DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form')) {
  62. $twigPaths[] = __DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form';
  63. } elseif (is_dir(__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) {
  64. $twigPaths[] = __DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
  65. } else {
  66. $twigPaths[] = __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
  67. }
  68. $loader = new StubFilesystemLoader($twigPaths);
  69. $environment = new \Twig_Environment($loader, array('strict_variables' => true));
  70. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  71. $environment->addExtension($this->extension);
  72. $this->extension->initRuntime($environment);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function tearDown()
  78. {
  79. parent::tearDown();
  80. $this->extension = null;
  81. }
  82. /**
  83. * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
  84. *
  85. * @param FormView $view
  86. * @param array $vars
  87. *
  88. * @return string
  89. */
  90. protected function renderWidget(FormView $view, array $vars = array())
  91. {
  92. $sonataAdmin = $sonataAdmin = array(
  93. 'name' => null,
  94. 'admin' => null,
  95. 'value' => null,
  96. 'edit' => 'standard',
  97. 'inline' => 'natural',
  98. 'field_description' => null,
  99. 'block_name' => false,
  100. 'options' => array(),
  101. );
  102. $vars = array_merge(array(
  103. 'sonata_admin' => $sonataAdmin,
  104. ), $vars);
  105. return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
  106. }
  107. /**
  108. * Helper method to strip newline and space characters from html string to make comparing easier.
  109. *
  110. * @param string $html
  111. *
  112. * @return string
  113. */
  114. protected function cleanHtmlWhitespace($html)
  115. {
  116. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  117. return '>'.trim($value[1]).'<';
  118. }, $html);
  119. return $html;
  120. }
  121. }