Skip to content

Commit

Permalink
Add NumberFilterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Aug 24, 2011
1 parent 14d1a4e commit b5ba007
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Filter/ORM/NumberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NumberFilter extends Filter
*/
public function filter($queryBuilder, $alias, $field, $value)
{
if ($value == null) {
if ($value == null || !is_array($value)) {
return;
}

Expand Down
81 changes: 81 additions & 0 deletions Tests/Filter/ORM/NumberFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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\NumberFilter;
use Sonata\AdminBundle\Form\Type\Filter\NumberType;

class NumberFilterTest 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 testFilterEmpty()
{
$filter = new NumberFilter;
$filter->setFieldDescription($this->getFieldDescription(array('field_options' => array('class' => 'FooBar'))));

$builder = new QueryBuilder;

$filter->filter($builder, 'alias', 'field', null);
$filter->filter($builder, 'alias', 'field', 'asds');

$this->assertEquals(array(), $builder->query);
}

public function testFilterInvalidOperator()
{
$filter = new NumberFilter;
$filter->setFieldDescription($this->getFieldDescription(array('field_options' => array('class' => 'FooBar'))));

$builder = new QueryBuilder;

$filter->filter($builder, 'alias', 'field', array('type' => 'foo'));

$this->assertEquals(array(), $builder->query);
}

public function testFilter()
{
$filter = new NumberFilter;
$filter->setFieldDescription($this->getFieldDescription(array('field_options' => array('class' => 'FooBar'))));

$builder = new QueryBuilder;

$filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_EQUAL, 'value' => 42));
$filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_GREATER_EQUAL, 'value' => 42));
$filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_GREATER_THAN, 'value' => 42));
$filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_LESS_EQUAL, 'value' => 42));
$filter->filter($builder, 'alias', 'field', array('type' => NumberType::TYPE_LESS_THAN, 'value' => 42));

$expected = array(
'alias.field = :field_name',
'alias.field >= :field_name',
'alias.field > :field_name',
'alias.field <= :field_name',
'alias.field < :field_name'
);

$this->assertEquals($expected, $builder->query);
}
}

0 comments on commit b5ba007

Please sign in to comment.