|
@@ -11,14 +11,10 @@
|
|
|
|
|
|
namespace Sonata\AdminBundle\Tests\Form\Widget;
|
|
|
|
|
|
-use Symfony\Bridge\Twig\Extension\FormExtension;
|
|
|
+use Sonata\CoreBundle\Test\AbstractWidgetTestCase;
|
|
|
use Symfony\Bridge\Twig\Extension\TranslationExtension;
|
|
|
-use Symfony\Bridge\Twig\Form\TwigRenderer;
|
|
|
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
|
|
|
-use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
|
|
|
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
|
|
|
-use Symfony\Component\Form\FormView;
|
|
|
-use Symfony\Component\Form\Test\TypeTestCase;
|
|
|
|
|
|
/**
|
|
|
* Class BaseWidgetTest.
|
|
@@ -27,18 +23,8 @@ use Symfony\Component\Form\Test\TypeTestCase;
|
|
|
* filter_admin_fields.html.twig. Template to use is defined by $this->type variable, that needs to be overridden in
|
|
|
* child classes.
|
|
|
*/
|
|
|
-abstract class BaseWidgetTest extends TypeTestCase
|
|
|
+abstract class BaseWidgetTest extends AbstractWidgetTestCase
|
|
|
{
|
|
|
- /**
|
|
|
- * @var FormExtension
|
|
|
- */
|
|
|
- protected $extension;
|
|
|
-
|
|
|
- /**
|
|
|
- * @var \Twig_Environment
|
|
|
- */
|
|
|
- protected $environment;
|
|
|
-
|
|
|
/**
|
|
|
* Current template type, form or filter.
|
|
|
*
|
|
@@ -66,90 +52,44 @@ abstract class BaseWidgetTest extends TypeTestCase
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
- public function setUp()
|
|
|
+ protected function getEnvironment()
|
|
|
{
|
|
|
- parent::setUp();
|
|
|
+ $environment = parent::getEnvironment();
|
|
|
+ $environment->addGlobal('sonata_admin', $this->getSonataAdmin());
|
|
|
+ $environment->addExtension(new TranslationExtension(new StubTranslator()));
|
|
|
+
|
|
|
+ return $environment;
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ protected function getRenderingEngine()
|
|
|
+ {
|
|
|
if (!in_array($this->type, array('form', 'filter'))) {
|
|
|
throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
|
|
|
}
|
|
|
|
|
|
- $rendererEngine = new TwigRendererEngine(array(
|
|
|
+ return new TwigRendererEngine(array(
|
|
|
$this->type.'_admin_fields.html.twig',
|
|
|
));
|
|
|
-
|
|
|
- $csrfManagerClass =
|
|
|
- interface_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface') ?
|
|
|
- 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface' :
|
|
|
- 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
|
|
|
-
|
|
|
- $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
|
|
|
-
|
|
|
- $this->extension = new FormExtension($renderer);
|
|
|
-
|
|
|
- //this is ugly workaround for different build strategies and, possibly,
|
|
|
- //different TwigBridge installation directories
|
|
|
- $twigPaths = array_filter(array(
|
|
|
- __DIR__.'/../../../vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form',
|
|
|
- __DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form',
|
|
|
- __DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
|
|
|
- __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form',
|
|
|
- ), 'is_dir');
|
|
|
-
|
|
|
- $twigPaths[] = __DIR__.'/../../../Resources/views/Form';
|
|
|
-
|
|
|
- $loader = new StubFilesystemLoader($twigPaths);
|
|
|
-
|
|
|
- $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
|
|
|
- $this->environment->addGlobal('sonata_admin', $this->getSonataAdmin());
|
|
|
- $this->environment->addExtension(new TranslationExtension(new StubTranslator()));
|
|
|
-
|
|
|
- $this->environment->addExtension($this->extension);
|
|
|
-
|
|
|
- $this->extension->initRuntime($this->environment);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
- public function tearDown()
|
|
|
- {
|
|
|
- parent::tearDown();
|
|
|
-
|
|
|
- $this->extension = null;
|
|
|
- }
|
|
|
-
|
|
|
protected function getSonataAdmin()
|
|
|
{
|
|
|
return $this->sonataAdmin;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML.
|
|
|
- *
|
|
|
- * @param FormView $view
|
|
|
- * @param array $vars
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- protected function renderWidget(FormView $view, array $vars = array())
|
|
|
- {
|
|
|
- return (string) $this->extension->renderer->searchAndRenderBlock($view, 'widget', $vars);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Helper method to strip newline and space characters from html string to make comparing easier.
|
|
|
- *
|
|
|
- * @param string $html
|
|
|
- *
|
|
|
- * @return string
|
|
|
+ * {@inheritdoc}
|
|
|
*/
|
|
|
- protected function cleanHtmlWhitespace($html)
|
|
|
+ protected function getTemplatePaths()
|
|
|
{
|
|
|
- $html = preg_replace_callback('/>([^<]+)</', function ($value) {
|
|
|
- return '>'.trim($value[1]).'<';
|
|
|
- }, $html);
|
|
|
-
|
|
|
- return $html;
|
|
|
+ return array_merge(parent::getTemplatePaths(), array(
|
|
|
+ __DIR__.'/../../../Resources/views/Form',
|
|
|
+ ));
|
|
|
}
|
|
|
}
|