-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
rafaelcalleja
wants to merge
1
commit into
sonata-project:master
from
rafaelcalleja:PR_format_type_supp_sf3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) { | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we use a global WidgetTest class @greg0ire @rafaelcalleja? We have an existing here https://github.com/sonata-project/SonataAdminBundle/blob/master/Tests/Form/Widget/BaseWidgetTest.php
There was a problem hiding this comment.
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.