Browse Source

Improved code coverage of Filter class. Minor CS issues fixed.

Andrej Hudec 11 years ago
parent
commit
9762f5d1b1
3 changed files with 153 additions and 43 deletions
  1. 13 11
      Filter/Filter.php
  2. 112 32
      Tests/Filter/FilterTest.php
  3. 28 0
      Tests/Fixtures/Filter/FooFilter.php

+ 13 - 11
Filter/Filter.php

@@ -114,7 +114,7 @@ abstract class Filter implements FilterInterface
         $fieldName = $this->getOption('field_name');
         $fieldName = $this->getOption('field_name');
 
 
         if (!$fieldName) {
         if (!$fieldName) {
-            throw new \RunTimeException(sprintf('The option `field_name` must be set for field : `%s`', $this->getName()));
+            throw new \RuntimeException(sprintf('The option `field_name` must be set for field: `%s`', $this->getName()));
         }
         }
 
 
         return $fieldName;
         return $fieldName;
@@ -136,7 +136,7 @@ abstract class Filter implements FilterInterface
         $fieldMapping = $this->getOption('field_mapping');
         $fieldMapping = $this->getOption('field_mapping');
 
 
         if (!$fieldMapping) {
         if (!$fieldMapping) {
-            throw new \RunTimeException(sprintf('The option `field_mapping` must be set for field : `%s`', $this->getName()));
+            throw new \RuntimeException(sprintf('The option `field_mapping` must be set for field: `%s`', $this->getName()));
         }
         }
 
 
         return $fieldMapping;
         return $fieldMapping;
@@ -150,16 +150,16 @@ abstract class Filter implements FilterInterface
         $associationMapping = $this->getOption('association_mapping');
         $associationMapping = $this->getOption('association_mapping');
 
 
         if (!$associationMapping) {
         if (!$associationMapping) {
-            throw new \RunTimeException(sprintf('The option `association_mapping` must be set for field : `%s`', $this->getName()));
+            throw new \RuntimeException(sprintf('The option `association_mapping` must be set for field: `%s`', $this->getName()));
         }
         }
 
 
         return $associationMapping;
         return $associationMapping;
     }
     }
 
 
     /**
     /**
-     * @param array $options
+     * Set options
      *
      *
-     * @return void
+     * @param array $options
      */
      */
     public function setOptions(array $options)
     public function setOptions(array $options)
     {
     {
@@ -167,6 +167,8 @@ abstract class Filter implements FilterInterface
     }
     }
 
 
     /**
     /**
+     * Get options
+     *
      * @return array
      * @return array
      */
      */
     public function getOptions()
     public function getOptions()
@@ -175,9 +177,9 @@ abstract class Filter implements FilterInterface
     }
     }
 
 
     /**
     /**
-     * @param mixed $value
+     * Set value
      *
      *
-     * @return void
+     * @param mixed $value
      */
      */
     public function setValue($value)
     public function setValue($value)
     {
     {
@@ -185,6 +187,8 @@ abstract class Filter implements FilterInterface
     }
     }
 
 
     /**
     /**
+     * Get value
+     *
      * @return mixed
      * @return mixed
      */
      */
     public function getValue()
     public function getValue()
@@ -205,9 +209,7 @@ abstract class Filter implements FilterInterface
     }
     }
 
 
     /**
     /**
-     * @param string $condition
-     *
-     * @return void
+     * {@inheritdoc}
      */
      */
     public function setCondition($condition)
     public function setCondition($condition)
     {
     {
@@ -215,7 +217,7 @@ abstract class Filter implements FilterInterface
     }
     }
 
 
     /**
     /**
-     * @return string
+     * {@inheritdoc}
      */
      */
     public function getCondition()
     public function getCondition()
     {
     {

+ 112 - 32
Tests/Filter/FilterTest.php

@@ -12,45 +12,23 @@
 namespace Sonata\AdminBundle\Tests\Filter;
 namespace Sonata\AdminBundle\Tests\Filter;
 
 
 use Sonata\AdminBundle\Filter\Filter;
 use Sonata\AdminBundle\Filter\Filter;
-use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
-
-class FilterTest_Filter extends Filter
-{
-    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value)
-    {
-    }
-
-    public function apply($query, $value)
-    {
-    }
-
-    public function getDefaultOptions()
-    {
-        return array(
-            'foo' => 'bar'
-        );
-    }
-
-    public function getRenderSettings()
-    {
-    }
-}
+use Sonata\AdminBundle\Tests\Fixtures\Filter\FooFilter;
 
 
 class FilterTest extends \PHPUnit_Framework_TestCase
 class FilterTest extends \PHPUnit_Framework_TestCase
 {
 {
     public function testFilter()
     public function testFilter()
     {
     {
-        $filter = new FilterTest_Filter;
+        $filter = new FooFilter();
 
 
         $this->assertEquals('text', $filter->getFieldType());
         $this->assertEquals('text', $filter->getFieldType());
         $this->assertEquals(array('required' => false), $filter->getFieldOptions());
         $this->assertEquals(array('required' => false), $filter->getFieldOptions());
         $this->assertNull($filter->getLabel());
         $this->assertNull($filter->getLabel());
 
 
         $options = array(
         $options = array(
-            'label' => 'foo',
-            'field_type' => 'integer',
+            'label'         => 'foo',
+            'field_type'    => 'integer',
             'field_options' => array('required' => true),
             'field_options' => array('required' => true),
-            'field_name' => 'name'
+            'field_name'    => 'name'
         );
         );
 
 
         $filter->setOptions($options);
         $filter->setOptions($options);
@@ -75,9 +53,9 @@ class FilterTest extends \PHPUnit_Framework_TestCase
 
 
     public function testInitialize()
     public function testInitialize()
     {
     {
-        $filter = new FilterTest_Filter;
+        $filter = new FooFilter();
         $filter->initialize('name', array(
         $filter->initialize('name', array(
-           'field_name' => 'bar'
+            'field_name' => 'bar'
         ));
         ));
 
 
         $this->assertEquals('name', $filter->getName());
         $this->assertEquals('name', $filter->getName());
@@ -87,7 +65,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
 
 
     public function testLabel()
     public function testLabel()
     {
     {
-        $filter = new FilterTest_Filter;
+        $filter = new FooFilter();
         $filter->setLabel('foo');
         $filter->setLabel('foo');
 
 
         $this->assertEquals('foo', $filter->getLabel());
         $this->assertEquals('foo', $filter->getLabel());
@@ -98,7 +76,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
      */
      */
     public function testExceptionOnNonDefinedFieldName()
     public function testExceptionOnNonDefinedFieldName()
     {
     {
-        $filter = new FilterTest_Filter;
+        $filter = new FooFilter();
 
 
         $filter->getFieldName();
         $filter->getFieldName();
     }
     }
@@ -111,7 +89,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
      */
      */
     public function testIsActive($expected, $value)
     public function testIsActive($expected, $value)
     {
     {
-        $filter = new FilterTest_Filter;
+        $filter = new FooFilter();
         $filter->setValue($value);
         $filter->setValue($value);
 
 
         $this->assertEquals($expected, $filter->isActive());
         $this->assertEquals($expected, $filter->isActive());
@@ -127,4 +105,106 @@ class FilterTest extends \PHPUnit_Framework_TestCase
             array(true, array('value' => "active")),
             array(true, array('value' => "active")),
         );
         );
     }
     }
+
+    public function testGetTranslationDomain()
+    {
+        $filter = new FooFilter();
+        $this->assertEquals(null, $filter->getTranslationDomain());
+        $filter->setOption('translation_domain', 'baz');
+        $this->assertEquals('baz', $filter->getTranslationDomain());
+    }
+
+    public function testGetFieldMappingException()
+    {
+        $filter = new FooFilter();
+        $filter->initialize('foo');
+
+        try {
+            $filter->getFieldMapping();
+        } catch (\RuntimeException $e) {
+            $this->assertContains('The option `field_mapping` must be set for field: `foo`', $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
+    }
+
+    public function testGetFieldMapping()
+    {
+        $fieldMapping = array(
+            'fieldName'  => 'username',
+            'type'       => 'string',
+            'columnName' => 'username',
+            'length'     => 200,
+            'unique'     => true,
+            'nullable'   => false,
+            'declared'   => 'Foo\Bar\User'
+        );
+
+        $filter = new FooFilter();
+        $filter->setOption('field_mapping', $fieldMapping);
+        $this->assertEquals($fieldMapping, $filter->getFieldMapping());
+    }
+
+    public function testGetParentAssociationMappings()
+    {
+        $parentAssociationMapping = array(
+            0 => array('fieldName'    => 'user',
+                'targetEntity' => 'Foo\Bar\User',
+                'joinColumns'  =>
+                array(
+                    0 =>
+                    array(
+                        'name'                 => 'user_id',
+                        'referencedColumnName' => 'user_id',
+                    )
+                ),
+                'type'         => 2,
+                'mappedBy'     => null,
+            )
+        );
+
+        $filter = new FooFilter();
+        $this->assertEquals(array(), $filter->getParentAssociationMappings());
+        $filter->setOption('parent_association_mappings', $parentAssociationMapping);
+        $this->assertEquals($parentAssociationMapping, $filter->getParentAssociationMappings());
+    }
+
+    public function testGetAssociationMappingException()
+    {
+        $filter = new FooFilter();
+        $filter->initialize('foo');
+        try {
+            $filter->getAssociationMapping();
+        } catch (\RuntimeException $e) {
+            $this->assertContains('The option `association_mapping` must be set for field: `foo`', $e->getMessage());
+
+            return;
+        }
+
+        $this->fail('Failed asserting that exception of type "\RuntimeException" is thrown.');
+    }
+
+    public function testGetAssociationMapping()
+    {
+        $associationMapping = array(
+            'fieldName'    => 'user',
+            'targetEntity' => 'Foo\Bar\User',
+            'joinColumns'  =>
+            array(
+                0 =>
+                array(
+                    'name'                 => 'user_id',
+                    'referencedColumnName' => 'user_id',
+                )
+            ),
+            'type'         => 2,
+            'mappedBy'     => null,
+        );
+
+        $filter = new FooFilter();
+        $filter->setOption('association_mapping', $associationMapping);
+        $this->assertEquals($associationMapping, $filter->getAssociationMapping());
+    }
 }
 }

+ 28 - 0
Tests/Fixtures/Filter/FooFilter.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Sonata\AdminBundle\Tests\Fixtures\Filter;
+
+use Sonata\AdminBundle\Filter\Filter;
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
+
+class FooFilter extends Filter
+{
+    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value)
+    {
+    }
+
+    public function apply($query, $value)
+    {
+    }
+
+    public function getDefaultOptions()
+    {
+        return array(
+            'foo' => 'bar',
+        );
+    }
+
+    public function getRenderSettings()
+    {
+    }
+}