Procházet zdrojové kódy

avoid Kernel::VERSION when detecting features

The kernel version might differ from components versions.
Grégoire Paris před 9 roky
rodič
revize
e289f25142

+ 4 - 6
Form/Type/CollectionType.php

@@ -12,7 +12,6 @@
 namespace Sonata\AdminBundle\Form\Type;
 
 use Symfony\Component\Form\AbstractType;
-use Symfony\Component\HttpKernel\Kernel;
 
 /**
  * This type wrap native `collection` form type and render `add` and `delete`
@@ -27,11 +26,10 @@ class CollectionType extends AbstractType
      */
     public function getParent()
     {
-        if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            return 'Symfony\Component\Form\Extension\Core\Type\CollectionType';
-        } else {
-            return 'collection';
-        }
+        return
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'collection' :
+            'Symfony\Component\Form\Extension\Core\Type\CollectionType';
     }
 
     /**

+ 2 - 3
Tests/Form/Extension/ChoiceTypeExtensionTest.php

@@ -14,17 +14,16 @@ namespace Sonata\AdminBundle\Tests\Form\Extension;
 use Sonata\AdminBundle\Form\Extension\ChoiceTypeExtension;
 use Sonata\CoreBundle\Form\Extension\DependencyInjectionExtension;
 use Symfony\Component\Form\Forms;
-use Symfony\Component\HttpKernel\Kernel;
 
 class ChoiceTypeExtensionTest extends \PHPUnit_Framework_TestCase
 {
     protected function setup()
     {
-        if (version_compare(Kernel::VERSION, '2.8', '<')) {
+        if (method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
             $this->factory = Forms::createFormFactoryBuilder()
                   ->addTypeExtension(new ChoiceTypeExtension())
                   ->getFormFactory();
-        } else { // SF2.7+
+        } else {
             $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
             $container->expects($this->any())->method('has')->will($this->returnValue(true));
             $container->expects($this->any())->method('get')

+ 4 - 6
Tests/Form/Widget/BaseWidgetTest.php

@@ -19,7 +19,6 @@ 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;
-use Symfony\Component\HttpKernel\Kernel;
 
 /**
  * Class BaseWidgetTest.
@@ -79,11 +78,10 @@ abstract class BaseWidgetTest extends TypeTestCase
             $this->type.'_admin_fields.html.twig',
         ));
 
-        if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            $csrfManagerClass = 'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface';
-        } else {
-            $csrfManagerClass = 'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
-        }
+        $csrfManagerClass =
+            interface_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface') ?
+            'Symfony\Component\Security\Csrf\CsrfTokenManagerInterface' :
+            'Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface';
 
         $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfManagerClass));
 

+ 8 - 8
Tests/Form/Widget/FilterChoiceWidgetTest.php

@@ -11,8 +11,6 @@
 
 namespace Sonata\AdminBundle\Tests\Form\Widget;
 
-use Symfony\Component\HttpKernel\Kernel;
-
 class FilterChoiceWidgetTest extends BaseWidgetTest
 {
     protected $type = 'filter';
@@ -77,11 +75,10 @@ class FilterChoiceWidgetTest extends BaseWidgetTest
 
     protected function getChoiceClass()
     {
-        if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
-        } else {
-            return 'choice';
-        }
+        return
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'choice' :
+            'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
     }
 
     /**
@@ -90,7 +87,10 @@ class FilterChoiceWidgetTest extends BaseWidgetTest
      */
     protected function getDefaultOption()
     {
-        if (version_compare(Kernel::VERSION, '2.6.0', '>=')) {
+        if (method_exists(
+            'Symfony\Component\Form\Tests\AbstractLayoutTest',
+            'testSingleChoiceNonRequiredWithPlaceholder'
+        )) {
             return array(
                 'placeholder' => 'Choose an option',
             );

+ 8 - 8
Tests/Form/Widget/FormChoiceWidgetTest.php

@@ -11,8 +11,6 @@
 
 namespace Sonata\AdminBundle\Tests\Form\Widget;
 
-use Symfony\Component\HttpKernel\Kernel;
-
 class FormChoiceWidgetTest extends BaseWidgetTest
 {
     protected $type = 'form';
@@ -129,11 +127,10 @@ class FormChoiceWidgetTest extends BaseWidgetTest
 
     protected function getChoiceClass()
     {
-        if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
-        } else {
-            return 'choice';
-        }
+        return
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'choice' :
+            'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
     }
 
     /**
@@ -142,7 +139,10 @@ class FormChoiceWidgetTest extends BaseWidgetTest
      */
     protected function getDefaultOption()
     {
-        if (version_compare(Kernel::VERSION, '2.6.0', '>=')) {
+        if (method_exists(
+            'Symfony\Component\Form\Tests\AbstractLayoutTest',
+            'testSingleChoiceNonRequiredWithPlaceholder'
+        )) {
             return array(
                 'placeholder' => 'Choose an option',
             );

+ 5 - 7
Tests/Form/Widget/FormSonataNativeCollectionWidgetTest.php

@@ -14,7 +14,6 @@ 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;
 
 class FormSonataNativeCollectionWidgetTest extends BaseWidgetTest
 {
@@ -57,7 +56,7 @@ class FormSonataNativeCollectionWidgetTest extends BaseWidgetTest
         $extensions = parent::getExtensions();
         $guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
         $extension = new TestExtension($guesser);
-        if (!version_compare(Kernel::VERSION, '2.8.0', '>=')) {
+        if (method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions')) {
             $extension->addType(new CollectionType());
         }
 
@@ -71,10 +70,9 @@ class FormSonataNativeCollectionWidgetTest extends BaseWidgetTest
 
     protected function getChoiceClass()
     {
-        if (version_compare(Kernel::VERSION, '2.8.0', '>=')) {
-            return 'Sonata\AdminBundle\Form\Type\CollectionType';
-        } else {
-            return 'sonata_type_native_collection';
-        }
+        return
+            method_exists('Symfony\Component\Form\FormTypeInterface', 'setDefaultOptions') ?
+            'sonata_type_native_collection' :
+            'Sonata\AdminBundle\Form\Type\CollectionType';
     }
 }