Bläddra i källkod

Merge pull request #4182 from core23/patch/filter-factory

Improved class support for filter factory
Grégoire Paris 8 år sedan
förälder
incheckning
c7440e6d8e
2 ändrade filer med 32 tillägg och 13 borttagningar
  1. 6 4
      Filter/FilterFactory.php
  2. 26 9
      Tests/Filter/FilterFactoryTest.php

+ 6 - 4
Filter/FilterFactory.php

@@ -51,14 +51,16 @@ class FilterFactory implements FilterFactoryInterface
 
         $id = isset($this->types[$type]) ? $this->types[$type] : false;
 
-        if (!$id) {
+        if ($id) {
+            $filter = $this->container->get($id);
+        } elseif (class_exists($type)) {
+            $filter = new $type();
+        } else {
             throw new \RuntimeException(sprintf('No attached service to type named `%s`', $type));
         }
 
-        $filter = $this->container->get($id);
-
         if (!$filter instanceof FilterInterface) {
-            throw new \RuntimeException(sprintf('The service `%s` must implement `FilterInterface`', $id));
+            throw new \RuntimeException(sprintf('The service `%s` must implement `FilterInterface`', $type));
         }
 
         $filter->initialize($name, $options);

+ 26 - 9
Tests/Filter/FilterFactoryTest.php

@@ -15,33 +15,50 @@ use Sonata\AdminBundle\Filter\FilterFactory;
 
 class FilterFactoryTest extends \PHPUnit_Framework_TestCase
 {
-    /**
-     * @expectedException RuntimeException
-     */
     public function testEmptyType()
     {
+        $this->setExpectedException('\RuntimeException', 'The type must be defined');
+
         $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
 
         $filter = new FilterFactory($container, array());
         $filter->create('test', null);
     }
 
-    /**
-     * @expectedException RuntimeException
-     */
     public function testUnknownType()
     {
+        $this->setExpectedException('\RuntimeException', 'No attached service to type named `mytype`');
+
         $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
 
         $filter = new FilterFactory($container, array());
         $filter->create('test', 'mytype');
     }
 
-    /**
-     * @expectedException RuntimeException
-     */
+    public function testUnknownClassType()
+    {
+        $this->setExpectedException('\RuntimeException', 'No attached service to type named `Sonata\AdminBundle\Form\Type\Filter\FooType`');
+
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $filter = new FilterFactory($container, array());
+        $filter->create('test', 'Sonata\AdminBundle\Form\Type\Filter\FooType');
+    }
+
+    public function testClassType()
+    {
+        $this->setExpectedException('\RuntimeException', 'The service `Sonata\AdminBundle\Form\Type\Filter\DefaultType` must implement `FilterInterface`');
+
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+
+        $filter = new FilterFactory($container, array());
+        $filter->create('test', 'Sonata\AdminBundle\Form\Type\Filter\DefaultType');
+    }
+
     public function testInvalidTypeInstance()
     {
+        $this->setExpectedException('\RuntimeException', 'The service `mytype` must implement `FilterInterface`');
+
         $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
         $container->expects($this->once())
             ->method('get')