Browse Source

Add StringFilterTest

Thomas Rabaix 13 years ago
parent
commit
037d25b628
2 changed files with 98 additions and 0 deletions
  1. 32 0
      Tests/Filter/ORM/QueryBuilder.php
  2. 66 0
      Tests/Filter/ORM/StringFilterTest.php

+ 32 - 0
Tests/Filter/ORM/QueryBuilder.php

@@ -0,0 +1,32 @@
+<?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 QueryBuilder
+{
+    public $parameters = array();
+
+    public $query = array();
+
+    public function setParameter($name, $value)
+    {
+        $this->parameters[$name] = $value;
+    }
+
+    public function andWhere($query)
+    {
+        $this->query[] = $query;
+    }
+}

+ 66 - 0
Tests/Filter/ORM/StringFilterTest.php

@@ -0,0 +1,66 @@
+<?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\StringFilter;
+
+class StringFilterTest extends \PHPUnit_Framework_TestCase
+{
+
+    public function getFieldDescription(array $options)
+    {
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())
+            ->method('getOptions')
+            ->will($this->returnValue($options));
+
+        $fieldDescription->expects($this->once())
+            ->method('getName')
+            ->will($this->returnValue('field_name'));
+
+        return $fieldDescription;
+    }
+
+    public function testFilter()
+    {
+        $filter = new StringFilter;
+        $filter->setFieldDescription($this->getFieldDescription(array('field_options' => array('class' => 'FooBar'))));
+
+        $builder = new QueryBuilder;
+
+        $filter->filter($builder, 'alias', 'field', null);
+        $filter->filter($builder, 'alias', 'field', '');
+
+        $this->assertEquals(array(), $builder->query);
+
+        $filter->filter($builder, 'alias', 'field', 'asd');
+        $this->assertEquals(array('alias.field LIKE :field_name'), $builder->query);
+        $this->assertEquals(array('field_name' => '%asd%'), $builder->parameters);
+    }
+
+    public function testFormat()
+    {
+        $filter = new StringFilter;
+        $filter->setFieldDescription($this->getFieldDescription(array('format' => '%s')));
+
+        $builder = new QueryBuilder;
+
+        $filter->filter($builder, 'alias', 'field', null);
+        $filter->filter($builder, 'alias', 'field', '');
+
+        $this->assertEquals(array(), $builder->query);
+
+        $filter->filter($builder, 'alias', 'field', 'asd');
+        $this->assertEquals(array('alias.field LIKE :field_name'), $builder->query);
+        $this->assertEquals(array('field_name' => 'asd'), $builder->parameters);
+    }
+}