Procházet zdrojové kódy

Merge pull request #3631 from greg0ire/widget_test

Widget test
Oskar Stark před 9 roky
rodič
revize
9dbc50b189

+ 4 - 1
Form/Extension/Field/Type/FormTypeFieldExtension.php

@@ -205,7 +205,10 @@ class FormTypeFieldExtension extends AbstractTypeExtension
      */
     public function getExtendedType()
     {
-        return 'form';
+        return
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'form' :
+            'Symfony\Component\Form\Extension\Core\Type\FormType';
     }
 
     /**

+ 1 - 1
Resources/views/Form/form_admin_fields.html.twig

@@ -268,7 +268,7 @@ file that was distributed with this source code.
             {% set div_class = div_class ~ ' sonata-collection-row-without-label' %}
         {% endif %}
 
-        {% if sonata_admin is defined and sonata_admin.options['form_type'] == 'horizontal' %}
+        {% if sonata_admin.options['form_type'] == 'horizontal' %}
             {% if label is same as(false) %}
                 {% if 'collection' in form.parent.vars.block_prefixes %}
                     {% set div_class = div_class ~ ' col-sm-12' %}

+ 6 - 1
Tests/Form/Extension/Field/Type/FormTypeFieldExtensionTest.php

@@ -23,7 +23,12 @@ class FormTypeFieldExtensionTest extends \PHPUnit_Framework_TestCase
     {
         $extension = new FormTypeFieldExtension(array(), array());
 
-        $this->assertSame('form', $extension->getExtendedType());
+        $this->assertSame(
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'form' :
+            'Symfony\Component\Form\Extension\Core\Type\FormType',
+            $extension->getExtendedType()
+        );
     }
 
     public function testDefaultOptions()

+ 17 - 15
Tests/Form/Widget/BaseWidgetTest.php

@@ -82,6 +82,7 @@ abstract class BaseWidgetTest extends TypeTestCase
         $loader = new StubFilesystemLoader($twigPaths);
 
         $environment = new \Twig_Environment($loader, array('strict_variables' => true));
+        $environment->addGlobal('sonata_admin', $this->getSonataAdmin());
         $environment->addExtension(new TranslationExtension(new StubTranslator()));
 
         $environment->addExtension($this->extension);
@@ -89,6 +90,22 @@ abstract class BaseWidgetTest extends TypeTestCase
         $this->extension->initRuntime($environment);
     }
 
+    protected function getSonataAdmin()
+    {
+        return array(
+            'name'              => null,
+            'admin'             => null,
+            'value'             => null,
+            'edit'              => 'standard',
+            'inline'            => 'natural',
+            'field_description' => null,
+            'block_name'        => false,
+            'options'           => array(
+                'form_type' => 'vertical',
+            ),
+        );
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -109,21 +126,6 @@ abstract class BaseWidgetTest extends TypeTestCase
      */
     protected function renderWidget(FormView $view, array $vars = array())
     {
-        $sonataAdmin = $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);
     }
 

+ 24 - 0
Tests/Form/Widget/FormChoiceWidgetTest.php

@@ -22,6 +22,30 @@ class FormChoiceWidgetTest extends BaseWidgetTest
         parent::setUp();
     }
 
+    public function testLabelRendering()
+    {
+        $choices = array('some', 'choices');
+        if (!method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
+            $choices = array_flip($choices);
+        }
+
+        $choice = $this->factory->create(
+            $this->getChoiceClass(),
+            null,
+            $this->getDefaultOption() + array(
+                'multiple' => true,
+                'expanded' => true,
+            ) + compact('choices')
+        );
+
+        $html = $this->renderWidget($choice->createView());
+
+        $this->assertContains(
+            '<span>[trans]some[/trans]</span>',
+            $this->cleanHtmlWhitespace($html)
+        );
+    }
+
     public function testDefaultValueRendering()
     {
         $choice = $this->factory->create(

+ 8 - 4
Tests/Form/Widget/FormSonataNativeCollectionWidgetTest.php

@@ -11,6 +11,7 @@
 
 namespace Sonata\AdminBundle\Tests\Form\Widget;
 
+use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
 use Sonata\AdminBundle\Form\Type\CollectionType;
 use Symfony\Component\Form\Tests\Fixtures\TestExtension;
 use Symfony\Component\HttpKernel\Kernel;
@@ -54,14 +55,17 @@ class FormSonataNativeCollectionWidgetTest extends BaseWidgetTest
     protected function getExtensions()
     {
         $extensions = parent::getExtensions();
+        $guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
+        $extension = new TestExtension($guesser);
         if (!version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            $guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
-            $extension = new TestExtension($guesser);
             $extension->addType(new CollectionType());
-
-            $extensions[] = $extension;
         }
 
+        $extension->addTypeExtension(new FormTypeFieldExtension(array(), array(
+            'form_type' => 'vertical',
+        )));
+        $extensions[] = $extension;
+
         return $extensions;
     }