diff --git a/Tests/Form/Widget/BaseWidgetTest.php b/Tests/Form/Widget/BaseWidgetTest.php new file mode 100644 index 00000000..2be8ecae --- /dev/null +++ b/Tests/Form/Widget/BaseWidgetTest.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\CoreBundle\Tests\Form\Widget; + +use Symfony\Bridge\Twig\Extension\FormExtension; +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\FormExtensionInterface; +use Symfony\Component\Form\FormView; +use Symfony\Component\Form\Test\TypeTestCase; + +/** + * Class BaseWidgetTest. + * + * @author Christian Gripp + */ +abstract class BaseWidgetTest extends TypeTestCase +{ + /** + * @var FormExtensionInterface + */ + private $extension; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + parent::setUp(); + + $rendererEngine = new TwigRendererEngine(array( + 'formatter.html.twig', + 'form_div_layout.html.twig', + )); + + if (class_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')) { + $csrfManagerClass = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'; + } else { + $csrfManagerClass = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'; + } + + $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass)); + + $this->extension = new FormExtension($renderer); + + /* + * this is an 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); + + $environment = new \Twig_Environment($loader, array('strict_variables' => true)); + $environment->addExtension(new TranslationExtension(new StubTranslator())); + foreach ($this->getTwigExtensions() as $extension) { + $environment->addExtension($extension); + } + $environment->addExtension($this->extension); + + $this->extension->initRuntime($environment); + } + + /** + * @return \Twig_ExtensionInterface[] + */ + protected function getTwigExtensions() + { + return array(); + } + + /** + * Renders widget from FormView, in SonataAdmin context, with optional view variables $vars. Returns plain HTML. + * + * @param FormView $view + * @param array $vars + * + * @return string + */ + final 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 + */ + final protected function cleanHtmlWhitespace($html) + { + return preg_replace_callback('/>([^<]+)'.trim($value[1]).'<'; + }, $html); + } + + /** + * @param string $html + * + * @return string + */ + final protected function cleanHtmlAttributeWhitespace($html) + { + return preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', function ($m) { + return preg_replace('~\s*~', '', $m[0]); + }, $html); + } +} diff --git a/composer.json b/composer.json index 56ad8cae..ff369eb2 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "symfony/form": "^2.3 || ^3.0", "symfony/config": "^2.3 || ^3.0", "symfony/translation": "^2.3 || ^3.0", + "symfony/twig-bridge": "^2.3.5 || ^3.0", "symfony/validator": "^2.3 || ^3.0", "symfony/property-access": "^2.3 || ^3.0", "twig/twig": "^1.23",