BaseWidgetTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //this is ugly workaround for different build strategies and, possibly,
  77. //different TwigBridge installation directories
  78. $twigPaths = array_filter(array(
  79. __DIR__.'/../../../vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form',
  80. __DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form',
  81. __DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
  82. __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
  83. ), 'is_dir');
  84. $twigPaths[] = __DIR__.'/../../../Resources/views/Form';
  85. $loader = new StubFilesystemLoader($twigPaths);
  86. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
  87. $this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
  88. $this->environment->addExtension(new TranslationExtension(new StubTranslator()));
  89. $this->environment->addExtension($this->extension);
  90. $this->extension->initRuntime($this->environment);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function tearDown()
  96. {
  97. parent::tearDown();
  98. $this->extension = null;
  99. }
  100. protected function getSonataAdmin()
  101. {
  102. return $this->sonataAdmin;
  103. }
  104. /**
  105. * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
  106. *
  107. * @param FormView $view
  108. * @param array $vars
  109. *
  110. * @return string
  111. */
  112. protected function renderWidget(FormView $view, array $vars = array())
  113. {
  114. return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
  115. }
  116. /**
  117. * Helper method to strip newline and space characters from html string to make comparing easier.
  118. *
  119. * @param string $html
  120. *
  121. * @return string
  122. */
  123. protected function cleanHtmlWhitespace($html)
  124. {
  125. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  126. return '>'.trim($value[1]).'<';
  127. }, $html);
  128. return $html;
  129. }
  130. }