Skip to content

Commit

Permalink
Add deprecation for StringFilter format option
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jun 28, 2020
1 parent 5adeca0 commit 2f61fb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
break;
default:
$format = $this->getOption('format');

if ('%%%s%%' !== $format) {
@trigger_error(
'The format option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.',
E_USER_DEPRECATED
);
}
}

$queryBuilder->setParameter(
Expand All @@ -94,6 +101,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
public function getDefaultOptions()
{
return [
// NEXT_MAJOR: Remove the format option.
'format' => '%%%s%%',
'case_sensitive' => true,
];
Expand Down
37 changes: 27 additions & 10 deletions tests/Filter/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testEmpty(): void
public function testNullValue(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);
Expand All @@ -50,28 +50,28 @@ public function testNullValue(): void
public function testContains(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

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

$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters);
$this->assertSame(['field_name_0' => '%asd%'], $builder->parameters);

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

$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => null]);
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters);
$this->assertSame(['field_name_0' => '%asd%'], $builder->parameters);
$this->assertTrue($filter->isActive());
}

public function testStartsWith(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);
Expand All @@ -84,7 +84,7 @@ public function testStartsWith(): void
public function testEndsWith(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);
Expand All @@ -97,21 +97,21 @@ public function testEndsWith(): void
public function testNotContains(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

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

$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]);
$this->assertSame(['alias.field NOT LIKE :field_name_0 OR alias.field IS NULL'], $builder->query);
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters);
$this->assertSame(['field_name_0' => '%asd%'], $builder->parameters);
$this->assertTrue($filter->isActive());
}

public function testEquals(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);
$filter->initialize('field_name');

$builder = new ProxyQuery(new QueryBuilder());
$this->assertSame([], $builder->query);
Expand All @@ -126,7 +126,6 @@ public function testEqualsWithValidParentAssociationMappings(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', [
'format' => '%s',
'field_name' => 'field_name',
'parent_association_mappings' => [
[
Expand Down Expand Up @@ -195,4 +194,22 @@ public function caseSensitiveDataProvider(): array

];
}

/**
* @group legacy
*
* @expectedDeprecation The format option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.
*/
public function testFormatOption(): void
{
$filter = new StringFilter();
$filter->initialize('field_name', ['format' => '%s']);

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

$filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]);
$this->assertSame(['alias.field LIKE :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 'asd'], $builder->parameters);
}
}

0 comments on commit 2f61fb7

Please sign in to comment.