BaseWidgetTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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->addGlobal('sonata_admin', $this->getSonataAdmin());
  71. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  72. $environment->addExtension($this->extension);
  73. $this->extension->initRuntime($environment);
  74. }
  75. protected function getSonataAdmin()
  76. {
  77. return array(
  78. 'name' => null,
  79. 'admin' => null,
  80. 'value' => null,
  81. 'edit' => 'standard',
  82. 'inline' => 'natural',
  83. 'field_description' => null,
  84. 'block_name' => false,
  85. 'options' => array(
  86. 'form_type' => 'vertical',
  87. ),
  88. );
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function tearDown()
  94. {
  95. parent::tearDown();
  96. $this->extension = null;
  97. }
  98. /**
  99. * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
  100. *
  101. * @param FormView $view
  102. * @param array $vars
  103. *
  104. * @return string
  105. */
  106. protected function renderWidget(FormView $view, array $vars = array())
  107. {
  108. return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
  109. }
  110. /**
  111. * Helper method to strip newline and space characters from html string to make comparing easier.
  112. *
  113. * @param string $html
  114. *
  115. * @return string
  116. */
  117. protected function cleanHtmlWhitespace($html)
  118. {
  119. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  120. return '>'.trim($value[1]).'<';
  121. }, $html);
  122. return $html;
  123. }
  124. }