Skip to content

Commit

Permalink
Improved code coverage of Filter class. Minor CS issues fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
pulzarraider committed Jul 24, 2014
1 parent 707dfdb commit 9762f5d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 43 deletions.
24 changes: 13 additions & 11 deletions Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getFieldName()
$fieldName = $this->getOption('field_name');

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;
Expand All @@ -136,7 +136,7 @@ public function getFieldMapping()
$fieldMapping = $this->getOption('field_mapping');

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;
Expand All @@ -150,23 +150,25 @@ public function getAssociationMapping()
$associationMapping = $this->getOption('association_mapping');

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;
}

/**
* @param array $options
* Set options
*
* @return void
* @param array $options
*/
public function setOptions(array $options)
{
$this->options = array_merge($this->getDefaultOptions(), $options);
}

/**
* Get options
*
* @return array
*/
public function getOptions()
Expand All @@ -175,16 +177,18 @@ public function getOptions()
}

/**
* @param mixed $value
* Set value
*
* @return void
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
}

/**
* Get value
*
* @return mixed
*/
public function getValue()
Expand All @@ -205,17 +209,15 @@ public function isActive()
}

/**
* @param string $condition
*
* @return void
* {@inheritdoc}
*/
public function setCondition($condition)
{
$this->condition = $condition;
}

/**
* @return string
* {@inheritdoc}
*/
public function getCondition()
{
Expand Down
144 changes: 112 additions & 32 deletions Tests/Filter/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,23 @@
namespace Sonata\AdminBundle\Tests\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
{
public function testFilter()
{
$filter = new FilterTest_Filter;
$filter = new FooFilter();

$this->assertEquals('text', $filter->getFieldType());
$this->assertEquals(array('required' => false), $filter->getFieldOptions());
$this->assertNull($filter->getLabel());

$options = array(
'label' => 'foo',
'field_type' => 'integer',
'label' => 'foo',
'field_type' => 'integer',
'field_options' => array('required' => true),
'field_name' => 'name'
'field_name' => 'name'
);

$filter->setOptions($options);
Expand All @@ -75,9 +53,9 @@ public function testFilter()

public function testInitialize()
{
$filter = new FilterTest_Filter;
$filter = new FooFilter();
$filter->initialize('name', array(
'field_name' => 'bar'
'field_name' => 'bar'
));

$this->assertEquals('name', $filter->getName());
Expand All @@ -87,7 +65,7 @@ public function testInitialize()

public function testLabel()
{
$filter = new FilterTest_Filter;
$filter = new FooFilter();
$filter->setLabel('foo');

$this->assertEquals('foo', $filter->getLabel());
Expand All @@ -98,7 +76,7 @@ public function testLabel()
*/
public function testExceptionOnNonDefinedFieldName()
{
$filter = new FilterTest_Filter;
$filter = new FooFilter();

$filter->getFieldName();
}
Expand All @@ -111,7 +89,7 @@ public function testExceptionOnNonDefinedFieldName()
*/
public function testIsActive($expected, $value)
{
$filter = new FilterTest_Filter;
$filter = new FooFilter();
$filter->setValue($value);

$this->assertEquals($expected, $filter->isActive());
Expand All @@ -127,4 +105,106 @@ public function isActiveData()
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 changes: 28 additions & 0 deletions Tests/Fixtures/Filter/FooFilter.php
Original file line number Diff line number Diff line change
@@ -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()
{
}
}

0 comments on commit 9762f5d

Please sign in to comment.