Skip to content

Commit

Permalink
Fix equal for case insensitive (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored May 31, 2020
1 parent 0da9e03 commit 93f5dec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
$this->applyWhere($queryBuilder, $or);

if (ContainsOperatorType::TYPE_EQUAL === $data['type']) {
$queryBuilder->setParameter($parameterName, $data['value']);
$queryBuilder->setParameter(
$parameterName,
$this->getOption('case_sensitive') ? $data['value'] : mb_strtolower($data['value'])
);
} else {
$queryBuilder->setParameter(
$parameterName,
Expand Down
34 changes: 18 additions & 16 deletions tests/Filter/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,31 +139,33 @@ public function testEqualsWithValidParentAssociationMappings(): void
$this->assertTrue($filter->isActive());
}

public function testCaseSensitiveFalse(): void
/**
* @dataProvider caseSensitiveDataProvider
*/
public function testCaseSensitive(bool $caseSensitive, int $operatorType, string $expectedQuery, string $expectedParameter): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['case_sensitive' => false]);
$filter->initialize('field_name', ['case_sensitive' => $caseSensitive]);

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);

$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
$this->assertSame(['LOWER(alias.field) LIKE :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => '%foobar%'], $builder->parameters);
$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => $operatorType]);
$this->assertSame([$expectedQuery], $builder->query);
$this->assertSame(['field_name_0' => $expectedParameter], $builder->parameters);
$this->assertTrue($filter->isActive());
}

public function testCaseSensitiveTrue(): void
public function caseSensitiveDataProvider(): array
{
$filter = new StringFilter();
$filter->initialize('field_name', ['case_sensitive' => true]);

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);

$filter->filter($builder, 'alias', 'field', ['value' => 'FooBar', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => '%FooBar%'], $builder->parameters);
$this->assertTrue($filter->isActive());
return [
[false, ContainsOperatorType::TYPE_CONTAINS, 'LOWER(alias.field) LIKE :field_name_0', '%foobar%'],
[false, ContainsOperatorType::TYPE_NOT_CONTAINS, 'LOWER(alias.field) NOT LIKE :field_name_0 OR alias.field IS NULL', '%foobar%'],
[false, ContainsOperatorType::TYPE_EQUAL, 'LOWER(alias.field) = :field_name_0', 'foobar'],
[true, ContainsOperatorType::TYPE_CONTAINS, 'alias.field LIKE :field_name_0', '%FooBar%'],
[true, ContainsOperatorType::TYPE_NOT_CONTAINS, 'alias.field NOT LIKE :field_name_0 OR alias.field IS NULL', '%FooBar%'],
[true, ContainsOperatorType::TYPE_EQUAL, 'alias.field = :field_name_0', 'FooBar'],

];
}
}

0 comments on commit 93f5dec

Please sign in to comment.