Skip to content

Commit

Permalink
Add CallbackFilterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Aug 24, 2011
1 parent 8a8c084 commit 14d1a4e
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Tests/Filter/ORM/CallbackFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\CallbackFilter;

class CallbackFilterTest 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 testFilterClosure()
{
$builder = new QueryBuilder;

$filter = new CallbackFilter;
$filter->setFieldDescription($this->getFieldDescription(array(
'callback' => function($builder, $alias, $field, $value) {
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field));
$builder->setParameter('value', $value);
}
)));
;

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

$this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query);
$this->assertEquals(array('value' => 'myValue'), $builder->parameters);
}

public function testFilterMethod()
{
$builder = new QueryBuilder;

$filter = new CallbackFilter;
$filter->setFieldDescription($this->getFieldDescription(array(
'callback' => array($this, 'customCallback')
)));

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

$this->assertEquals(array('CUSTOM QUERY alias.field'), $builder->query);
$this->assertEquals(array('value' => 'myValue'), $builder->parameters);
}

public function customCallback($builder, $alias, $field, $value) {
$builder->andWhere(sprintf('CUSTOM QUERY %s.%s', $alias, $field));
$builder->setParameter('value', $value);
}

/**
* @expectedException RuntimeException
*/
public function testFilterException()
{
$builder = new QueryBuilder;

$filter = new CallbackFilter;
$filter->setFieldDescription($this->getFieldDescription(array()));

$filter->filter($builder, 'alias', 'field', 'myValue');
}
}

0 comments on commit 14d1a4e

Please sign in to comment.