Ver código fonte

make the sonata_admin twig variable global

This is what a compiler pass in this bundle does, let's do the same.
This enables us to remove a check I introduced in the admin fields
template for the sole purpose of testing (which is bad, but was not as
as bad as not having tests).
Grégoire Paris 9 anos atrás
pai
commit
64a5ed0617

+ 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' %}

+ 16 - 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,21 @@ 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(),
+        );
+
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -109,21 +125,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);
     }
 

+ 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;
     }