Skip to content

Commit

Permalink
fixed tests and integer range
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Girard committed Oct 23, 2013
1 parent 27055c2 commit 379fec7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
44 changes: 32 additions & 12 deletions Event/Subscriber/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public function filterValue(ApplyFilterEvent $event)
if ('' !== $values['value'] && null !== $values['value']) {
// alias.field -> alias_field
$fieldName = str_replace('.', '_', $event->getField());
if(is_array($values['value']) && sizeof($values['value']) > 0) {

if (is_array($values['value']) && sizeof($values['value']) > 0) {
$qb->andWhere($expr->in($event->getField(), $values['value']));
} elseif(is_array($values['value']) && sizeof($values['value']) == 0) {
// Multiselect is Empty - Don't Filter
} else {

} elseif (!is_array($values['value'])) {
$qb->andWhere($expr->eq($event->getField(), ':' . $fieldName))
->setParameter($fieldName, $values['value']);
}
Expand Down Expand Up @@ -243,18 +243,38 @@ public function filterNumberRange(ApplyFilterEvent $event)
$values = $event->getValues();
$value = $values['value'];

if (isset($value['left_number'][0]) && isset($value['left_number'][0]['condition_operator'])) {
$leftCond = $value['left_number'][0]['condition_operator'];
$leftValue = $value['left_number'][0]['text'];
if (isset($value['left_number'][0])) {
$hasSelector = ( FilterOperands::OPERAND_SELECTOR == $value['left_number']['condition_operator'] );

if (!$hasSelector && isset($value['left_number'][0])) {
$leftValue = $value['left_number'][0];
$leftCond = $value['left_number']['condition_operator'];

$qb->andWhere($expr->$leftCond($event->getField(), $leftValue));

} else if ($hasSelector && isset($value['left_number'][0]['text'])) {
$leftValue = $value['left_number'][0]['text'];
$leftCond = $value['left_number'][0]['condition_operator'];

$qb->andWhere($expr->$leftCond($event->getField(), $leftValue));
$qb->andWhere($expr->$leftCond($event->getField(), $leftValue));
}
}

if (isset($value['right_number'][0]) && isset($value['right_number'][0]['condition_operator'])) {
$rightCond = $value['right_number'][0]['condition_operator'];
$rightValue = $value['right_number'][0]['text'];
if (isset($value['right_number'][0])) {
$hasSelector = ( FilterOperands::OPERAND_SELECTOR == $value['right_number']['condition_operator'] );

if (!$hasSelector && isset($value['right_number'][0])) {
$rightValue = $value['right_number'][0];
$rightCond = $value['right_number']['condition_operator'];

$qb->andWhere($expr->$rightCond($event->getField(), $rightValue));

$qb->andWhere($expr->$rightCond($event->getField(), $rightValue));
} else if ($hasSelector && isset($value['right_number'][0]['text'])) {
$rightValue = $value['right_number'][0]['text'];
$rightCond = $value['right_number'][0]['condition_operator'];

$qb->andWhere($expr->$rightCond($event->getField(), $rightValue));
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Filter/Extension/Type/TextFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'trim' => true,
),
'choice_options' => array(
'choices' => FilterOperands::getStringOperandsChoices(),
'required' => false,
'translation_domain' => 'LexikFormFilterBundle'
'choices' => FilterOperands::getStringOperandsChoices(),
'required' => false,
'translation_domain' => 'LexikFormFilterBundle'
),
'data_extraction_method' => function (Options $options) {
return $options['compound'] ? 'text' : 'default';
Expand Down
7 changes: 7 additions & 0 deletions Tests/Filter/Doctrine/DBALQueryBuilderUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public function testNumberRange()
));
}

public function testNumberRangeWithSelector()
{
parent::createNumberRangeCompoundTest('getSQL', array(
'SELECT i FROM item i WHERE (i.position_selector > 4) AND (i.position_selector <= 8)',
));
}

public function testNumberRangeDefaultValues()
{
parent::createNumberRangeDefaultValuesTest('getSQL', array(
Expand Down
16 changes: 16 additions & 0 deletions Tests/Filter/Doctrine/DoctrineQueryBuilderUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ protected function createNumberRangeTest($method, array $dqls)
$this->assertEquals($dqls[0], $doctrineQueryBuilder->{$method}());
}

protected function createNumberRangeCompoundTest($method, array $dqls)
{
// use filter type options
$form = $this->formFactory->create(new RangeFilterType());
$filterQueryBuilder = $this->initQueryBuilder();

$doctrineQueryBuilder = $this->createDoctrineQueryBuilder();
$form->bind(array('position_selector' => array(
'left_number' => array('text' => 4, 'condition_operator' => FilterOperands::OPERATOR_GREATER_THAN),
'right_number' => array('text' => 8, 'condition_operator' => FilterOperands::OPERATOR_LOWER_THAN_EQUAL),
)));

$filterQueryBuilder->addFilterConditions($form, $doctrineQueryBuilder);
$this->assertEquals($dqls[0], $doctrineQueryBuilder->{$method}());
}

protected function createNumberRangeDefaultValuesTest($method, array $dqls)
{
// use filter type options
Expand Down
7 changes: 7 additions & 0 deletions Tests/Filter/Doctrine/ORMQueryBuilderUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function testNumberRange()
));
}

public function testNumberRangeWithSelector()
{
parent::createNumberRangeCompoundTest('getSQL', array(
'SELECT i FROM Lexik\Bundle\FormFilterBundle\Tests\Fixtures\Entity\Item i WHERE (i.position_selector > 4) AND (i.position_selector <= 8)',
));
}

public function testNumberRangeDefaultValues()
{
parent::createNumberRangeDefaultValuesTest('getDQL', array(
Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixtures/Filter/RangeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'left_number_options' => array('condition_operator' => FilterOperands::OPERATOR_GREATER_THAN),
'right_number_options' => array('condition_operator' => FilterOperands::OPERATOR_LOWER_THAN)
))
->add('position_selector', 'filter_number_range', array(
'left_number_options' => array('condition_operator' => FilterOperands::OPERAND_SELECTOR),
'right_number_options' => array('condition_operator' => FilterOperands::OPERAND_SELECTOR)
))
->add('default_position', 'filter_number_range')
->add('createdAt', 'filter_date_range', array(
'left_date_options' => array('widget' => 'choice'),
Expand Down

0 comments on commit 379fec7

Please sign in to comment.