Pārlūkot izejas kodu

Add FormTypeFieldExtension Test

Thomas Rabaix 12 gadi atpakaļ
vecāks
revīzija
1fdaa7bea0

+ 3 - 3
Form/Extension/Field/Type/FormTypeFieldExtension.php

@@ -94,7 +94,7 @@ class FormTypeFieldExtension extends AbstractTypeExtension
         $sonataAdmin = $form->getConfig()->getAttribute('sonata_admin');
 
         // avoid to add extra information not required by non admin field
-        if ($form->getConfig()->getAttribute('sonata_admin_enabled', true)) {
+        if ($sonataAdmin && $form->getConfig()->getAttribute('sonata_admin_enabled', true)) {
             $sonataAdmin['value'] = $form->getData();
 
             // add a new block types, so the Admin Form element can be tweaked based on the admin code
@@ -105,7 +105,7 @@ class FormTypeFieldExtension extends AbstractTypeExtension
             $block_prefixes[] = sprintf('%s_%s', $baseName, $baseType);
             $block_prefixes[] = sprintf('%s_%s_%s', $baseName, $sonataAdmin['field_description']->getName(), $baseType);
 
-            if ($sonataAdmin['block_name']) {
+            if (isset($sonataAdmin['block_name'])) {
                 $block_prefixes[] = $sonataAdmin['block_name'];
             }
 
@@ -115,7 +115,7 @@ class FormTypeFieldExtension extends AbstractTypeExtension
 
             $attr = $view->vars['attr'];
 
-            if (!isset($attr['class'])) {
+            if (!isset($attr['class']) && isset($sonataAdmin['class'])) {
                 $attr['class'] = $sonataAdmin['class'];
             }
 

+ 109 - 0
Tests/Form/Extension/Field/Type/FormTypeFieldExtensionTest.php

@@ -0,0 +1,109 @@
+<?php
+
+/*
+ * This file is part of the Sonata 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\AdminBundle\Tests\Form\Extension\Field\Type;
+
+use Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension;
+use Symfony\Component\Form\FormConfigBuilder;
+use Symfony\Component\Form\Form;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+use Symfony\Component\Form\FormView;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+
+class FormTypeFieldExtensionTest extends \PHPUnit_Framework_TestCase
+{
+    public function testExtendedType()
+    {
+        $extension = new FormTypeFieldExtension();
+
+        $this->assertEquals('field', $extension->getExtendedType());
+    }
+
+    public function testDefaultOptions()
+    {
+        $extension = new FormTypeFieldExtension();
+
+        $resolver = new OptionsResolver();
+        $extension->setDefaultOptions($resolver);
+
+        $options = $resolver->resolve();
+
+        $this->assertArrayHasKey('sonata_admin', $options);
+        $this->assertArrayHasKey('sonata_field_description', $options);
+
+        $this->assertNull($options['sonata_admin']);
+        $this->assertNull($options['sonata_field_description']);
+    }
+
+    public function testbuildViewWithNoSonataAdminArray()
+    {
+        $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+
+        $formView = new FormView();
+        $options = array();
+        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
+        $form = new Form($config);
+
+        $extension = new FormTypeFieldExtension();
+        $extension->buildView($formView, $form, array());
+
+        $this->assertArrayHasKey('sonata_admin', $formView->vars);
+        $this->assertNull($formView->vars['sonata_admin']);
+    }
+
+    public function testbuildViewWithWithSonataAdmin()
+    {
+        $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $admin->expects($this->once())->method('getCode')->will($this->returnValue('admin_code'));
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getAdmin')->will($this->returnValue($admin));
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('name'));
+
+        $eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+
+        $formView = new FormView();
+        $options = array();
+        $config = new FormConfigBuilder('test', 'stdClass', $eventDispatcher, $options);
+        $config->setAttribute('sonata_admin', array(
+            'field_description' => $fieldDescription,
+        ));
+        $config->setAttribute('sonata_admin_enabled', true);
+
+        $form = new Form($config);
+
+
+        $formView->vars['block_prefixes'] = array('form', 'field', 'text', '_s50b26aa76cb96_username');
+
+
+        $extension = new FormTypeFieldExtension();
+        $extension->buildView($formView, $form, array(
+            'block_name' => 'foo'
+        ));
+
+        $this->assertArrayHasKey('block_prefixes', $formView->vars);
+        $this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
+        $this->assertArrayHasKey('sonata_admin', $formView->vars);
+
+        $expected = array(
+            'form',
+            'field',
+            'text',
+            '_s50b26aa76cb96_username',
+            'admin_code__s50b26aa76cb96_username',
+            'admin_code_name__s50b26aa76cb96_username',
+        );
+
+        $this->assertEquals($expected, $formView->vars['block_prefixes']);
+        $this->assertTrue($formView->vars['sonata_admin_enabled']);
+    }
+}

+ 9 - 0
Tests/Form/Type/BooleanTypeTest.php

@@ -1,5 +1,14 @@
 <?php
 
+/*
+ * This file is part of the Sonata 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\AdminBundle\Tests\Form\Type;
 
 use Sonata\AdminBundle\Form\Type\BooleanType;

+ 9 - 0
Tests/Form/Type/TranslatableChoiceTypeTest.php

@@ -1,5 +1,14 @@
 <?php
 
+/*
+ * This file is part of the Sonata 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\AdminBundle\Tests\Form\Type;
 
 use Sonata\AdminBundle\Form\Type\TranslatableChoiceType;