forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Rabaix
committed
Aug 24, 2011
1 parent
8a8c084
commit 14d1a4e
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |