瀏覽代碼

Add test on FilterInterface

Thomas Rabaix 13 年之前
父節點
當前提交
178b98b999
共有 3 個文件被更改,包括 118 次插入12 次删除
  1. 42 5
      Filter/Filter.php
  2. 0 7
      Filter/FilterInterface.php
  3. 76 0
      Tests/Filter/ORM/FilterTest.php

+ 42 - 5
Filter/Filter.php

@@ -33,11 +33,6 @@ abstract class Filter implements FilterInterface
         $this->options            = array_merge($this->getDefaultOptions(), $fieldDescription->getOptions());
     }
 
-    public function initialize(array $options = array())
-    {
-        $this->options = array_replace($this->getDefaultOptions(), $options);
-    }
-
     public function getName()
     {
         return $this->name;
@@ -51,11 +46,19 @@ abstract class Filter implements FilterInterface
         return $this->fieldDescription;
     }
 
+    /**
+     * @return array
+     */
     public function getDefaultOptions()
     {
         return array();
     }
 
+    /**
+     * @param $name
+     * @param null $default
+     * @return mixed
+     */
     public function getOption($name, $default = null)
     {
         if (array_key_exists($name, $this->options)) {
@@ -80,4 +83,38 @@ abstract class Filter implements FilterInterface
     {
         return $this->getOption('field_options', array('required' => false));
     }
+
+    /**
+     * @param $options
+     * @return void
+     */
+    public function setOptions($options)
+    {
+        $this->options = $options;
+    }
+
+    /**
+     * @return array
+     */
+    public function getOptions()
+    {
+        return $this->options;
+    }
+
+    /**
+     * @param $value
+     * @return void
+     */
+    public function setValue($value)
+    {
+        $this->value = $value;
+    }
+
+    /**
+     * @return null
+     */
+    public function getValue()
+    {
+        return $this->value;
+    }
 }

+ 0 - 7
Filter/FilterInterface.php

@@ -60,11 +60,4 @@ interface FilterInterface
      * @return void
      */
     function setFieldDescription(FieldDescriptionInterface $fieldDescription);
-
-    /**
-     * @abstract
-     * @param array $options
-     * @return void
-     */
-    function initialize(array $options = array());
 }

+ 76 - 0
Tests/Filter/ORM/FilterTest.php

@@ -0,0 +1,76 @@
+<?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\Filter\ORM;
+
+use Sonata\AdminBundle\Filter\ORM\Filter;
+
+class FilterTest_Filter extends Filter
+{
+    /**
+     * Apply the filter to the QueryBuilder instance
+     *
+     * @param $queryBuilder
+     * @param string $alias
+     * @param string $field
+     * @param string $value
+     * @return void
+     */
+    function filter($queryBuilder, $alias, $field, $value)
+    {
+        // TODO: Implement filter() method.
+    }
+
+    function getDefaultOptions()
+    {
+        return array('option1' => 2);
+    }
+}
+
+
+class FilterTest extends \PHPUnit_Framework_TestCase
+{
+
+    public function testFieldDescription()
+    {
+        $filter = new FilterTest_Filter();
+        $this->assertEquals(array('option1' => 2), $filter->getDefaultOptions());
+        $this->assertEquals(null, $filter->getOption('1'));
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())
+            ->method('getOptions')
+            ->will($this->returnValue(array('field_options' => array('class' => 'FooBar'))));
+
+        $fieldDescription->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('field_name'));
+
+        $filter->setFieldDescription($fieldDescription);
+
+        $this->assertEquals(2, $filter->getOption('option1'));
+        $this->assertEquals(null, $filter->getOption('foo'));
+        $this->assertEquals('bar', $filter->getOption('foo', 'bar'));
+
+        $this->assertEquals('field_name', $filter->getName());
+        $this->assertEquals('text', $filter->getFieldType());
+        $this->assertEquals(array('class' => 'FooBar'), $filter->getFieldOptions());
+    }
+
+    public function testValues()
+    {
+        $filter = new FilterTest_Filter();
+        $this->assertEmpty($filter->getValue());
+
+        $filter->setValue(42);
+        $this->assertEquals(42, $filter->getValue());
+    }
+}