Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatterType Support SF3 #146

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Form/Type/FormatterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Translation\TranslatorInterface;

Expand Down Expand Up @@ -161,6 +162,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$pool = $this->pool;
$translator = $this->translator;
Expand Down
150 changes: 150 additions & 0 deletions Tests/Form/Widget/BaseWidgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\FormatterBundle\Tests\Form\Widget;

use Ivory\CKEditorBundle\Twig\CKEditorExtension;
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\FormView;
use Symfony\Component\Form\Test\TypeTestCase;

/**
* Class BaseWidgetTest.
*/
abstract class BaseWidgetTest extends TypeTestCase
{
/**
* @var FormExtension
*/
protected $extension;

/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();

$rendererEngine = new TwigRendererEngine(array(
'formatter.html.twig',
'form_div_layout.html.twig',
));

if (class_exists('Symfony\Component\Form\Extension\Core\Type\RangeType')) {
$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);

$twigPaths = array(__DIR__.'/../../../Resources/views/Form');

//this is ugly workaround for different build strategies and, possibly,
//different TwigBridge installation directories
if (is_dir(__DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to a seperate method? So we can use this for other tests too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? It already is in a public method…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatter depends on Core, so I think we could try to factor out something into core. Maybe let it mature into something generic enough first, b/c tests must stay easy to write.

$twigPaths[] = __DIR__.'/../../../vendor/symfony/twig-bridge/Resources/views/Form';
} elseif (is_dir(__DIR__.'/../../../vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form')) {
$twigPaths[] = __DIR__.'/../../../vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form';
} elseif (is_dir(__DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form')) {
$twigPaths[] = __DIR__.'/../../../vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
} else {
$twigPaths[] = __DIR__.'/../../../../../symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form';
}

$loader = new StubFilesystemLoader($twigPaths);

$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));

if (class_exists('Ivory\CKEditorBundle\Templating\CKEditorHelper')) {
$helperClass = 'Ivory\CKEditorBundle\Templating\CKEditorHelper';
} else {
$helperClass = 'Ivory\CKEditorBundle\Helper\CKEditorHelper';
}

$helper = $this->getMockBuilder($helperClass)
->disableOriginalConstructor()
->getMock();

$environment->addExtension(new CKEditorExtension($helper));
$environment->addExtension($this->extension);

$this->extension->initRuntime($environment);
}

/**
* 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())
{
$sonataAdmin = array(
'name' => null,
'admin' => null,
'value' => null,
'edit' => 'standard',
'inline' => 'natural',
'field_description' => null,
'block_name' => false,
'options' => array(),
);

$vars = array_merge(array(
'sonata_admin' => $sonataAdmin,
), $vars);

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
*/
protected function cleanHtmlWhitespace($html)
{
$html = preg_replace_callback('/>([^<]+)</', function ($value) {
return '>'.trim($value[1]).'<';
}, $html);

return $html;
}

/**
* @param $html
*
* @return mixed
*/
protected function cleanHtmlAttributeWhitespace($html)
{
$html = preg_replace_callback('~<([A-Z0-9]+) \K(.*?)>~i', function ($m) {
$replacement = preg_replace('~\s*~', '', $m[0]);

return $replacement;
}, $html);

return $html;
}
}
104 changes: 104 additions & 0 deletions Tests/Form/Widget/FormatterTypeWidgetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\FormatterBundle\Tests\Form\Widget;

use Sonata\FormatterBundle\Form\Type\FormatterType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Tests\Fixtures\TestExtension;

class FormatterTypeWidgetTest extends BaseWidgetTest
{
public function testFormatterTypedIsRendering()
{
$form = $this->factory->create(
'sonata_formatter_type',
null,
$this->getDefaultOption()
);

$html = $this->cleanHtmlWhitespace($this->renderWidget($form->createView()));
$html = $this->cleanHtmlAttributeWhitespace($html);

$this->assertContains(
'<option value="foo">[trans]foo[/trans]</option>',
$html
);

$this->assertContains(
'<option value="bar">[trans]bar[/trans]</option>',
$html
);
}

protected function getExtensions()
{
$pool = $this->getMockBuilder('Sonata\FormatterBundle\Formatter\Pool')
->disableOriginalConstructor()
->getMock();

$formatters = array(
'foo' => 'foo',
'bar' => 'bar',
);

$pool->expects($this->once())
->method('getFormatters')
->will($this->returnValue($formatters));

$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');

$translator->expects($this->exactly(2))
->method('trans')
->will($this->returnCallback(function ($arg) { return $arg;})
);

$configManager = $this->getMock('Ivory\CKEditorBundle\Model\ConfigManagerInterface');

$formatType = new FormatterType($pool, $translator, $configManager);

$extensions = parent::getExtensions();
$extensions[] = $this->registerExtension($formatType, 'sonata_formatter_type');
$extensions[] = $this->registerExtension(new ChoiceType(), 'choice');
$extensions[] = $this->registerExtension(new TextareaType(), 'textarea');

return $extensions;
}

protected function registerExtension($objectType, $name)
{
$guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');

$extension = new TestExtension($guesser);
$extension->addType($objectType);

if (!$extension->hasType($name)) {
$reflection = new \ReflectionClass($extension);
$property = $reflection->getProperty('types');
$property->setAccessible(true);
$property->setValue($extension, array($name => current($property->getValue($extension))));
}

return $extension;
}

protected function getDefaultOption()
{
return array(
'format_field' => 'foo',
'source_field' => 'source',
'target_field' => 'target',
'event_dispatcher' => $this->dispatcher,
'inherit_data' => false,
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require-dev": {
"symfony/validator": "~2.3|~3.0",
"symfony/phpunit-bridge": "~2.7|~3.0",
"fabpot/php-cs-fixer": "~0.5|~1.0"
"fabpot/php-cs-fixer": "~0.5|~1.0",
"symfony/twig-bridge": "~2.3|~3.0"
},
"autoload": {
"psr-4": { "Sonata\\FormatterBundle\\": "" }
Expand Down