BaseWidgetTest.php 4.8 KB

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